Skip to content

Commit ef578d0

Browse files
committed
refactor
1 parent bc1b787 commit ef578d0

File tree

8 files changed

+52
-29
lines changed

8 files changed

+52
-29
lines changed

src/Rules/PHPUnit/DataProviderHelper.php

Lines changed: 5 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()) {
6969
return;
7070
}
7171

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

159-
if ($deprecationRulesInstalled && $this->phpunit10OrNewer && !$dataProviderMethodReflection->isStatic()) {
159+
if ($deprecationRulesInstalled && $this->PHPUnitVersion->requiresStaticDataProviders() && !$dataProviderMethodReflection->isStatic()) {
160160
$errorBuilder = RuleErrorBuilder::message(sprintf(
161161
'@dataProvider %s related method must be static in PHPUnit 10 and newer.',
162162
$dataProviderValue,

src/Rules/PHPUnit/DataProviderHelperFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(
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->PHPUnitVersionDetector->getPHPUnitVersion());
3636
}
3737

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

src/Rules/PHPUnit/PHPUnitVersionDetector.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
class PHPUnitVersionDetector
1414
{
1515

16-
private bool $initialized = false;
17-
18-
private ?int $majorVersion = null;
16+
private ?PHPUnitVersion $PHPUnitVersion = null;
1917

2018
private ReflectionProvider $reflectionProvider;
2119

@@ -24,19 +22,13 @@ public function __construct(ReflectionProvider $reflectionProvider)
2422
$this->reflectionProvider = $reflectionProvider;
2523
}
2624

27-
public function isPHPUnit10OrNewer(): bool
28-
{
29-
$majorVersion = $this->getMajorVersion();
30-
return $majorVersion !== null && $majorVersion >= 10;
31-
}
32-
33-
private function getMajorVersion(): ?int
25+
public function getPHPUnitVersion(): PHPUnitVersion
3426
{
35-
if ($this->initialized) {
36-
return $this->majorVersion;
27+
if ($this->PHPUnitVersion !== null) {
28+
return $this->PHPUnitVersion;
3729
}
38-
$this->initialized = true;
3930

31+
$majorVersion = null;
4032
if ($this->reflectionProvider->hasClass(TestCase::class)) {
4133
$testCase = $this->reflectionProvider->getClass(TestCase::class);
4234
$file = $testCase->getFileName();
@@ -49,14 +41,14 @@ private function getMajorVersion(): ?int
4941
$json = json_decode($composerJson, true);
5042
$version = $json['extra']['branch-alias']['dev-main'] ?? null;
5143
if ($version !== null) {
52-
$this->majorVersion = (int) explode('.', $version)[0];
44+
$majorVersion = (int) explode('.', $version)[0];
5345
}
5446
}
5547
}
5648
}
5749
}
5850

59-
return $this->majorVersion;
51+
return $this->PHPUnitVersion = new PHPUnitVersion($majorVersion);
6052
}
6153

6254
}

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()) {
6767
continue;
6868
}
6969

src/Rules/PHPUnit/TestMethodsHelperFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222

2323
public function create(): TestMethodsHelper
2424
{
25-
return new TestMethodsHelper($this->fileTypeMapper, $this->PHPUnitVersionDetector->isPHPUnit10OrNewer());
25+
return new TestMethodsHelper($this->fileTypeMapper, $this->PHPUnitVersionDetector->getPHPUnitVersion());
2626
}
2727

2828
}

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ protected function getRule(): Rule
2424
new DataProviderDataRule(
2525
new TestMethodsHelper(
2626
self::getContainer()->getByType(FileTypeMapper::class),
27-
true
27+
new PHPUnitVersion(11)
2828
),
2929
new DataProviderHelper(
3030
$reflectionProvider,
3131
self::getContainer()->getByType(FileTypeMapper::class),
3232
self::getContainer()->getService('defaultAnalysisParser'),
33-
true
33+
new PHPUnitVersion(11)
3434
),
3535

3636
),

tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function getRule(): Rule
2121
$reflection,
2222
self::getContainer()->getByType(FileTypeMapper::class),
2323
self::getContainer()->getService('defaultAnalysisParser'),
24-
true
24+
new PHPUnitVersion(11)
2525
),
2626
true,
2727
true

0 commit comments

Comments
 (0)