Skip to content

Commit

Permalink
Handle sub-namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 23, 2024
1 parent 2c52441 commit 752cfed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Target/MapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

use function array_keys;
use function array_merge;
use function array_slice;
use function array_unique;
use function count;
use function explode;
use function implode;
use function range;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\StaticAnalysis\Class_;
Expand Down Expand Up @@ -48,7 +52,7 @@ public function build(Filter $filter, FileAnalyser $analyser): array
foreach ($filter->files() as $file) {
foreach ($analyser->traitsIn($file) as $trait) {
if ($trait->isNamespaced()) {
$this->process($namespaces, $trait->namespace(), $file, $trait->startLine(), $trait->endLine());
$this->processNamespace($namespaces, $trait->namespace(), $file, $trait->startLine(), $trait->endLine());
}

$this->process($traits, $trait->namespacedName(), $file, $trait->startLine(), $trait->endLine());
Expand Down Expand Up @@ -88,7 +92,7 @@ public function build(Filter $filter, FileAnalyser $analyser): array

foreach ($analyser->classesIn($file) as $class) {
if ($class->isNamespaced()) {
$this->process($namespaces, $class->namespace(), $file, $class->startLine(), $class->endLine());
$this->processNamespace($namespaces, $class->namespace(), $file, $class->startLine(), $class->endLine());
}

$this->process($classes, $class->namespacedName(), $file, $class->startLine(), $class->endLine());
Expand Down Expand Up @@ -122,7 +126,7 @@ public function build(Filter $filter, FileAnalyser $analyser): array

foreach ($analyser->functionsIn($file) as $function) {
if ($function->isNamespaced()) {
$this->process($namespaces, $function->namespace(), $file, $function->startLine(), $function->endLine());
$this->processNamespace($namespaces, $function->namespace(), $file, $function->startLine(), $function->endLine());
}

$this->process($functions, $function->namespacedName(), $file, $function->startLine(), $function->endLine());
Expand Down Expand Up @@ -199,6 +203,24 @@ private function processMethods(Class_|Trait_ $classOrTrait, string $file, array
}
}

/**
* @param TargetMapPart $data
* @param non-empty-string $namespace
* @param non-empty-string $file
* @param positive-int $startLine
* @param positive-int $endLine
*
* @param-out TargetMapPart $data
*/
private function processNamespace(array &$data, string $namespace, string $file, int $startLine, int $endLine): void
{
$parts = explode('\\', $namespace);

foreach (range(1, count($parts)) as $i) {
$this->process($data, implode('\\', array_slice($parts, 0, $i)), $file, $startLine, $endLine);
}
}

/**
* @param TargetMapPart $data
* @param non-empty-string $unit
Expand Down
16 changes: 16 additions & 0 deletions tests/tests/Target/MapBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public function testBuildsMap(): void
$this->assertSame(
[
'namespaces' => [
'SebastianBergmann' => [
$file => array_merge(
range(19, 24),
range(26, 31),
range(33, 52),
range(54, 56),
),
],
'SebastianBergmann\\CodeCoverage' => [
$file => array_merge(
range(19, 24),
range(26, 31),
range(33, 52),
range(54, 56),
),
],
'SebastianBergmann\\CodeCoverage\\StaticAnalysis' => [
$file => array_merge(
range(19, 24),
Expand Down

0 comments on commit 752cfed

Please sign in to comment.