Skip to content

Commit 415ca3d

Browse files
committed
Refactor
1 parent 05e5f7c commit 415ca3d

File tree

4 files changed

+43
-73
lines changed

4 files changed

+43
-73
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
8-
use PHPStan\Reflection\ReflectionProvider;
98
use PHPStan\Rules\Rule;
109
use PHPUnit\Framework\TestCase;
1110
use function count;
@@ -18,11 +17,15 @@ class DataProviderDataRule implements Rule
1817

1918
private TestMethodsHelper $testMethodsHelper;
2019

20+
private DataProviderHelper $dataProviderHelper;
21+
2122
public function __construct(
22-
TestMethodsHelper $testMethodsHelper
23+
TestMethodsHelper $testMethodsHelper,
24+
DataProviderHelper $dataProviderHelper
2325
)
2426
{
2527
$this->testMethodsHelper = $testMethodsHelper;
28+
$this->dataProviderHelper = $dataProviderHelper;
2629
}
2730

2831
public function getNodeType(): string
@@ -62,8 +65,8 @@ public function processNode(Node $node, Scope $scope): array
6265
$testsWithProvider = [];
6366
$testMethods = $this->testMethodsHelper->getTestMethods($classReflection);
6467
foreach ($testMethods as $testMethod) {
65-
foreach ($this->testMethodsHelper->getDataProviderMethods($scope, $testMethod, $classReflection) as [$providerMethod]) {
66-
if ($providerMethod === $method->getName()) {
68+
foreach ($this->dataProviderHelper->getDataProviderMethods($scope, $testMethod, $classReflection) as [, $providerMethodName]) {
69+
if ($providerMethodName === $method->getName()) {
6770
$testsWithProvider[] = $testMethod;
6871
continue 2;
6972
}

src/Rules/PHPUnit/DataProviderHelper.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ private function parseDataProviderAttribute(Attribute $attribute, ClassReflectio
281281
*/
282282
private function yieldDataProviderAttributes($node, ClassReflection $classReflection): iterable
283283
{
284+
if ($node instanceof ReflectionMethod) {
285+
foreach ($node->getAttributes('PHPUnit\Framework\Attributes\DataProvider') as $attr) {
286+
$args = $attr->getArguments();
287+
if (count($args) !== 1) {
288+
continue;
289+
}
290+
291+
yield [$classReflection, $args[0], $node->getStartLine()];
292+
}
293+
294+
return;
295+
}
296+
284297
foreach ($node->attrGroups as $attrGroup) {
285298
foreach ($attrGroup->attrs as $attr) {
286299
$dataProviderMethod = null;
@@ -306,26 +319,28 @@ private function yieldDataProviderAttributes($node, ClassReflection $classReflec
306319
private function yieldDataProviderAnnotations($node, Scope $scope, ClassReflection $classReflection): iterable
307320
{
308321
$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();
322+
if ($docComment === null || $docComment === false) {
323+
return;
324+
}
326325

327-
yield $dataProviderValue => $dataProviderMethod;
326+
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
327+
$scope->getFile(),
328+
$classReflection->getName(),
329+
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
330+
$node instanceof ClassMethod ? $node->name->toString() : $node->getName(),
331+
$node instanceof ClassMethod ? $docComment->getText() : $docComment,
332+
);
333+
foreach ($this->getDataProviderAnnotations($methodPhpDoc) as $annotation) {
334+
$dataProviderValue = $this->getDataProviderAnnotationValue($annotation);
335+
if ($dataProviderValue === null) {
336+
// Missing value is already handled in NoMissingSpaceInMethodAnnotationRule
337+
continue;
328338
}
339+
340+
$dataProviderMethod = $this->parseDataProviderAnnotationValue($scope, $dataProviderValue);
341+
$dataProviderMethod[] = $node->getStartLine();
342+
343+
yield $dataProviderValue => $dataProviderMethod;
329344
}
330345
}
331346

src/Rules/PHPUnit/TestMethodsHelper.php

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace PHPStan\Rules\PHPUnit;
44

5-
use PHPStan\Analyser\Scope;
65
use PHPStan\Parser\Parser;
76
use PHPStan\Reflection\ClassReflection;
87
use PHPStan\Reflection\ReflectionProvider;
98
use PHPStan\Type\FileTypeMapper;
109
use ReflectionMethod;
11-
use function count;
1210
use function str_starts_with;
1311
use function strtolower;
1412

@@ -57,52 +55,4 @@ public function getTestMethods(ClassReflection $class): array
5755
return $testMethods;
5856
}
5957

60-
/**
61-
* @return iterable<array{string}>
62-
*/
63-
public function getDataProviderMethods(
64-
Scope $scope,
65-
ReflectionMethod $node,
66-
ClassReflection $classReflection
67-
): iterable
68-
{
69-
/*
70-
$docComment = $node->getDocComment();
71-
if ($docComment !== null) {
72-
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
73-
$scope->getFile(),
74-
$classReflection->getName(),
75-
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
76-
$node->name->toString(),
77-
$docComment->getText(),
78-
);
79-
foreach ($this->getDataProviderAnnotations($methodPhpDoc) as $annotation) {
80-
$dataProviderValue = $this->getDataProviderAnnotationValue($annotation);
81-
if ($dataProviderValue === null) {
82-
// Missing value is already handled in NoMissingSpaceInMethodAnnotationRule
83-
continue;
84-
}
85-
86-
$dataProviderMethod = $this->parseDataProviderAnnotationValue($scope, $dataProviderValue);
87-
$dataProviderMethod[] = $node->getStartLine();
88-
89-
yield $dataProviderValue => $dataProviderMethod;
90-
}
91-
}
92-
93-
if (!$this->phpunit10OrNewer) {
94-
return;
95-
}
96-
*/
97-
98-
foreach ($node->getAttributes('PHPUnit\Framework\Attributes\DataProvider') as $attr) {
99-
$args = $attr->getArguments();
100-
if (count($args) !== 1) {
101-
continue;
102-
}
103-
104-
yield [$args[0]];
105-
}
106-
}
107-
10858
}

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Rules\NullsafeCheck;
1111
use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper;
1212
use PHPStan\Rules\PHPUnit\DataProviderDataRule;
13+
use PHPStan\Rules\PHPUnit\DataProviderHelper;
1314
use PHPStan\Rules\PHPUnit\TestMethodsHelper;
1415
use PHPStan\Rules\Properties\PropertyReflectionFinder;
1516
use PHPStan\Rules\Rule;
@@ -29,7 +30,8 @@ protected function getRule(): Rule
2930

3031
return new CompositeRule(new DirectRegistry([
3132
new DataProviderDataRule(
32-
new TestMethodsHelper($reflectionProvider, self::getContainer()->getByType(FileTypeMapper::class), self::getContainer()->getService('defaultAnalysisParser'))
33+
new TestMethodsHelper($reflectionProvider, self::getContainer()->getByType(FileTypeMapper::class), self::getContainer()->getService('defaultAnalysisParser')),
34+
new DataProviderHelper($reflectionProvider, self::getContainer()->getByType(FileTypeMapper::class), self::getContainer()->getService('defaultAnalysisParser'), true),
3335
),
3436
self::getContainer()->getByType(CallMethodsRule::class)
3537
]));

0 commit comments

Comments
 (0)