Skip to content

Commit

Permalink
Add rule to check @dataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed Dec 7, 2022
1 parent b906029 commit c206996
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
5 changes: 4 additions & 1 deletion rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ rules:
services:
- class: PHPStan\Rules\PHPUnit\ClassCoversExistsRule
- class: PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule
- class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
-
class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
arguments:
checkFunctionNameCase: %checkFunctionNameCase%
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule

Expand Down
13 changes: 11 additions & 2 deletions src/Rules/PHPUnit/DataProviderDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ class DataProviderDeclarationRule implements Rule
*/
private $fileTypeMapper;

/**
* When set to true, it reports data provider method with incorrect name case.
*
* @var bool
*/
private $checkFunctionNameCase;

public function __construct(
DataProviderHelper $dataProviderHelper,
FileTypeMapper $fileTypeMapper
FileTypeMapper $fileTypeMapper,
bool $checkFunctionNameCase
)
{
$this->dataProviderHelper = $dataProviderHelper;
$this->fileTypeMapper = $fileTypeMapper;
$this->checkFunctionNameCase = $checkFunctionNameCase;
}

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

Expand Down
17 changes: 4 additions & 13 deletions src/Rules/PHPUnit/DataProviderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use function array_merge;
use function preg_match;
use function sprintf;
use function trim;

class DataProviderHelper
{
Expand Down Expand Up @@ -44,7 +43,8 @@ public function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array
*/
public function processDataProvider(
Scope $scope,
PhpDocTagNode $phpDocTag
PhpDocTagNode $phpDocTag,
bool $checkFunctionNameCase
): array
{
$dataProviderName = $this->getDataProviderName($phpDocTag);
Expand Down Expand Up @@ -72,7 +72,7 @@ public function processDataProvider(

$errors = [];

if ($dataProviderName !== $dataProviderMethodReflection->getName()) {
if ($checkFunctionNameCase && $dataProviderName !== $dataProviderMethodReflection->getName()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method is used with incorrect case: %s.',
$dataProviderName,
Expand All @@ -87,21 +87,12 @@ public function processDataProvider(
))->build();
}

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

return $errors;
}

private function getDataProviderName(PhpDocTagNode $phpDocTag): ?string
{
$value = trim((string) $phpDocTag->value);

if (preg_match('/^[\S]+/', $value, $matches) !== 1) {
if (preg_match('/^[^ \t]+/', (string) $phpDocTag->value, $matches) !== 1) {
return null;
}

Expand Down
7 changes: 2 additions & 5 deletions tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ protected function getRule(): Rule
{
return new DataProviderDeclarationRule(
new DataProviderHelper(),
self::getContainer()->getByType(FileTypeMapper::class)
self::getContainer()->getByType(FileTypeMapper::class),
true
);
}

Expand All @@ -27,10 +28,6 @@ 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 c206996

Please sign in to comment.