Skip to content

Commit cd9c693

Browse files
committed
DataProviderDeclarationRule - report non-static dataProvider only with PHPUnit 10+
1 parent b9827cf commit cd9c693

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

extension.neon

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ services:
5757
class: PHPStan\Rules\PHPUnit\AnnotationHelper
5858
-
5959
class: PHPStan\Rules\PHPUnit\DataProviderHelper
60+
factory: @PHPStan\Rules\PHPUnit\DataProviderHelperFactory::create()
61+
-
62+
class: PHPStan\Rules\PHPUnit\DataProviderHelperFactory
6063

6164
conditionalTags:
6265
PHPStan\PhpDoc\PHPUnit\MockObjectTypeNodeResolverExtension:

src/Rules/PHPUnit/DataProviderHelper.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ class DataProviderHelper
2626
*/
2727
private $reflectionProvider;
2828

29-
public function __construct(ReflectionProvider $reflectionProvider)
29+
/** @var bool */
30+
private $phpunit10OrNewer;
31+
32+
public function __construct(ReflectionProvider $reflectionProvider, bool $phpunit10OrNewer)
3033
{
3134
$this->reflectionProvider = $reflectionProvider;
35+
$this->phpunit10OrNewer = $phpunit10OrNewer;
3236
}
3337

3438
/**
@@ -108,9 +112,9 @@ public function processDataProvider(
108112
))->build();
109113
}
110114

111-
if ($deprecationRulesInstalled && !$dataProviderMethodReflection->isStatic()) {
115+
if ($deprecationRulesInstalled && $this->phpunit10OrNewer && !$dataProviderMethodReflection->isStatic()) {
112116
$errors[] = RuleErrorBuilder::message(sprintf(
113-
'@dataProvider %s related method must be static.',
117+
'@dataProvider %s related method must be static in PHPUnit 10 and newer.',
114118
$dataProviderValue
115119
))->build();
116120
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PHPStan\Reflection\ReflectionProvider;
6+
use PHPUnit\Framework\TestCase;
7+
use function dirname;
8+
use function explode;
9+
use function file_get_contents;
10+
use function is_file;
11+
use function json_decode;
12+
13+
class DataProviderHelperFactory
14+
{
15+
16+
/** @var ReflectionProvider */
17+
private $reflectionProvider;
18+
19+
public function __construct(ReflectionProvider $reflectionProvider)
20+
{
21+
$this->reflectionProvider = $reflectionProvider;
22+
}
23+
24+
public function create(): DataProviderHelper
25+
{
26+
$phpUnit10OrNewer = false;
27+
if ($this->reflectionProvider->hasClass(TestCase::class)) {
28+
$testCase = $this->reflectionProvider->getClass(TestCase::class);
29+
$file = $testCase->getFileName();
30+
if ($file !== null) {
31+
$phpUnitRoot = dirname($file, 3);
32+
$phpUnitComposer = $phpUnitRoot . '/composer.json';
33+
if (is_file($phpUnitComposer)) {
34+
$composerJson = @file_get_contents($phpUnitComposer);
35+
if ($composerJson !== false) {
36+
$json = json_decode($composerJson, true);
37+
$version = $json['extra']['branch-alias']['dev-main'] ?? null;
38+
if ($version !== null) {
39+
$majorVersion = (int) explode('.', $version)[0];
40+
if ($majorVersion >= 10) {
41+
$phpUnit10OrNewer = true;
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
49+
return new DataProviderHelper($this->reflectionProvider, $phpUnit10OrNewer);
50+
}
51+
52+
}

tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected function getRule(): Rule
1717
$reflection = $this->createReflectionProvider();
1818

1919
return new DataProviderDeclarationRule(
20-
new DataProviderHelper($reflection),
20+
new DataProviderHelper($reflection, true),
2121
self::getContainer()->getByType(FileTypeMapper::class),
2222
true,
2323
true
@@ -32,7 +32,7 @@ public function testRule(): void
3232
14,
3333
],
3434
[
35-
'@dataProvider provideQux related method must be static.',
35+
'@dataProvider provideQux related method must be static in PHPUnit 10 and newer.',
3636
14,
3737
],
3838
[

0 commit comments

Comments
 (0)