forked from sebastianbergmann/php-code-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapper.php
96 lines (82 loc) · 2.65 KB
/
Mapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-code-coverage.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeCoverage\Test\Target;
use function array_merge;
use function array_unique;
use function sort;
/**
* @phpstan-type TargetMap array{namespaces: TargetMapPart, classes: TargetMapPart, classesThatExtendClass: TargetMapPart, classesThatImplementInterface: TargetMapPart, traits: TargetMapPart, methods: TargetMapPart, functions: TargetMapPart, reverseLookup: ReverseLookup}
* @phpstan-type TargetMapPart array<non-empty-string, array<non-empty-string, list<positive-int>>>
* @phpstan-type ReverseLookup array<non-empty-string, non-empty-string>
*
* @immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for phpunit/php-code-coverage
*
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
*/
final readonly class Mapper
{
/**
* @var TargetMap
*/
private array $map;
/**
* @param TargetMap $map
*/
public function __construct(array $map)
{
$this->map = $map;
}
/**
* @return array<non-empty-string, list<positive-int>>
*/
public function mapTargets(TargetCollection $targets): array
{
$result = [];
foreach ($targets as $target) {
foreach ($this->mapTarget($target) as $file => $lines) {
if (!isset($result[$file])) {
$result[$file] = $lines;
continue;
}
$result[$file] = array_unique(array_merge($result[$file], $lines));
sort($result[$file]);
}
}
return $result;
}
/**
* @throws InvalidCodeCoverageTargetException
*
* @return array<non-empty-string, list<positive-int>>
*/
public function mapTarget(Target $target): array
{
if (!isset($this->map[$target->key()][$target->target()])) {
throw new InvalidCodeCoverageTargetException($target);
}
return $this->map[$target->key()][$target->target()];
}
/**
* @param non-empty-string $file
* @param positive-int $line
*
* @return non-empty-string
*/
public function lookup(string $file, int $line): string
{
$key = $file . ':' . $line;
if (isset($this->map['reverseLookup'][$key])) {
return $this->map['reverseLookup'][$key];
}
return $key;
}
}