diff --git a/composer.json b/composer.json index a7ec427..f3ce0e7 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/rules.neon b/rules.neon index 195ace0..8dc7056 100644 --- a/rules.neon +++ b/rules.neon @@ -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 diff --git a/src/Rules/PHPUnit/DataProviderDeclarationRule.php b/src/Rules/PHPUnit/DataProviderDeclarationRule.php index 7d1afd6..612cf06 100644 --- a/src/Rules/PHPUnit/DataProviderDeclarationRule.php +++ b/src/Rules/PHPUnit/DataProviderDeclarationRule.php @@ -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 @@ -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 + ) ); } diff --git a/src/Rules/PHPUnit/DataProviderHelper.php b/src/Rules/PHPUnit/DataProviderHelper.php index ef3bfcd..e88f293 100644 --- a/src/Rules/PHPUnit/DataProviderHelper.php +++ b/src/Rules/PHPUnit/DataProviderHelper.php @@ -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); @@ -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; } diff --git a/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php b/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php index 44434e3..cfd27ae 100644 --- a/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php +++ b/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php @@ -17,6 +17,7 @@ protected function getRule(): Rule return new DataProviderDeclarationRule( new DataProviderHelper(), self::getContainer()->getByType(FileTypeMapper::class), + true, true ); } @@ -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,