diff --git a/src/Type/Php/BcMathStringOrNullReturnTypeExtension.php b/src/Type/Php/BcMathStringOrNullReturnTypeExtension.php index a3bc589d4f..ca79bffe01 100644 --- a/src/Type/Php/BcMathStringOrNullReturnTypeExtension.php +++ b/src/Type/Php/BcMathStringOrNullReturnTypeExtension.php @@ -5,6 +5,7 @@ use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\UnaryMinus; use PHPStan\Analyser\Scope; +use PHPStan\Php\PhpVersion; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\Accessory\AccessoryNumericStringType; use PHPStan\Type\Constant\ConstantBooleanType; @@ -22,6 +23,14 @@ class BcMathStringOrNullReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension { + /** @var PhpVersion */ + private $phpVersion; + + public function __construct(PhpVersion $phpVersion) + { + $this->phpVersion = $phpVersion; + } + public function isFunctionSupported(FunctionReflection $functionReflection): bool { return in_array($functionReflection->getName(), ['bcdiv', 'bcmod', 'bcpowmod', 'bcsqrt'], true); @@ -41,7 +50,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, $defaultReturnType = new UnionType([$stringAndNumericStringType, new NullType()]); - if (isset($functionCall->args[1]) === false) { + if (isset($functionCall->args[1]) === false || $this->phpVersion->getVersionId() >= 80000) { return $stringAndNumericStringType; } @@ -126,7 +135,7 @@ private function getTypeForBcSqrt(FuncCall $functionCall, Scope $scope): Type } /** - * bcpowmod() + * @see bcpowmod() * https://www.php.net/manual/en/function.bcpowmod.php * > Returns the result as a string, or FALSE if modulus is 0 or exponent is negative. * @param FuncCall $functionCall diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 45318d4b29..2a84daeca6 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -122,7 +122,11 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-2550.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2899.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/preg_split.php'); - yield from $this->gatherAssertTypes(__DIR__ . '/data/bcmath-dynamic-return.php'); + + if (PHP_VERSION_ID < 80000) { + yield from $this->gatherAssertTypes(__DIR__ . '/data/bcmath-dynamic-return.php'); + } + yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3875.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2611.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3548.php');