Skip to content

Commit

Permalink
DataProviderDeclarationRule - report non-static dataProvider only wit…
Browse files Browse the repository at this point in the history
…h PHPUnit 10+
  • Loading branch information
ondrejmirtes committed Dec 13, 2022
1 parent b9827cf commit cd9c693
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
3 changes: 3 additions & 0 deletions extension.neon
Expand Up @@ -57,6 +57,9 @@ services:
class: PHPStan\Rules\PHPUnit\AnnotationHelper
-
class: PHPStan\Rules\PHPUnit\DataProviderHelper
factory: @PHPStan\Rules\PHPUnit\DataProviderHelperFactory::create()
-
class: PHPStan\Rules\PHPUnit\DataProviderHelperFactory

conditionalTags:
PHPStan\PhpDoc\PHPUnit\MockObjectTypeNodeResolverExtension:
Expand Down
10 changes: 7 additions & 3 deletions src/Rules/PHPUnit/DataProviderHelper.php
Expand Up @@ -26,9 +26,13 @@ class DataProviderHelper
*/
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
/** @var bool */
private $phpunit10OrNewer;

public function __construct(ReflectionProvider $reflectionProvider, bool $phpunit10OrNewer)
{
$this->reflectionProvider = $reflectionProvider;
$this->phpunit10OrNewer = $phpunit10OrNewer;
}

/**
Expand Down Expand Up @@ -108,9 +112,9 @@ public function processDataProvider(
))->build();
}

if ($deprecationRulesInstalled && !$dataProviderMethodReflection->isStatic()) {
if ($deprecationRulesInstalled && $this->phpunit10OrNewer && !$dataProviderMethodReflection->isStatic()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method must be static.',
'@dataProvider %s related method must be static in PHPUnit 10 and newer.',
$dataProviderValue
))->build();
}
Expand Down
52 changes: 52 additions & 0 deletions src/Rules/PHPUnit/DataProviderHelperFactory.php
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PHPStan\Reflection\ReflectionProvider;
use PHPUnit\Framework\TestCase;
use function dirname;
use function explode;
use function file_get_contents;
use function is_file;
use function json_decode;

class DataProviderHelperFactory
{

/** @var ReflectionProvider */
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

public function create(): DataProviderHelper
{
$phpUnit10OrNewer = false;
if ($this->reflectionProvider->hasClass(TestCase::class)) {
$testCase = $this->reflectionProvider->getClass(TestCase::class);
$file = $testCase->getFileName();
if ($file !== null) {
$phpUnitRoot = dirname($file, 3);
$phpUnitComposer = $phpUnitRoot . '/composer.json';
if (is_file($phpUnitComposer)) {
$composerJson = @file_get_contents($phpUnitComposer);
if ($composerJson !== false) {
$json = json_decode($composerJson, true);
$version = $json['extra']['branch-alias']['dev-main'] ?? null;
if ($version !== null) {
$majorVersion = (int) explode('.', $version)[0];
if ($majorVersion >= 10) {
$phpUnit10OrNewer = true;
}
}
}
}
}
}

return new DataProviderHelper($this->reflectionProvider, $phpUnit10OrNewer);
}

}
4 changes: 2 additions & 2 deletions tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php
Expand Up @@ -17,7 +17,7 @@ protected function getRule(): Rule
$reflection = $this->createReflectionProvider();

return new DataProviderDeclarationRule(
new DataProviderHelper($reflection),
new DataProviderHelper($reflection, true),
self::getContainer()->getByType(FileTypeMapper::class),
true,
true
Expand All @@ -32,7 +32,7 @@ public function testRule(): void
14,
],
[
'@dataProvider provideQux related method must be static.',
'@dataProvider provideQux related method must be static in PHPUnit 10 and newer.',
14,
],
[
Expand Down

0 comments on commit cd9c693

Please sign in to comment.