Skip to content

Commit 8a46ab1

Browse files
committed
Make phpunit.dataProviderPublic auto-fixable
1 parent a3436e6 commit 8a46ab1

4 files changed

Lines changed: 113 additions & 23 deletions

File tree

‎src/Rules/PHPUnit/DataProviderHelper.php‎

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
1919
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
2020
use PHPStan\Reflection\ClassReflection;
21+
use PHPStan\Reflection\ExtendedMethodReflection;
2122
use PHPStan\Reflection\MissingMethodFromReflectionException;
2223
use PHPStan\Reflection\ReflectionProvider;
2324
use PHPStan\Rules\IdentifierRuleError;
@@ -149,13 +150,24 @@ public function processDataProvider(
149150
}
150151

151152
if (!$dataProviderMethodReflection->isPublic()) {
152-
$errors[] = RuleErrorBuilder::message(sprintf(
153+
$errorBuilder = RuleErrorBuilder::message(sprintf(
153154
'@dataProvider %s related method must be public.',
154155
$dataProviderValue,
155156
))
156157
->line($lineNumber)
157-
->identifier('phpunit.dataProviderPublic')
158-
->build();
158+
->identifier('phpunit.dataProviderPublic');
159+
160+
$methodNodeAndFile = $this->findMethodNode($dataProviderMethodReflection);
161+
if ($methodNodeAndFile !== null) {
162+
[$methodNode, $fileName] = $methodNodeAndFile;
163+
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
164+
$methodNode->flags &= ~Modifiers::VISIBILITY_MASK;
165+
$methodNode->flags |= Modifiers::PUBLIC;
166+
167+
return $methodNode;
168+
}, $fileName);
169+
}
170+
$errors[] = $errorBuilder->build();
159171
}
160172

161173
if (
@@ -170,23 +182,14 @@ public function processDataProvider(
170182
->line($lineNumber)
171183
->identifier('phpunit.dataProviderStatic');
172184

173-
// a method from a trait is declared in the trait, which can be in another file
174-
$nativeMethod = $dataProviderMethodReflection->getDeclaringClass()->getNativeReflection()->getMethod($dataProviderMethodReflection->getName())->getBetterReflection();
175-
$declaringClassFileName = $nativeMethod->getFileName();
176-
if ($declaringClassFileName !== null) {
177-
$stmts = $this->parser->parseFile($declaringClassFileName);
178-
$methodNode = $this->findMethodNode(
179-
$stmts,
180-
$nativeMethod->getDeclaringClass()->getName(),
181-
$dataProviderMethodReflection->getName(),
182-
);
183-
if ($methodNode !== null) {
184-
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
185-
$methodNode->flags |= Modifiers::STATIC;
186-
187-
return $methodNode;
188-
}, $declaringClassFileName);
189-
}
185+
$methodNodeAndFile = $this->findMethodNode($dataProviderMethodReflection);
186+
if ($methodNodeAndFile !== null) {
187+
[$methodNode, $fileName] = $methodNodeAndFile;
188+
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
189+
$methodNode->flags |= Modifiers::STATIC;
190+
191+
return $methodNode;
192+
}, $fileName);
190193
}
191194
$errors[] = $errorBuilder->build();
192195
}
@@ -195,10 +198,19 @@ public function processDataProvider(
195198
}
196199

197200
/**
198-
* @param Node[] $stmts
201+
* @return array{ClassMethod, string}|null
199202
*/
200-
private function findMethodNode(array $stmts, string $className, string $methodName): ?ClassMethod
203+
private function findMethodNode(ExtendedMethodReflection $methodReflection): ?array
201204
{
205+
// a method from a trait is declared in the trait, which can be in another file
206+
$nativeMethod = $methodReflection->getDeclaringClass()->getNativeReflection()->getMethod($methodReflection->getName())->getBetterReflection();
207+
$fileName = $nativeMethod->getFileName();
208+
if ($fileName === null) {
209+
return null;
210+
}
211+
212+
$stmts = $this->parser->parseFile($fileName);
213+
$className = $nativeMethod->getDeclaringClass()->getName();
202214
$nodeFinder = new NodeFinder();
203215

204216
/** @var ClassLike|null $classNode */
@@ -212,7 +224,12 @@ private function findMethodNode(array $stmts, string $className, string $methodN
212224
return null;
213225
}
214226

215-
return $classNode->getMethod($methodName);
227+
$methodNode = $classNode->getMethod($methodReflection->getName());
228+
if ($methodNode === null) {
229+
return null;
230+
}
231+
232+
return [$methodNode, $fileName];
216233
}
217234

218235
private function getDataProviderAnnotationValue(PhpDocTagNode $phpDocTag): ?string

‎tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ public function testFixDataProviderStaticDeclaredInTraitInOtherFile(): void
161161
);
162162
}
163163

164+
public function testFixDataProviderPublic(): void
165+
{
166+
$this->phpunitVersion = 10;
167+
168+
$this->fix(__DIR__ . '/data/data-provider-public-fix.php', __DIR__ . '/data/data-provider-public-fix.php.fixed');
169+
}
170+
164171
/**
165172
* @return string[]
166173
*/
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)