|
19 | 19 | use PHPStan\Rules\IdentifierRuleError; |
20 | 20 | use PHPStan\Rules\RuleErrorBuilder; |
21 | 21 | use PHPStan\Type\FileTypeMapper; |
| 22 | +use ReflectionMethod; |
22 | 23 | use function array_merge; |
23 | 24 | use function count; |
24 | 25 | use function explode; |
@@ -58,56 +59,23 @@ public function __construct( |
58 | 59 | } |
59 | 60 |
|
60 | 61 | /** |
| 62 | + * @param ReflectionMethod|ClassMethod $node |
| 63 | + * |
61 | 64 | * @return iterable<array{ClassReflection|null, string, int}> |
62 | 65 | */ |
63 | 66 | public function getDataProviderMethods( |
64 | 67 | Scope $scope, |
65 | | - ClassMethod $node, |
| 68 | + $node, |
66 | 69 | ClassReflection $classReflection |
67 | 70 | ): iterable |
68 | 71 | { |
69 | | - $docComment = $node->getDocComment(); |
70 | | - if ($docComment !== null) { |
71 | | - $methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
72 | | - $scope->getFile(), |
73 | | - $classReflection->getName(), |
74 | | - $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
75 | | - $node->name->toString(), |
76 | | - $docComment->getText(), |
77 | | - ); |
78 | | - foreach ($this->getDataProviderAnnotations($methodPhpDoc) as $annotation) { |
79 | | - $dataProviderValue = $this->getDataProviderAnnotationValue($annotation); |
80 | | - if ($dataProviderValue === null) { |
81 | | - // Missing value is already handled in NoMissingSpaceInMethodAnnotationRule |
82 | | - continue; |
83 | | - } |
84 | | - |
85 | | - $dataProviderMethod = $this->parseDataProviderAnnotationValue($scope, $dataProviderValue); |
86 | | - $dataProviderMethod[] = $node->getStartLine(); |
87 | | - |
88 | | - yield $dataProviderValue => $dataProviderMethod; |
89 | | - } |
90 | | - } |
| 72 | + yield from $this->yieldDataProviderAnnotations($node, $scope, $classReflection); |
91 | 73 |
|
92 | 74 | if (!$this->phpunit10OrNewer) { |
93 | 75 | return; |
94 | 76 | } |
95 | 77 |
|
96 | | - foreach ($node->attrGroups as $attrGroup) { |
97 | | - foreach ($attrGroup->attrs as $attr) { |
98 | | - $dataProviderMethod = null; |
99 | | - if ($attr->name->toLowerString() === 'phpunit\\framework\\attributes\\dataprovider') { |
100 | | - $dataProviderMethod = $this->parseDataProviderAttribute($attr, $classReflection); |
101 | | - } elseif ($attr->name->toLowerString() === 'phpunit\\framework\\attributes\\dataproviderexternal') { |
102 | | - $dataProviderMethod = $this->parseDataProviderExternalAttribute($attr); |
103 | | - } |
104 | | - if ($dataProviderMethod === null) { |
105 | | - continue; |
106 | | - } |
107 | | - |
108 | | - yield from $dataProviderMethod; |
109 | | - } |
110 | | - } |
| 78 | + yield from $this->yieldDataProviderAttributes($node, $classReflection); |
111 | 79 | } |
112 | 80 |
|
113 | 81 | /** |
@@ -306,4 +274,59 @@ private function parseDataProviderAttribute(Attribute $attribute, ClassReflectio |
306 | 274 | ]; |
307 | 275 | } |
308 | 276 |
|
| 277 | + /** |
| 278 | + * @param ReflectionMethod|ClassMethod $node |
| 279 | + * |
| 280 | + * @return iterable<array{ClassReflection|null, string, int}> |
| 281 | + */ |
| 282 | + private function yieldDataProviderAttributes($node, ClassReflection $classReflection): iterable |
| 283 | + { |
| 284 | + foreach ($node->attrGroups as $attrGroup) { |
| 285 | + foreach ($attrGroup->attrs as $attr) { |
| 286 | + $dataProviderMethod = null; |
| 287 | + if ($attr->name->toLowerString() === 'phpunit\\framework\\attributes\\dataprovider') { |
| 288 | + $dataProviderMethod = $this->parseDataProviderAttribute($attr, $classReflection); |
| 289 | + } elseif ($attr->name->toLowerString() === 'phpunit\\framework\\attributes\\dataproviderexternal') { |
| 290 | + $dataProviderMethod = $this->parseDataProviderExternalAttribute($attr); |
| 291 | + } |
| 292 | + if ($dataProviderMethod === null) { |
| 293 | + continue; |
| 294 | + } |
| 295 | + |
| 296 | + yield from $dataProviderMethod; |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * @param ReflectionMethod|ClassMethod $node |
| 303 | + * |
| 304 | + * @return iterable<array{ClassReflection|null, string, int}> |
| 305 | + */ |
| 306 | + private function yieldDataProviderAnnotations($node, Scope $scope, ClassReflection $classReflection): iterable |
| 307 | + { |
| 308 | + $docComment = $node->getDocComment(); |
| 309 | + if ($docComment !== null) { |
| 310 | + $methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( |
| 311 | + $scope->getFile(), |
| 312 | + $classReflection->getName(), |
| 313 | + $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, |
| 314 | + $node->name->toString(), |
| 315 | + $docComment->getText(), |
| 316 | + ); |
| 317 | + foreach ($this->getDataProviderAnnotations($methodPhpDoc) as $annotation) { |
| 318 | + $dataProviderValue = $this->getDataProviderAnnotationValue($annotation); |
| 319 | + if ($dataProviderValue === null) { |
| 320 | + // Missing value is already handled in NoMissingSpaceInMethodAnnotationRule |
| 321 | + continue; |
| 322 | + } |
| 323 | + |
| 324 | + $dataProviderMethod = $this->parseDataProviderAnnotationValue($scope, $dataProviderValue); |
| 325 | + $dataProviderMethod[] = $node->getStartLine(); |
| 326 | + |
| 327 | + yield $dataProviderValue => $dataProviderMethod; |
| 328 | + } |
| 329 | + } |
| 330 | + } |
| 331 | + |
309 | 332 | } |
0 commit comments