Skip to content

Commit 50c5c7b

Browse files
committed
Make phpunit.dataProviderPublic auto-fixable
1 parent 8c16263 commit 50c5c7b

4 files changed

Lines changed: 111 additions & 16 deletions

File tree

‎src/Rules/PHPUnit/DataProviderHelper.php‎

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
1717
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
1818
use PHPStan\Reflection\ClassReflection;
19+
use PHPStan\Reflection\ExtendedMethodReflection;
1920
use PHPStan\Reflection\MissingMethodFromReflectionException;
2021
use PHPStan\Reflection\ReflectionProvider;
2122
use PHPStan\Rules\IdentifierRuleError;
@@ -147,13 +148,23 @@ public function processDataProvider(
147148
}
148149

149150
if (!$dataProviderMethodReflection->isPublic()) {
150-
$errors[] = RuleErrorBuilder::message(sprintf(
151+
$errorBuilder = RuleErrorBuilder::message(sprintf(
151152
'@dataProvider %s related method must be public.',
152153
$dataProviderValue,
153154
))
154155
->line($lineNumber)
155-
->identifier('phpunit.dataProviderPublic')
156-
->build();
156+
->identifier('phpunit.dataProviderPublic');
157+
158+
$methodNode = $this->findMethodNode($dataProviderMethodReflection);
159+
if ($methodNode !== null) {
160+
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
161+
$methodNode->flags &= ~Modifiers::VISIBILITY_MASK;
162+
$methodNode->flags |= Modifiers::PUBLIC;
163+
164+
return $methodNode;
165+
});
166+
}
167+
$errors[] = $errorBuilder->build();
157168
}
158169

159170
if (
@@ -168,26 +179,37 @@ public function processDataProvider(
168179
->line($lineNumber)
169180
->identifier('phpunit.dataProviderStatic');
170181

171-
$dataProviderMethodReflectionDeclaringClass = $dataProviderMethodReflection->getDeclaringClass();
172-
if ($dataProviderMethodReflectionDeclaringClass->getFileName() !== null) {
173-
$stmts = $this->parser->parseFile($dataProviderMethodReflectionDeclaringClass->getFileName());
174-
$nodeFinder = new NodeFinder();
175-
/** @var ClassMethod|null $methodNode */
176-
$methodNode = $nodeFinder->findFirst($stmts, static fn ($node) => $node instanceof ClassMethod && $node->name->toString() === $dataProviderMethodReflection->getName());
177-
if ($methodNode !== null) {
178-
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
179-
$methodNode->flags |= Modifiers::STATIC;
180-
181-
return $methodNode;
182-
});
183-
}
182+
$methodNode = $this->findMethodNode($dataProviderMethodReflection);
183+
if ($methodNode !== null) {
184+
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
185+
$methodNode->flags |= Modifiers::STATIC;
186+
187+
return $methodNode;
188+
});
184189
}
185190
$errors[] = $errorBuilder->build();
186191
}
187192

188193
return $errors;
189194
}
190195

196+
private function findMethodNode(ExtendedMethodReflection $methodReflection): ?ClassMethod
197+
{
198+
$fileName = $methodReflection->getDeclaringClass()->getFileName();
199+
if ($fileName === null) {
200+
return null;
201+
}
202+
203+
$stmts = $this->parser->parseFile($fileName);
204+
$nodeFinder = new NodeFinder();
205+
$methodName = $methodReflection->getName();
206+
207+
/** @var ClassMethod|null $methodNode */
208+
$methodNode = $nodeFinder->findFirst($stmts, static fn ($node) => $node instanceof ClassMethod && $node->name->toString() === $methodName);
209+
210+
return $methodNode;
211+
}
212+
191213
private function getDataProviderAnnotationValue(PhpDocTagNode $phpDocTag): ?string
192214
{
193215
if (preg_match('/^[^ \t]+/', (string) $phpDocTag->value, $matches) !== 1) {

‎tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ public function testFixDataProviderStatic(): void
118118
$this->fix(__DIR__ . '/data/data-provider-static-fix.php', __DIR__ . '/data/data-provider-static-fix.php.fixed');
119119
}
120120

121+
public function testFixDataProviderPublic(): void
122+
{
123+
$this->phpunitVersion = 10;
124+
125+
$this->fix(__DIR__ . '/data/data-provider-public-fix.php', __DIR__ . '/data/data-provider-public-fix.php.fixed');
126+
}
127+
121128
/**
122129
* @return string[]
123130
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderPublicFix;
4+
5+
class FixTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
private static function dataPrivate(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
protected static function dataProtected(): iterable
14+
{
15+
yield [];
16+
}
17+
18+
protected function dataProtectedNonStatic(): iterable
19+
{
20+
yield [];
21+
}
22+
23+
/**
24+
* @dataProvider dataPrivate
25+
* @dataProvider dataProtected
26+
* @dataProvider dataProtectedNonStatic
27+
*/
28+
public function testFoo(): void
29+
{
30+
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderPublicFix;
4+
5+
class FixTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public static function dataPrivate(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
public static function dataProtected(): iterable
14+
{
15+
yield [];
16+
}
17+
18+
public static function dataProtectedNonStatic(): iterable
19+
{
20+
yield [];
21+
}
22+
23+
/**
24+
* @dataProvider dataPrivate
25+
* @dataProvider dataProtected
26+
* @dataProvider dataProtectedNonStatic
27+
*/
28+
public function testFoo(): void
29+
{
30+
31+
}
32+
33+
}

0 commit comments

Comments
 (0)