Skip to content

Commit

Permalink
More work on mapping code coverage targets to source locations
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 31, 2024
1 parent 2947257 commit 043bf9a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/Target/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,21 @@ private function mapTarget(Target $target): array
if ($target->isClass()) {
assert($target instanceof Class_);

if (!isset($this->map['classes'][$target->className()])) {
throw new InvalidCodeCoverageTargetException('Class ' . $target->className());
if (!isset($this->map['classes'][$target->asString()])) {
throw new InvalidCodeCoverageTargetException('Class ' . $target->asString());
}

return $this->map['classes'][$target->className()];
return $this->map['classes'][$target->asString()];

Check failure on line 78 in src/Target/Mapper.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Method SebastianBergmann\CodeCoverage\Test\Target\Mapper::mapTarget() should return array<non-empty-string, array<int, int<1, max>>> but returns array<int, int<1, max>>.
}

if ($target->isMethod()) {

Check failure on line 81 in src/Target/Mapper.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Method SebastianBergmann\CodeCoverage\Test\Target\Mapper::mapTarget() should return array<string, array<int, int<1, max>>> but return statement is missing.
assert($target instanceof Method);

if (!isset($this->map['methods'][$target->asString()])) {
throw new InvalidCodeCoverageTargetException('Method ' . $target->asString());
}

return $this->map['methods'][$target->asString()];

Check failure on line 88 in src/Target/Mapper.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Method SebastianBergmann\CodeCoverage\Test\Target\Mapper::mapTarget() should return array<non-empty-string, array<int, int<1, max>>> but returns array<int, int<1, max>>.
}
}
}
37 changes: 35 additions & 2 deletions tests/tests/Target/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace SebastianBergmann\CodeCoverage\Test\Target;

use function array_keys;
use function array_merge;
use function range;
use function realpath;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -44,6 +45,29 @@ public static function provider(): array
],
),
],
[
'single method',
[
$file => range(37, 39),
],
TargetCollection::fromArray(
[
Target::forMethod('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass', 'six'),
],
),
],
[
'multiple methods',
[
$file => array_merge(range(37, 39), range(41, 43)),
],
TargetCollection::fromArray(
[
Target::forMethod('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass', 'six'),
Target::forMethod('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass', 'one'),
],
),
],
];
}

Expand All @@ -55,10 +79,19 @@ public static function invalidProvider(): array
return [
[
'single class',
'Class SebastianBergmann\CodeCoverage\StaticAnalysis\ChildClass is not a valid target for code coverage',
'Class DoesNotExist is not a valid target for code coverage',
TargetCollection::fromArray(
[
Target::forClass('SebastianBergmann\\CodeCoverage\\StaticAnalysis\\ChildClass'),
Target::forClass('DoesNotExist'),
],
),
],
[
'single method',
'Method DoesNotExist::doesNotExist is not a valid target for code coverage',
TargetCollection::fromArray(
[
Target::forMethod('DoesNotExist', 'doesNotExist'),
],
),
],
Expand Down

0 comments on commit 043bf9a

Please sign in to comment.