Skip to content

Commit 0631d67

Browse files
committed
Implement Scope->getPhpVersion()
1 parent cc4eb92 commit 0631d67

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/Analyser/MutatingScope.php

+23
Original file line numberDiff line numberDiff line change
@@ -5721,4 +5721,27 @@ public function getIterableValueType(Type $iteratee): Type
57215721
return $iteratee->getIterableValueType();
57225722
}
57235723

5724+
public function getPhpVersion(): PhpVersion
5725+
{
5726+
$versionId = $this->getType(new ConstFetch(new Name('PHP_VERSION_ID')));
5727+
if ($versionId instanceof IntegerRangeType && $versionId->getMin() !== null) {
5728+
return new PhpVersion($versionId->getMin(), PhpVersion::SOURCE_RUNTIME);
5729+
}
5730+
5731+
$scalars = $versionId->getConstantScalarValues();
5732+
if ($scalars !== []) {
5733+
$ints = [];
5734+
foreach($scalars as $scalar) {
5735+
if (!is_int($scalar)) {
5736+
throw new ShouldNotHappenException();
5737+
}
5738+
$ints[] = $scalar;
5739+
}
5740+
5741+
return new PhpVersion(min($ints), PhpVersion::SOURCE_RUNTIME);
5742+
}
5743+
5744+
return $this->phpVersion;
5745+
}
5746+
57245747
}

src/Analyser/Scope.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Name;
88
use PhpParser\Node\Param;
9+
use PHPStan\Php\PhpVersion;
910
use PHPStan\Reflection\ClassConstantReflection;
1011
use PHPStan\Reflection\ClassMemberAccessAnswerer;
1112
use PHPStan\Reflection\ClassReflection;
@@ -136,4 +137,6 @@ public function filterByFalseyValue(Expr $expr): self;
136137

137138
public function isInFirstLevelStatement(): bool;
138139

140+
public function getPhpVersion(): PhpVersion;
141+
139142
}

src/Rules/Methods/FinalPrivateMethodRule.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Node\InClassMethodNode;
8-
use PHPStan\Php\PhpVersion;
98
use PHPStan\Rules\Rule;
109
use PHPStan\Rules\RuleErrorBuilder;
1110
use function sprintf;
@@ -14,12 +13,6 @@
1413
final class FinalPrivateMethodRule implements Rule
1514
{
1615

17-
public function __construct(
18-
private PhpVersion $phpVersion,
19-
)
20-
{
21-
}
22-
2316
public function getNodeType(): string
2417
{
2518
return InClassMethodNode::class;
@@ -28,7 +21,7 @@ public function getNodeType(): string
2821
public function processNode(Node $node, Scope $scope): array
2922
{
3023
$method = $node->getMethodReflection();
31-
if (!$this->phpVersion->producesWarningForFinalPrivateMethods()) {
24+
if (!$scope->getPhpVersion()->producesWarningForFinalPrivateMethods()) {
3225
return [];
3326
}
3427

0 commit comments

Comments
 (0)