Skip to content

Commit 758d343

Browse files
authored
Refactor PHPUnitVersionDetector to ease different major version checks
1 parent dd87f2e commit 758d343

File tree

9 files changed

+159
-99
lines changed

9 files changed

+159
-99
lines changed

extension.neon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ services:
5353
class: PHPStan\Rules\PHPUnit\AnnotationHelper
5454

5555
-
56-
class: PHPStan\Rules\PHPUnit\PHPUnitVersionDetector
56+
class: PHPStan\Rules\PHPUnit\TestMethodsHelper
5757

5858
-
59-
class: PHPStan\Rules\PHPUnit\TestMethodsHelper
60-
factory: @PHPStan\Rules\PHPUnit\TestMethodsHelperFactory::create()
59+
class: PHPStan\Rules\PHPUnit\PHPUnitVersion
60+
factory: @PHPStan\Rules\PHPUnit\PHPUnitVersionDetector::createPHPUnitVersion()
6161
-
62-
class: PHPStan\Rules\PHPUnit\TestMethodsHelperFactory
62+
class: PHPStan\Rules\PHPUnit\PHPUnitVersionDetector
6363

6464
-
6565
class: PHPStan\Rules\PHPUnit\DataProviderHelper

src/Rules/PHPUnit/DataProviderHelper.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ class DataProviderHelper
3737

3838
private Parser $parser;
3939

40-
private bool $phpunit10OrNewer;
40+
private PHPUnitVersion $PHPUnitVersion;
4141

4242
public function __construct(
4343
ReflectionProvider $reflectionProvider,
4444
FileTypeMapper $fileTypeMapper,
4545
Parser $parser,
46-
bool $phpunit10OrNewer
46+
PHPUnitVersion $PHPUnitVersion
4747
)
4848
{
4949
$this->reflectionProvider = $reflectionProvider;
5050
$this->fileTypeMapper = $fileTypeMapper;
5151
$this->parser = $parser;
52-
$this->phpunit10OrNewer = $phpunit10OrNewer;
52+
$this->PHPUnitVersion = $PHPUnitVersion;
5353
}
5454

5555
/**
@@ -65,7 +65,7 @@ public function getDataProviderMethods(
6565
{
6666
yield from $this->yieldDataProviderAnnotations($testMethod, $scope, $classReflection);
6767

68-
if (!$this->phpunit10OrNewer) {
68+
if (!$this->PHPUnitVersion->supportsDataProviderAttribute()->yes()) {
6969
return;
7070
}
7171

@@ -156,7 +156,11 @@ public function processDataProvider(
156156
->build();
157157
}
158158

159-
if ($deprecationRulesInstalled && $this->phpunit10OrNewer && !$dataProviderMethodReflection->isStatic()) {
159+
if (
160+
$deprecationRulesInstalled
161+
&& $this->PHPUnitVersion->requiresStaticDataProviders()->yes()
162+
&& !$dataProviderMethodReflection->isStatic()
163+
) {
160164
$errorBuilder = RuleErrorBuilder::message(sprintf(
161165
'@dataProvider %s related method must be static in PHPUnit 10 and newer.',
162166
$dataProviderValue,

src/Rules/PHPUnit/DataProviderHelperFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@ class DataProviderHelperFactory
1515

1616
private Parser $parser;
1717

18-
private PHPUnitVersionDetector $PHPUnitVersionDetector;
18+
private PHPUnitVersion $PHPUnitVersion;
1919

2020
public function __construct(
2121
ReflectionProvider $reflectionProvider,
2222
FileTypeMapper $fileTypeMapper,
2323
Parser $parser,
24-
PHPUnitVersionDetector $PHPUnitVersionDetector
24+
PHPUnitVersion $PHPUnitVersion
2525
)
2626
{
2727
$this->reflectionProvider = $reflectionProvider;
2828
$this->fileTypeMapper = $fileTypeMapper;
2929
$this->parser = $parser;
30-
$this->PHPUnitVersionDetector = $PHPUnitVersionDetector;
30+
$this->PHPUnitVersion = $PHPUnitVersion;
3131
}
3232

3333
public function create(): DataProviderHelper
3434
{
35-
return new DataProviderHelper($this->reflectionProvider, $this->fileTypeMapper, $this->parser, $this->PHPUnitVersionDetector->isPHPUnit10OrNewer());
35+
return new DataProviderHelper($this->reflectionProvider, $this->fileTypeMapper, $this->parser, $this->PHPUnitVersion);
3636
}
3737

3838
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PHPStan\TrinaryLogic;
6+
7+
class PHPUnitVersion
8+
{
9+
10+
private ?int $majorVersion;
11+
12+
public function __construct(?int $majorVersion)
13+
{
14+
$this->majorVersion = $majorVersion;
15+
}
16+
17+
public function supportsDataProviderAttribute(): TrinaryLogic
18+
{
19+
if ($this->majorVersion === null) {
20+
return TrinaryLogic::createMaybe();
21+
}
22+
return TrinaryLogic::createFromBoolean($this->majorVersion >= 10);
23+
}
24+
25+
public function supportsTestAttribute(): TrinaryLogic
26+
{
27+
if ($this->majorVersion === null) {
28+
return TrinaryLogic::createMaybe();
29+
}
30+
return TrinaryLogic::createFromBoolean($this->majorVersion >= 10);
31+
}
32+
33+
public function requiresStaticDataProviders(): TrinaryLogic
34+
{
35+
if ($this->majorVersion === null) {
36+
return TrinaryLogic::createMaybe();
37+
}
38+
return TrinaryLogic::createFromBoolean($this->majorVersion >= 10);
39+
}
40+
41+
}

src/Rules/PHPUnit/PHPUnitVersionDetector.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,16 @@
1313
class PHPUnitVersionDetector
1414
{
1515

16-
private ?bool $is10OrNewer = null;
17-
1816
private ReflectionProvider $reflectionProvider;
1917

2018
public function __construct(ReflectionProvider $reflectionProvider)
2119
{
2220
$this->reflectionProvider = $reflectionProvider;
2321
}
2422

25-
public function isPHPUnit10OrNewer(): bool
23+
public function createPHPUnitVersion(): PHPUnitVersion
2624
{
27-
if ($this->is10OrNewer !== null) {
28-
return $this->is10OrNewer;
29-
}
30-
31-
$this->is10OrNewer = false;
25+
$majorVersion = null;
3226
if ($this->reflectionProvider->hasClass(TestCase::class)) {
3327
$testCase = $this->reflectionProvider->getClass(TestCase::class);
3428
$file = $testCase->getFileName();
@@ -42,16 +36,13 @@ public function isPHPUnit10OrNewer(): bool
4236
$version = $json['extra']['branch-alias']['dev-main'] ?? null;
4337
if ($version !== null) {
4438
$majorVersion = (int) explode('.', $version)[0];
45-
if ($majorVersion >= 10) {
46-
$this->is10OrNewer = true;
47-
}
4839
}
4940
}
5041
}
5142
}
5243
}
5344

54-
return $this->is10OrNewer;
45+
return new PHPUnitVersion($majorVersion);
5546
}
5647

5748
}

src/Rules/PHPUnit/TestMethodsHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ final class TestMethodsHelper
1616

1717
private FileTypeMapper $fileTypeMapper;
1818

19-
private bool $phpunit10OrNewer;
19+
private PHPUnitVersion $PHPUnitVersion;
2020

2121
public function __construct(
2222
FileTypeMapper $fileTypeMapper,
23-
bool $phpunit10OrNewer
23+
PHPUnitVersion $PHPUnitVersion
2424
)
2525
{
2626
$this->fileTypeMapper = $fileTypeMapper;
27-
$this->phpunit10OrNewer = $phpunit10OrNewer;
27+
$this->PHPUnitVersion = $PHPUnitVersion;
2828
}
2929

3030
/**
@@ -63,7 +63,7 @@ public function getTestMethods(ClassReflection $classReflection, Scope $scope):
6363
}
6464
}
6565

66-
if (!$this->phpunit10OrNewer) {
66+
if ($this->PHPUnitVersion->supportsTestAttribute()->no()) {
6767
continue;
6868
}
6969

src/Rules/PHPUnit/TestMethodsHelperFactory.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
class DataProviderDataRuleTest extends RuleTestCase
1616
{
17+
private int $phpunitVersion;
1718

1819
protected function getRule(): Rule
1920
{
@@ -24,13 +25,13 @@ protected function getRule(): Rule
2425
new DataProviderDataRule(
2526
new TestMethodsHelper(
2627
self::getContainer()->getByType(FileTypeMapper::class),
27-
true
28+
new PHPUnitVersion($this->phpunitVersion)
2829
),
2930
new DataProviderHelper(
3031
$reflectionProvider,
3132
self::getContainer()->getByType(FileTypeMapper::class),
3233
self::getContainer()->getService('defaultAnalysisParser'),
33-
true
34+
new PHPUnitVersion($this->phpunitVersion)
3435
),
3536

3637
),
@@ -42,6 +43,8 @@ protected function getRule(): Rule
4243

4344
public function testRule(): void
4445
{
46+
$this->phpunitVersion = 10;
47+
4548
$this->analyse([__DIR__ . '/data/data-provider-data.php'], [
4649
[
4750
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, int given.',
@@ -176,6 +179,8 @@ public function testRulePhp8(): void
176179
self::markTestSkipped();
177180
}
178181

182+
$this->phpunitVersion = 10;
183+
179184
$this->analyse([__DIR__ . '/data/data-provider-data-named.php'], [
180185
[
181186
'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.',
@@ -203,6 +208,8 @@ public function testRulePhp8(): void
203208

204209
public function testVariadicMethod(): void
205210
{
211+
$this->phpunitVersion = 10;
212+
206213
$this->analyse([__DIR__ . '/data/data-provider-variadic-method.php'], [
207214
[
208215
'Method DataProviderVariadicMethod\FooTest::testProvide2() invoked with 1 parameter, at least 2 required.',
@@ -241,6 +248,8 @@ public function testVariadicMethod(): void
241248

242249
public function testTrimmingArgs(): void
243250
{
251+
$this->phpunitVersion = 10;
252+
244253
$this->analyse([__DIR__ . '/data/data-provider-trimming-args.php'], [
245254
[
246255
'Method DataProviderTrimmingArgs\FooTest::testProvide() invoked with 2 parameters, 1 required.',

0 commit comments

Comments
 (0)