Skip to content

Commit f49fb32

Browse files
committed
Utilize phpVersion.min+max in VersionCompareFunctionDynamicReturnTypeExtension
1 parent 5a132f1 commit f49fb32

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

conf/config.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,8 @@ services:
16831683

16841684
-
16851685
class: PHPStan\Type\Php\VersionCompareFunctionDynamicReturnTypeExtension
1686+
arguments:
1687+
configPhpVersion: %phpVersion%
16861688
tags:
16871689
- phpstan.broker.dynamicFunctionReturnTypeExtension
16881690

src/Type/Php/VersionCompareFunctionDynamicReturnTypeExtension.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr\FuncCall;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Php\ComposerPhpVersionFactory;
9+
use PHPStan\Php\PhpVersion;
910
use PHPStan\Reflection\FunctionReflection;
1011
use PHPStan\Type\BooleanType;
1112
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -16,12 +17,16 @@
1617
use PHPStan\Type\TypeCombinator;
1718
use function array_filter;
1819
use function count;
20+
use function is_array;
1921
use function version_compare;
2022

2123
final class VersionCompareFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
2224
{
2325

24-
public function __construct(private ComposerPhpVersionFactory $composerPhpVersionFactory)
26+
public function __construct(
27+
private int|array|null $configPhpVersion,
28+
private ComposerPhpVersionFactory $composerPhpVersionFactory,
29+
)
2530
{
2631
}
2732

@@ -93,13 +98,21 @@ private function getVersionStrings(Expr $expr, Scope $scope): array
9398
if (
9499
$expr instanceof Expr\ConstFetch
95100
&& $expr->name->toString() === 'PHP_VERSION'
96-
&& $this->composerPhpVersionFactory->getMinVersion() !== null
97-
&& $this->composerPhpVersionFactory->getMaxVersion() !== null
98101
) {
99-
return [
100-
new ConstantStringType($this->composerPhpVersionFactory->getMinVersion()->getVersionString()),
101-
new ConstantStringType($this->composerPhpVersionFactory->getMaxVersion()->getVersionString()),
102-
];
102+
if (is_array($this->configPhpVersion)) {
103+
$minVersion = new PhpVersion($this->configPhpVersion['min']);
104+
$maxVersion = new PhpVersion($this->configPhpVersion['max']);
105+
} else {
106+
$minVersion = $this->composerPhpVersionFactory->getMinVersion();
107+
$maxVersion = $this->composerPhpVersionFactory->getMaxVersion();
108+
}
109+
110+
if ($minVersion !== null && $maxVersion !== null) {
111+
return [
112+
new ConstantStringType($minVersion->getVersionString()),
113+
new ConstantStringType($maxVersion->getVersionString()),
114+
];
115+
}
103116
}
104117

105118
return $scope->getType($expr)->getConstantStrings();

0 commit comments

Comments
 (0)