forked from phpstan/phpstan-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMethodsHelper.php
More file actions
111 lines (89 loc) · 2.58 KB
/
TestMethodsHelper.php
File metadata and controls
111 lines (89 loc) · 2.58 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\FileTypeMapper;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use function str_starts_with;
use function strtolower;
final class TestMethodsHelper
{
private FileTypeMapper $fileTypeMapper;
private PHPUnitVersion $PHPUnitVersion;
public function __construct(
FileTypeMapper $fileTypeMapper,
PHPUnitVersion $PHPUnitVersion
)
{
$this->fileTypeMapper = $fileTypeMapper;
$this->PHPUnitVersion = $PHPUnitVersion;
}
public function getTestMethodReflection(ClassReflection $classReflection, MethodReflection $methodReflection, Scope $scope): ?ReflectionMethod
{
foreach ($this->getTestMethods($classReflection, $scope) as $testMethod) {
if ($testMethod->getName() === $methodReflection->getName()) {
return $testMethod;
}
}
return null;
}
/**
* @return array<ReflectionMethod>
*/
public function getTestMethods(ClassReflection $classReflection, Scope $scope): array
{
if (!$classReflection->is(TestCase::class)) {
return [];
}
$testMethods = [];
foreach ($classReflection->getNativeReflection()->getMethods() as $reflectionMethod) {
if (!$reflectionMethod->isPublic()) {
continue;
}
if (str_starts_with(strtolower($reflectionMethod->getName()), 'test')) {
$testMethods[] = $reflectionMethod;
continue;
}
$docComment = $reflectionMethod->getDocComment();
if ($docComment !== false) {
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
$classReflection->getName(),
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
$reflectionMethod->getName(),
$docComment,
);
if ($this->hasTestAnnotation($methodPhpDoc)) {
$testMethods[] = $reflectionMethod;
continue;
}
}
if ($this->PHPUnitVersion->supportsTestAttribute()->no()) {
continue;
}
$testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\Test'); // @phpstan-ignore argument.type
if ($testAttributes === []) {
continue;
}
$testMethods[] = $reflectionMethod;
}
return $testMethods;
}
private function hasTestAnnotation(?ResolvedPhpDocBlock $phpDoc): bool
{
if ($phpDoc === null) {
return false;
}
$phpDocNodes = $phpDoc->getPhpDocNodes();
foreach ($phpDocNodes as $docNode) {
$tags = $docNode->getTagsByName('@test');
if ($tags !== []) {
return true;
}
}
return false;
}
}