-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPhpatUsageProvider.php
More file actions
93 lines (73 loc) · 2.59 KB
/
Copy pathPhpatUsageProvider.php
File metadata and controls
93 lines (73 loc) · 2.59 KB
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
<?php declare(strict_types = 1);
namespace ShipMonk\PHPStan\DeadCode\Provider;
use Composer\InstalledVersions;
use PHPStan\DependencyInjection\Container;
use ReflectionMethod;
use function is_object;
use function str_starts_with;
/**
* phpat registers architecture tests as services tagged "phpat.test" in the PHPStan DIC.
* At runtime, phpat iterates those services and invokes their public methods that either
* carry the #[TestRule] attribute or whose name starts with "test" (see PHPat\Test\TestParser).
*
* Their constructors are already covered by PhpStanUsageProvider (registered DIC services),
* so this provider only marks the invoked test methods as used.
*/
final class PhpatUsageProvider extends ReflectionBasedMemberUsageProvider
{
private const TEST_TAG = 'phpat.test';
private const TEST_RULE_ATTRIBUTE = 'PHPat\Test\Attributes\TestRule';
private readonly bool $enabled;
private readonly Container $container;
/**
* @var array<string, true>|null Set of class names tagged as phpat tests, lazily resolved.
*/
private ?array $testClasses = null;
public function __construct(
?bool $enabled,
Container $container,
)
{
$this->enabled = $enabled ?? InstalledVersions::isInstalled('phpat/phpat');
$this->container = $container;
}
public function shouldMarkMethodAsUsed(ReflectionMethod $method): ?VirtualUsageData
{
if (!$this->enabled) {
return null;
}
if (!$method->isPublic()) {
return null;
}
if (!isset($this->getTestClasses()[$method->getDeclaringClass()->getName()])) {
return null;
}
if (!$this->isTestMethod($method)) {
return null;
}
return VirtualUsageData::withNote('Architecture test method invoked by phpat');
}
private function isTestMethod(ReflectionMethod $method): bool
{
if ($method->getAttributes(self::TEST_RULE_ATTRIBUTE) !== []) {
return true;
}
// phpat invokes every public method whose name starts with "test"
return str_starts_with($method->getName(), 'test');
}
/**
* @return array<string, true>
*/
private function getTestClasses(): array
{
if ($this->testClasses === null) {
$this->testClasses = [];
foreach ($this->container->getServicesByTag(self::TEST_TAG) as $service) {
if (is_object($service)) {
$this->testClasses[$service::class] = true;
}
}
}
return $this->testClasses;
}
}