diff --git a/src/Php/PhpVersion.php b/src/Php/PhpVersion.php index a1fc40fe26..b236a79122 100644 --- a/src/Php/PhpVersion.php +++ b/src/Php/PhpVersion.php @@ -156,6 +156,11 @@ public function supportsCaseInsensitiveConstantNames(): bool return $this->versionId < 80000; } + public function hasStricterRoundFunctions(): bool + { + return $this->versionId >= 80000; + } + public function hasTentativeReturnTypes(): bool { return $this->versionId >= 80100; diff --git a/src/Type/Php/RoundFunctionReturnTypeExtension.php b/src/Type/Php/RoundFunctionReturnTypeExtension.php index 858133aac5..2808e33c28 100644 --- a/src/Type/Php/RoundFunctionReturnTypeExtension.php +++ b/src/Type/Php/RoundFunctionReturnTypeExtension.php @@ -4,6 +4,7 @@ use PhpParser\Node\Expr\FuncCall; use PHPStan\Analyser\Scope; +use PHPStan\Php\PhpVersion; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\BenevolentUnionType; use PHPStan\Type\Constant\ConstantBooleanType; @@ -17,10 +18,12 @@ use PHPStan\Type\TypeCombinator; use function count; use function in_array; -use const PHP_VERSION_ID; class RoundFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension { + public function __construct(private PhpVersion $phpVersion) + { + } public function isFunctionSupported(FunctionReflection $functionReflection): bool { @@ -37,7 +40,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type { - if (PHP_VERSION_ID >= 80000) { + if ($this->phpVersion->hasStricterRoundFunctions()) { // PHP 8 fatals with a missing parameter. $noArgsReturnType = new NeverType(true); // PHP 8 can either return a float or fatal. @@ -65,7 +68,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, return $defaultReturnType; } - if (PHP_VERSION_ID >= 80000) { + if ($this->phpVersion->hasStricterRoundFunctions()) { $allowed = TypeCombinator::union( new IntegerType(), new FloatType(),