Skip to content

Commit b919664

Browse files
Eliminate code duplication
1 parent 084a4cd commit b919664

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/Target/MapBuilder.php

+17-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use function array_unique;
1515
use function range;
1616
use SebastianBergmann\CodeCoverage\Filter;
17+
use SebastianBergmann\CodeCoverage\StaticAnalysis\Class_;
1718
use SebastianBergmann\CodeCoverage\StaticAnalysis\FileAnalyser;
19+
use SebastianBergmann\CodeCoverage\StaticAnalysis\Trait_;
1820

1921
/**
2022
* @phpstan-import-type TargetMap from Mapper
@@ -50,16 +52,7 @@ public function build(Filter $filter, FileAnalyser $analyser): array
5052
}
5153

5254
$this->process($traits, $trait->namespacedName(), $file, $trait->startLine(), $trait->endLine());
53-
54-
foreach ($trait->methods() as $method) {
55-
$methodName = $trait->namespacedName() . '::' . $method->name();
56-
57-
$this->process($methods, $methodName, $file, $method->startLine(), $method->endLine());
58-
59-
foreach (range($method->startLine(), $method->endLine()) as $line) {
60-
$reverseLookup[$file . ':' . $line] = $methodName;
61-
}
62-
}
55+
$this->processMethods($trait, $file, $methods, $reverseLookup);
6356
}
6457
}
6558

@@ -121,15 +114,7 @@ public function build(Filter $filter, FileAnalyser $analyser): array
121114
}
122115
}
123116

124-
foreach ($class->methods() as $method) {
125-
$methodName = $class->namespacedName() . '::' . $method->name();
126-
127-
$this->process($methods, $methodName, $file, $method->startLine(), $method->endLine());
128-
129-
foreach (range($method->startLine(), $method->endLine()) as $line) {
130-
$reverseLookup[$file . ':' . $line] = $methodName;
131-
}
132-
}
117+
$this->processMethods($class, $file, $methods, $reverseLookup);
133118

134119
$classesThatExtendClass[$class->namespacedName()] = [];
135120
$classDetails[] = $class;
@@ -201,6 +186,19 @@ public function build(Filter $filter, FileAnalyser $analyser): array
201186
];
202187
}
203188

189+
private function processMethods(Class_|Trait_ $classOrTrait, string $file, array &$methods, array &$reverseLookup): void
190+
{
191+
foreach ($classOrTrait->methods() as $method) {
192+
$methodName = $classOrTrait->namespacedName() . '::' . $method->name();
193+
194+
$this->process($methods, $methodName, $file, $method->startLine(), $method->endLine());
195+
196+
foreach (range($method->startLine(), $method->endLine()) as $line) {
197+
$reverseLookup[$file . ':' . $line] = $methodName;
198+
}
199+
}
200+
}
201+
204202
/**
205203
* @param TargetMapPart $data
206204
* @param non-empty-string $unit

0 commit comments

Comments
 (0)