Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions src/Rules/PHPUnit/DataProviderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\IdentifierRuleError;
Expand Down Expand Up @@ -149,13 +150,24 @@ public function processDataProvider(
}

if (!$dataProviderMethodReflection->isPublic()) {
$errors[] = RuleErrorBuilder::message(sprintf(
$errorBuilder = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method must be public.',
$dataProviderValue,
))
->line($lineNumber)
->identifier('phpunit.dataProviderPublic')
->build();
->identifier('phpunit.dataProviderPublic');

$methodNodeAndFile = $this->findMethodNode($dataProviderMethodReflection);
if ($methodNodeAndFile !== null) {
[$methodNode, $fileName] = $methodNodeAndFile;
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
$methodNode->flags &= ~Modifiers::VISIBILITY_MASK;
$methodNode->flags |= Modifiers::PUBLIC;

return $methodNode;
}, $fileName);
}
$errors[] = $errorBuilder->build();
}

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

// a method from a trait is declared in the trait, which can be in another file
$nativeMethod = $dataProviderMethodReflection->getDeclaringClass()->getNativeReflection()->getMethod($dataProviderMethodReflection->getName())->getBetterReflection();
$declaringClassFileName = $nativeMethod->getFileName();
if ($declaringClassFileName !== null) {
$stmts = $this->parser->parseFile($declaringClassFileName);
$methodNode = $this->findMethodNode(
$stmts,
$nativeMethod->getDeclaringClass()->getName(),
$dataProviderMethodReflection->getName(),
);
if ($methodNode !== null) {
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
$methodNode->flags |= Modifiers::STATIC;

return $methodNode;
}, $declaringClassFileName);
}
$methodNodeAndFile = $this->findMethodNode($dataProviderMethodReflection);
if ($methodNodeAndFile !== null) {
[$methodNode, $fileName] = $methodNodeAndFile;
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
$methodNode->flags |= Modifiers::STATIC;

return $methodNode;
}, $fileName);
}
$errors[] = $errorBuilder->build();
}
Expand All @@ -195,10 +198,19 @@ public function processDataProvider(
}

/**
* @param Node[] $stmts
* @return array{ClassMethod, string}|null
*/
private function findMethodNode(array $stmts, string $className, string $methodName): ?ClassMethod
private function findMethodNode(ExtendedMethodReflection $methodReflection): ?array
{
// a method from a trait is declared in the trait, which can be in another file
$nativeMethod = $methodReflection->getDeclaringClass()->getNativeReflection()->getMethod($methodReflection->getName())->getBetterReflection();
$fileName = $nativeMethod->getFileName();
if ($fileName === null) {
return null;
}

$stmts = $this->parser->parseFile($fileName);
$className = $nativeMethod->getDeclaringClass()->getName();
$nodeFinder = new NodeFinder();

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

return $classNode->getMethod($methodName);
$methodNode = $classNode->getMethod($methodReflection->getName());
if ($methodNode === null) {
return null;
}

return [$methodNode, $fileName];
}

private function getDataProviderAnnotationValue(PhpDocTagNode $phpDocTag): ?string
Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ public function testFixDataProviderStaticDeclaredInTraitInOtherFile(): void
);
}

public function testFixDataProviderPublic(): void
{
$this->phpunitVersion = 10;

$this->fix(__DIR__ . '/data/data-provider-public-fix.php', __DIR__ . '/data/data-provider-public-fix.php.fixed');
}

/**
* @return string[]
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Rules/PHPUnit/data/data-provider-public-fix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace DataProviderPublicFix;

class FixTestCase extends \PHPUnit\Framework\TestCase
{

private static function dataPrivate(): iterable
{
yield [];
}

protected static function dataProtected(): iterable
{
yield [];
}

protected function dataProtectedNonStatic(): iterable
{
yield [];
}

/**
* @dataProvider dataPrivate
* @dataProvider dataProtected
* @dataProvider dataProtectedNonStatic
*/
public function testFoo(): void
{

}

}
33 changes: 33 additions & 0 deletions tests/Rules/PHPUnit/data/data-provider-public-fix.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace DataProviderPublicFix;

class FixTestCase extends \PHPUnit\Framework\TestCase
{

public static function dataPrivate(): iterable
{
yield [];
}

public static function dataProtected(): iterable
{
yield [];
}

public static function dataProtectedNonStatic(): iterable
{
yield [];
}

/**
* @dataProvider dataPrivate
* @dataProvider dataProtected
* @dataProvider dataProtectedNonStatic
*/
public function testFoo(): void
{

}

}