|
6 | 6 | use PhpParser\Node\Expr\FuncCall; |
7 | 7 | use PHPStan\Analyser\Scope; |
8 | 8 | use PHPStan\Php\ComposerPhpVersionFactory; |
| 9 | +use PHPStan\Php\PhpVersion; |
9 | 10 | use PHPStan\Reflection\FunctionReflection; |
10 | 11 | use PHPStan\Type\BooleanType; |
11 | 12 | use PHPStan\Type\Constant\ConstantBooleanType; |
|
16 | 17 | use PHPStan\Type\TypeCombinator; |
17 | 18 | use function array_filter; |
18 | 19 | use function count; |
| 20 | +use function is_array; |
19 | 21 | use function version_compare; |
20 | 22 |
|
21 | 23 | final class VersionCompareFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
22 | 24 | { |
23 | 25 |
|
24 | | - public function __construct(private ComposerPhpVersionFactory $composerPhpVersionFactory) |
| 26 | + /** |
| 27 | + * @param int|array{min: int, max: int}|null $configPhpVersion |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + private int|array|null $configPhpVersion, |
| 31 | + private ComposerPhpVersionFactory $composerPhpVersionFactory, |
| 32 | + ) |
25 | 33 | { |
26 | 34 | } |
27 | 35 |
|
@@ -93,13 +101,21 @@ private function getVersionStrings(Expr $expr, Scope $scope): array |
93 | 101 | if ( |
94 | 102 | $expr instanceof Expr\ConstFetch |
95 | 103 | && $expr->name->toString() === 'PHP_VERSION' |
96 | | - && $this->composerPhpVersionFactory->getMinVersion() !== null |
97 | | - && $this->composerPhpVersionFactory->getMaxVersion() !== null |
98 | 104 | ) { |
99 | | - return [ |
100 | | - new ConstantStringType($this->composerPhpVersionFactory->getMinVersion()->getVersionString()), |
101 | | - new ConstantStringType($this->composerPhpVersionFactory->getMaxVersion()->getVersionString()), |
102 | | - ]; |
| 105 | + if (is_array($this->configPhpVersion)) { |
| 106 | + $minVersion = new PhpVersion($this->configPhpVersion['min']); |
| 107 | + $maxVersion = new PhpVersion($this->configPhpVersion['max']); |
| 108 | + } else { |
| 109 | + $minVersion = $this->composerPhpVersionFactory->getMinVersion(); |
| 110 | + $maxVersion = $this->composerPhpVersionFactory->getMaxVersion(); |
| 111 | + } |
| 112 | + |
| 113 | + if ($minVersion !== null && $maxVersion !== null) { |
| 114 | + return [ |
| 115 | + new ConstantStringType($minVersion->getVersionString()), |
| 116 | + new ConstantStringType($maxVersion->getVersionString()), |
| 117 | + ]; |
| 118 | + } |
103 | 119 | } |
104 | 120 |
|
105 | 121 | return $scope->getType($expr)->getConstantStrings(); |
|
0 commit comments