Skip to content

Commit 2058fb9

Browse files
committed
Report data providers deprecated usage
1 parent 4c06b7e commit 2058fb9

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.2 || ^8.0",
10-
"phpstan/phpstan": "^1.9.0"
10+
"phpstan/phpstan": "^1.9.3"
1111
},
1212
"conflict": {
1313
"phpunit/phpunit": "<7.0"

rules.neon

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
1313
arguments:
1414
checkFunctionNameCase: %checkFunctionNameCase%
15+
deprecationRulesInstalled: %deprecationRulesInstalled%
1516
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule
1617
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule
1718

src/Rules/PHPUnit/DataProviderDeclarationRule.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,24 @@ class DataProviderDeclarationRule implements Rule
3636
*/
3737
private $checkFunctionNameCase;
3838

39+
/**
40+
* When phpstan-deprecation-rules is installed, it reports deprecated usages.
41+
*
42+
* @var bool
43+
*/
44+
private $deprecationRulesInstalled;
45+
3946
public function __construct(
4047
DataProviderHelper $dataProviderHelper,
4148
FileTypeMapper $fileTypeMapper,
42-
bool $checkFunctionNameCase
49+
bool $checkFunctionNameCase,
50+
bool $deprecationRulesInstalled
4351
)
4452
{
4553
$this->dataProviderHelper = $dataProviderHelper;
4654
$this->fileTypeMapper = $fileTypeMapper;
4755
$this->checkFunctionNameCase = $checkFunctionNameCase;
56+
$this->deprecationRulesInstalled = $deprecationRulesInstalled;
4857
}
4958

5059
public function getNodeType(): string
@@ -80,7 +89,12 @@ public function processNode(Node $node, Scope $scope): array
8089
foreach ($annotations as $annotation) {
8190
$errors = array_merge(
8291
$errors,
83-
$this->dataProviderHelper->processDataProvider($scope, $annotation, $this->checkFunctionNameCase)
92+
$this->dataProviderHelper->processDataProvider(
93+
$scope,
94+
$annotation,
95+
$this->checkFunctionNameCase,
96+
$this->deprecationRulesInstalled
97+
)
8498
);
8599
}
86100

src/Rules/PHPUnit/DataProviderHelper.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array
4444
public function processDataProvider(
4545
Scope $scope,
4646
PhpDocTagNode $phpDocTag,
47-
bool $checkFunctionNameCase
47+
bool $checkFunctionNameCase,
48+
bool $deprecationRulesInstalled
4849
): array
4950
{
5051
$dataProviderName = $this->getDataProviderName($phpDocTag);
@@ -87,6 +88,13 @@ public function processDataProvider(
8788
))->build();
8889
}
8990

91+
if ($deprecationRulesInstalled && !$dataProviderMethodReflection->isStatic()) {
92+
$errors[] = RuleErrorBuilder::message(sprintf(
93+
'@dataProvider %s related method must be static.',
94+
$dataProviderName
95+
))->build();
96+
}
97+
9098
return $errors;
9199
}
92100

tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protected function getRule(): Rule
1717
return new DataProviderDeclarationRule(
1818
new DataProviderHelper(),
1919
self::getContainer()->getByType(FileTypeMapper::class),
20+
true,
2021
true
2122
);
2223
}
@@ -28,6 +29,10 @@ public function testRule(): void
2829
'@dataProvider providebaz related method is used with incorrect case: provideBaz.',
2930
13,
3031
],
32+
[
33+
'@dataProvider provideQux related method must be static.',
34+
13,
35+
],
3136
[
3237
'@dataProvider provideQuux related method must be public.',
3338
13,

0 commit comments

Comments
 (0)