Skip to content

Commit c4ba7f2

Browse files
More work on mapping code coverage targets to source locations
1 parent 9549d36 commit c4ba7f2

12 files changed

+228
-61
lines changed

src/Exception/InvalidCodeCoverageTargetException.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10-
namespace SebastianBergmann\CodeCoverage;
10+
namespace SebastianBergmann\CodeCoverage\Test\Target;
1111

1212
use function sprintf;
1313
use RuntimeException;
14+
use SebastianBergmann\CodeCoverage\Exception;
1415

1516
final class InvalidCodeCoverageTargetException extends RuntimeException implements Exception
1617
{
17-
/**
18-
* @param non-empty-string $target
19-
*/
20-
public function __construct(string $target)
18+
public function __construct(Target $target)
2119
{
2220
parent::__construct(
2321
sprintf(
2422
'%s is not a valid target for code coverage',
25-
$target,
23+
$target->description(),
2624
),
2725
);
2826
}

src/Target/Class_.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ public function className(): string
4545
/**
4646
* @return non-empty-string
4747
*/
48-
public function asString(): string
48+
public function key(): string
49+
{
50+
return 'classes';
51+
}
52+
53+
/**
54+
* @return non-empty-string
55+
*/
56+
public function target(): string
4957
{
5058
return $this->className;
5159
}
60+
61+
/**
62+
* @return non-empty-string
63+
*/
64+
public function description(): string
65+
{
66+
return 'Class ' . $this->target();
67+
}
5268
}

src/Target/ClassesThatExtendClass.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ public function className(): string
4545
/**
4646
* @return non-empty-string
4747
*/
48-
public function asString(): string
48+
public function key(): string
49+
{
50+
return 'classesThatExtendClass';
51+
}
52+
53+
/**
54+
* @return non-empty-string
55+
*/
56+
public function target(): string
4957
{
5058
return $this->className;
5159
}
60+
61+
/**
62+
* @return non-empty-string
63+
*/
64+
public function description(): string
65+
{
66+
return 'Classes that extend class ' . $this->target();
67+
}
5268
}

src/Target/ClassesThatImplementInterface.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ public function interfaceName(): string
4545
/**
4646
* @return non-empty-string
4747
*/
48-
public function asString(): string
48+
public function key(): string
49+
{
50+
return 'classesThatImplementInterface';
51+
}
52+
53+
/**
54+
* @return non-empty-string
55+
*/
56+
public function target(): string
4957
{
5058
return $this->interfaceName;
5159
}
60+
61+
/**
62+
* @return non-empty-string
63+
*/
64+
public function description(): string
65+
{
66+
return 'Classes that implement interface ' . $this->target();
67+
}
5268
}

src/Target/Function_.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ public function functionName(): string
4545
/**
4646
* @return non-empty-string
4747
*/
48-
public function asString(): string
48+
public function key(): string
49+
{
50+
return 'functions';
51+
}
52+
53+
/**
54+
* @return non-empty-string
55+
*/
56+
public function target(): string
4957
{
5058
return $this->functionName;
5159
}
60+
61+
/**
62+
* @return non-empty-string
63+
*/
64+
public function description(): string
65+
{
66+
return 'Function ' . $this->target();
67+
}
5268
}

src/Target/Mapper.php

+5-20
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111

1212
use function array_merge;
1313
use function array_unique;
14-
use function assert;
1514
use function sort;
16-
use SebastianBergmann\CodeCoverage\InvalidCodeCoverageTargetException;
1715

1816
/**
19-
* @phpstan-type TargetMap = array{namespaces: array<non-empty-string, list<positive-int>>, classes: array<non-empty-string, list<positive-int>>, classesThatExtendClass: array<non-empty-string, list<positive-int>>, classesThatImplementInterface: array<non-empty-string, list<positive-int>>, traits: array<non-empty-string, list<positive-int>>, methods: array<non-empty-string, list<positive-int>>, functions: array<non-empty-string, list<positive-int>>}
17+
* @phpstan-type TargetMap = array{namespaces: TargetMapPart, classes: TargetMapPart, classesThatExtendClass: TargetMapPart, classesThatImplementInterface: TargetMapPart, traits: TargetMapPart, methods: TargetMapPart, functions: TargetMapPart}
18+
* @phpstan-type TargetMapPart = array<non-empty-string, array<non-empty-string, list<positive-int>>>
2019
*
2120
* @immutable
2221
*
@@ -70,24 +69,10 @@ public function map(TargetCollection $targets): array
7069
*/
7170
private function mapTarget(Target $target): array
7271
{
73-
if ($target->isClass()) {
74-
assert($target instanceof Class_);
75-
76-
if (!isset($this->map['classes'][$target->asString()])) {
77-
throw new InvalidCodeCoverageTargetException('Class ' . $target->asString());
78-
}
79-
80-
return $this->map['classes'][$target->asString()];
72+
if (!isset($this->map[$target->key()][$target->target()])) {
73+
throw new InvalidCodeCoverageTargetException($target);
8174
}
8275

83-
if ($target->isMethod()) {
84-
assert($target instanceof Method);
85-
86-
if (!isset($this->map['methods'][$target->asString()])) {
87-
throw new InvalidCodeCoverageTargetException('Method ' . $target->asString());
88-
}
89-
90-
return $this->map['methods'][$target->asString()];
91-
}
76+
return $this->map[$target->key()][$target->target()];
9277
}
9378
}

src/Target/Method.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,24 @@ public function methodName(): string
6060
/**
6161
* @return non-empty-string
6262
*/
63-
public function asString(): string
63+
public function key(): string
64+
{
65+
return 'methods';
66+
}
67+
68+
/**
69+
* @return non-empty-string
70+
*/
71+
public function target(): string
6472
{
6573
return $this->className . '::' . $this->methodName;
6674
}
75+
76+
/**
77+
* @return non-empty-string
78+
*/
79+
public function description(): string
80+
{
81+
return 'Method ' . $this->target();
82+
}
6783
}

src/Target/Namespace_.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,24 @@ public function namespace(): string
4545
/**
4646
* @return non-empty-string
4747
*/
48-
public function asString(): string
48+
public function key(): string
49+
{
50+
return 'namespaces';
51+
}
52+
53+
/**
54+
* @return non-empty-string
55+
*/
56+
public function target(): string
4957
{
5058
return $this->namespace;
5159
}
60+
61+
/**
62+
* @return non-empty-string
63+
*/
64+
public function description(): string
65+
{
66+
return 'Namespace ' . $this->target();
67+
}
5268
}

src/Target/Target.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,15 @@ public function isFunction(): bool
9898
/**
9999
* @return non-empty-string
100100
*/
101-
abstract public function asString(): string;
101+
abstract public function key(): string;
102+
103+
/**
104+
* @return non-empty-string
105+
*/
106+
abstract public function target(): string;
107+
108+
/**
109+
* @return non-empty-string
110+
*/
111+
abstract public function description(): string;
102112
}

tests/tests/Target/MapBuilderTest.php

-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ public function testBuildsMap(): void
9595
);
9696
}
9797

98-
/**
99-
* @return array{namespaces: array<non-empty-string, list<positive-int>>, classes: array<non-empty-string, list<positive-int>>, classesThatExtendClass: array<non-empty-string, list<positive-int>>, classesThatImplementInterface: array<non-empty-string, list<positive-int>>, traits: array<non-empty-string, list<positive-int>>, methods: array<non-empty-string, list<positive-int>>, functions: array<non-empty-string, list<positive-int>>}
100-
*/
10198
private function map(array $files): array
10299
{
103100
$filter = new Filter;

0 commit comments

Comments
 (0)