forked from phpstan/phpstan-phpunit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataProviderDeclarationRule.php
110 lines (91 loc) · 2.4 KB
/
DataProviderDeclarationRule.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php declare(strict_types = 1);
namespace PHPStan\Rules\PHPUnit;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\FileTypeMapper;
use PHPUnit\Framework\TestCase;
use function array_merge;
/**
* @implements Rule<Node\Stmt\ClassMethod>
*/
class DataProviderDeclarationRule implements Rule
{
/**
* Data provider helper.
*
* @var DataProviderHelper
*/
private $dataProviderHelper;
/**
* The file type mapper.
*
* @var FileTypeMapper
*/
private $fileTypeMapper;
/**
* When set to true, it reports data provider method with incorrect name case.
*
* @var bool
*/
private $checkFunctionNameCase;
/**
* When phpstan-deprecation-rules is installed, it reports deprecated usages.
*
* @var bool
*/
private $deprecationRulesInstalled;
public function __construct(
DataProviderHelper $dataProviderHelper,
FileTypeMapper $fileTypeMapper,
bool $checkFunctionNameCase,
bool $deprecationRulesInstalled
)
{
$this->dataProviderHelper = $dataProviderHelper;
$this->fileTypeMapper = $fileTypeMapper;
$this->checkFunctionNameCase = $checkFunctionNameCase;
$this->deprecationRulesInstalled = $deprecationRulesInstalled;
}
public function getNodeType(): string
{
return Node\Stmt\ClassMethod::class;
}
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if ($classReflection === null || !$classReflection->isSubclassOf(TestCase::class)) {
return [];
}
$docComment = $node->getDocComment();
if ($docComment === null) {
return [];
}
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
$classReflection->getName(),
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
$node->name->toString(),
$docComment->getText()
);
$annotations = $this->dataProviderHelper->getDataProviderAnnotations($methodPhpDoc);
$testMethodReflection = null;
if ($classReflection->hasMethod($node->name->toString())) {
$testMethodReflection = $classReflection->getMethod($node->name->toString(), $scope);
}
$errors = [];
foreach ($annotations as $annotation) {
$errors = array_merge(
$errors,
$this->dataProviderHelper->processDataProvider(
$scope,
$annotation,
$this->checkFunctionNameCase,
$this->deprecationRulesInstalled,
$testMethodReflection
)
);
}
return $errors;
}
}