Skip to content

Commit

Permalink
Report data providers deprecated usage
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored and ondrejmirtes committed Dec 7, 2022
1 parent 4c06b7e commit f9bfc19
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.9.0"
"phpstan/phpstan": "^1.9.3"
},
"conflict": {
"phpunit/phpunit": "<7.0"
Expand Down
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
arguments:
checkFunctionNameCase: %checkFunctionNameCase%
deprecationRulesInstalled: %deprecationRulesInstalled%
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule

Expand Down
18 changes: 16 additions & 2 deletions src/Rules/PHPUnit/DataProviderDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,24 @@ class DataProviderDeclarationRule implements Rule
*/
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 $checkFunctionNameCase,
bool $deprecationRulesInstalled
)
{
$this->dataProviderHelper = $dataProviderHelper;
$this->fileTypeMapper = $fileTypeMapper;
$this->checkFunctionNameCase = $checkFunctionNameCase;
$this->deprecationRulesInstalled = $deprecationRulesInstalled;
}

public function getNodeType(): string
Expand Down Expand Up @@ -80,7 +89,12 @@ public function processNode(Node $node, Scope $scope): array
foreach ($annotations as $annotation) {
$errors = array_merge(
$errors,
$this->dataProviderHelper->processDataProvider($scope, $annotation, $this->checkFunctionNameCase)
$this->dataProviderHelper->processDataProvider(
$scope,
$annotation,
$this->checkFunctionNameCase,
$this->deprecationRulesInstalled
)
);
}

Expand Down
10 changes: 9 additions & 1 deletion src/Rules/PHPUnit/DataProviderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array
public function processDataProvider(
Scope $scope,
PhpDocTagNode $phpDocTag,
bool $checkFunctionNameCase
bool $checkFunctionNameCase,
bool $deprecationRulesInstalled
): array
{
$dataProviderName = $this->getDataProviderName($phpDocTag);
Expand Down Expand Up @@ -87,6 +88,13 @@ public function processDataProvider(
))->build();
}

if ($deprecationRulesInstalled && !$dataProviderMethodReflection->isStatic()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method must be static.',
$dataProviderName
))->build();
}

return $errors;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected function getRule(): Rule
return new DataProviderDeclarationRule(
new DataProviderHelper(),
self::getContainer()->getByType(FileTypeMapper::class),
true,
true
);
}
Expand All @@ -28,6 +29,10 @@ public function testRule(): void
'@dataProvider providebaz related method is used with incorrect case: provideBaz.',
13,
],
[
'@dataProvider provideQux related method must be static.',
13,
],
[
'@dataProvider provideQuux related method must be public.',
13,
Expand Down

0 comments on commit f9bfc19

Please sign in to comment.