Skip to content

Commit c206996

Browse files
committed
Add rule to check @dataProvider
1 parent b906029 commit c206996

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

Diff for: rules.neon

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ rules:
88
services:
99
- class: PHPStan\Rules\PHPUnit\ClassCoversExistsRule
1010
- class: PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule
11-
- class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
11+
-
12+
class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
13+
arguments:
14+
checkFunctionNameCase: %checkFunctionNameCase%
1215
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule
1316
- class: PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule
1417

Diff for: src/Rules/PHPUnit/DataProviderDeclarationRule.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,22 @@ class DataProviderDeclarationRule implements Rule
2929
*/
3030
private $fileTypeMapper;
3131

32+
/**
33+
* When set to true, it reports data provider method with incorrect name case.
34+
*
35+
* @var bool
36+
*/
37+
private $checkFunctionNameCase;
38+
3239
public function __construct(
3340
DataProviderHelper $dataProviderHelper,
34-
FileTypeMapper $fileTypeMapper
41+
FileTypeMapper $fileTypeMapper,
42+
bool $checkFunctionNameCase
3543
)
3644
{
3745
$this->dataProviderHelper = $dataProviderHelper;
3846
$this->fileTypeMapper = $fileTypeMapper;
47+
$this->checkFunctionNameCase = $checkFunctionNameCase;
3948
}
4049

4150
public function getNodeType(): string
@@ -71,7 +80,7 @@ public function processNode(Node $node, Scope $scope): array
7180
foreach ($annotations as $annotation) {
7281
$errors = array_merge(
7382
$errors,
74-
$this->dataProviderHelper->processDataProvider($scope, $annotation)
83+
$this->dataProviderHelper->processDataProvider($scope, $annotation, $this->checkFunctionNameCase)
7584
);
7685
}
7786

Diff for: src/Rules/PHPUnit/DataProviderHelper.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use function array_merge;
1212
use function preg_match;
1313
use function sprintf;
14-
use function trim;
1514

1615
class DataProviderHelper
1716
{
@@ -44,7 +43,8 @@ public function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array
4443
*/
4544
public function processDataProvider(
4645
Scope $scope,
47-
PhpDocTagNode $phpDocTag
46+
PhpDocTagNode $phpDocTag,
47+
bool $checkFunctionNameCase
4848
): array
4949
{
5050
$dataProviderName = $this->getDataProviderName($phpDocTag);
@@ -72,7 +72,7 @@ public function processDataProvider(
7272

7373
$errors = [];
7474

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

90-
if (!$dataProviderMethodReflection->isStatic()) {
91-
$errors[] = RuleErrorBuilder::message(sprintf(
92-
'@dataProvider %s related method must be static.',
93-
$dataProviderName
94-
))->build();
95-
}
96-
9790
return $errors;
9891
}
9992

10093
private function getDataProviderName(PhpDocTagNode $phpDocTag): ?string
10194
{
102-
$value = trim((string) $phpDocTag->value);
103-
104-
if (preg_match('/^[\S]+/', $value, $matches) !== 1) {
95+
if (preg_match('/^[^ \t]+/', (string) $phpDocTag->value, $matches) !== 1) {
10596
return null;
10697
}
10798

Diff for: tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ protected function getRule(): Rule
1616
{
1717
return new DataProviderDeclarationRule(
1818
new DataProviderHelper(),
19-
self::getContainer()->getByType(FileTypeMapper::class)
19+
self::getContainer()->getByType(FileTypeMapper::class),
20+
true
2021
);
2122
}
2223

@@ -27,10 +28,6 @@ public function testRule(): void
2728
'@dataProvider providebaz related method is used with incorrect case: provideBaz.',
2829
13,
2930
],
30-
[
31-
'@dataProvider provideQux related method must be static.',
32-
13,
33-
],
3431
[
3532
'@dataProvider provideQuux related method must be public.',
3633
13,

0 commit comments

Comments
 (0)