Skip to content

Commit 05e5f7c

Browse files
committed
Refactor DataProviderHelper
1 parent 094bf54 commit 05e5f7c

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

src/Rules/PHPUnit/DataProviderHelper.php

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Rules\IdentifierRuleError;
2020
use PHPStan\Rules\RuleErrorBuilder;
2121
use PHPStan\Type\FileTypeMapper;
22+
use ReflectionMethod;
2223
use function array_merge;
2324
use function count;
2425
use function explode;
@@ -58,56 +59,23 @@ public function __construct(
5859
}
5960

6061
/**
62+
* @param ReflectionMethod|ClassMethod $node
63+
*
6164
* @return iterable<array{ClassReflection|null, string, int}>
6265
*/
6366
public function getDataProviderMethods(
6467
Scope $scope,
65-
ClassMethod $node,
68+
$node,
6669
ClassReflection $classReflection
6770
): iterable
6871
{
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);
9173

9274
if (!$this->phpunit10OrNewer) {
9375
return;
9476
}
9577

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);
11179
}
11280

11381
/**
@@ -306,4 +274,59 @@ private function parseDataProviderAttribute(Attribute $attribute, ClassReflectio
306274
];
307275
}
308276

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+
309332
}

0 commit comments

Comments
 (0)