From bd6c00c13085ca06c175454efca931ec8ef31595 Mon Sep 17 00:00:00 2001 From: Sander Muller Date: Tue, 17 Nov 2026 11:09:48 +0100 Subject: [PATCH] Keep the PHP 8 throw types of built-in functions in a version range below PHP 8 With a PHP version range such as 7.4 to 8.5, the stubs of the minimum version do not have the @throws tags that PHP 8 added, so getThrowType() returns null. The throw type extensions of array_chunk(), str_split(), min(), max(), the printf() family, version_compare(), array_combine() and round() returned that null, and a catch of ValueError around these calls was reported as dead. These extensions now fall back to ValueError (and ArgumentCountError for the printf() family) when the range only maybe includes PHP 8, like the range() extension does. Co-Authored-By: Claude Opus 5.5 --- .../ArrayChunkFunctionThrowTypeExtension.php | 10 ++- ...ArrayCombineFunctionThrowTypeExtension.php | 10 ++- .../Php/MinMaxFunctionThrowTypeExtension.php | 8 +- .../Php/PrintfFunctionThrowTypeExtension.php | 31 +++++-- .../Php/RoundFunctionThrowTypeExtension.php | 7 +- .../StrSplitFunctionThrowTypeExtension.php | 8 +- ...mpareFunctionDynamicThrowTypeExtension.php | 4 +- .../unthrown-exception-php-version-range.php | 81 +++++++++++++++++++ 8 files changed, 138 insertions(+), 21 deletions(-) diff --git a/src/Type/Php/ArrayChunkFunctionThrowTypeExtension.php b/src/Type/Php/ArrayChunkFunctionThrowTypeExtension.php index 03e7d0ee1bc..e0e08a8355c 100644 --- a/src/Type/Php/ArrayChunkFunctionThrowTypeExtension.php +++ b/src/Type/Php/ArrayChunkFunctionThrowTypeExtension.php @@ -8,8 +8,10 @@ use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\DynamicFunctionThrowTypeExtension; use PHPStan\Type\IntegerRangeType; +use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\VoidType; +use ValueError; use function count; /** @@ -24,7 +26,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo return $functionReflection->getName() === 'array_chunk'; } - public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type + public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): Type { if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) { return new VoidType(); @@ -33,19 +35,19 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect $args = $funcCall->getArgs(); foreach ($args as $arg) { if ($arg->unpack || $arg->name !== null) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } } if (count($args) < 2) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } if (IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($scope->getNativeType($args[1]->value))->yes()) { return new VoidType(); } - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } } diff --git a/src/Type/Php/ArrayCombineFunctionThrowTypeExtension.php b/src/Type/Php/ArrayCombineFunctionThrowTypeExtension.php index 34041e37531..1e81cd29e27 100644 --- a/src/Type/Php/ArrayCombineFunctionThrowTypeExtension.php +++ b/src/Type/Php/ArrayCombineFunctionThrowTypeExtension.php @@ -7,7 +7,9 @@ use PHPStan\DependencyInjection\AutowiredService; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\DynamicFunctionThrowTypeExtension; +use PHPStan\Type\ObjectType; use PHPStan\Type\Type; +use ValueError; use function count; #[AutowiredService] @@ -25,9 +27,13 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type { + if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) { + return null; + } + $args = $funcCall->getArgs(); if (count($args) < 2) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } $firstArg = $args[0]->value; @@ -35,7 +41,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect $hasValueError = $this->arrayCombineHelper->getReturnAndThrowType($firstArg, $secondArg, $scope)[1]; if (!$hasValueError->no()) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } return null; diff --git a/src/Type/Php/MinMaxFunctionThrowTypeExtension.php b/src/Type/Php/MinMaxFunctionThrowTypeExtension.php index 0ec781780de..8dc21bed362 100644 --- a/src/Type/Php/MinMaxFunctionThrowTypeExtension.php +++ b/src/Type/Php/MinMaxFunctionThrowTypeExtension.php @@ -8,8 +8,10 @@ use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\DynamicFunctionThrowTypeExtension; +use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\VoidType; +use ValueError; use function count; use function in_array; @@ -33,7 +35,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect return $functionReflection->getThrowType(); } - if (!$scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->yes()) { + if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) { return new VoidType(); } @@ -49,7 +51,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect $argTypes = []; foreach ($args as $arg) { if ($arg->unpack || $arg->name !== null) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } $argTypes[] = $scope->getNativeType($arg->value); @@ -58,7 +60,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect $parametersAcceptor = ParametersAcceptorSelector::selectFromTypes($argTypes, $functionReflection->getVariants(), false); $parameters = $parametersAcceptor->getParameters(); if (isset($parameters[0]) && $parameters[0]->getType()->isArray()->yes()) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } return new VoidType(); diff --git a/src/Type/Php/PrintfFunctionThrowTypeExtension.php b/src/Type/Php/PrintfFunctionThrowTypeExtension.php index c7ee32d4d71..41ab267aa95 100644 --- a/src/Type/Php/PrintfFunctionThrowTypeExtension.php +++ b/src/Type/Php/PrintfFunctionThrowTypeExtension.php @@ -64,21 +64,21 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect foreach ($args as $i => $arg) { $originalArg = $arg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE); if ($arg->name !== null || ($originalArg instanceof Arg && $originalArg->name !== null)) { - return $functionReflection->getThrowType(); + return $this->getDefaultThrowType($functionReflection, $scope); } if ($i <= $formatPosition && $arg->unpack) { - return $functionReflection->getThrowType(); + return $this->getDefaultThrowType($functionReflection, $scope); } } if (!isset($args[$formatPosition])) { - return $functionReflection->getThrowType(); + return $this->getDefaultThrowType($functionReflection, $scope); } $formatType = $scope->getNativeType($args[$formatPosition]->value); $formats = $formatType->getConstantStrings(); if (!$formatType->isString()->yes() || count($formats) === 0) { - return $functionReflection->getThrowType(); + return $this->getDefaultThrowType($functionReflection, $scope); } $uses = []; @@ -86,7 +86,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect foreach ($formats as $format) { $formatUses = $this->formatParser->parse($format->getValue(), $scope->getPhpVersion()); if ($formatUses === null) { - return $functionReflection->getThrowType(); + return $this->getDefaultThrowType($functionReflection, $scope); } foreach ($formatUses as $use) { @@ -135,6 +135,27 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect return TypeCombinator::union(...$throwTypes); } + /** + * The stubs of a PHP version range below PHP 8 leave out the errors these functions throw as of PHP 8. + */ + private function getDefaultThrowType(FunctionReflection $functionReflection, Scope $scope): ?Type + { + $throwType = $functionReflection->getThrowType(); + if (!$scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->maybe()) { + return $throwType; + } + + $php8ThrowTypes = [new ObjectType(ValueError::class)]; + if (!in_array($functionReflection->getName(), self::ARRAY_FUNCTIONS, true)) { + $php8ThrowTypes[] = new ObjectType(ArgumentCountError::class); + } + if ($throwType !== null) { + $php8ThrowTypes[] = $throwType; + } + + return TypeCombinator::union(...$php8ThrowTypes); + } + /** * Types of the arguments after the format at positions known from the call, * and how many arguments are passed at least. An unpacked constant list diff --git a/src/Type/Php/RoundFunctionThrowTypeExtension.php b/src/Type/Php/RoundFunctionThrowTypeExtension.php index 2f3af9c16ce..e6ba375e413 100644 --- a/src/Type/Php/RoundFunctionThrowTypeExtension.php +++ b/src/Type/Php/RoundFunctionThrowTypeExtension.php @@ -12,6 +12,7 @@ use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use PHPStan\Type\VoidType; +use ValueError; use function count; /** @@ -27,7 +28,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo return $functionReflection->getName() === 'round'; } - public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type + public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): Type { if ($scope->getPhpVersion()->throwsValueErrorForInvalidRoundingMode()->no()) { return new VoidType(); @@ -36,7 +37,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect $args = $funcCall->getArgs(); foreach ($args as $arg) { if ($arg->unpack || $arg->name !== null) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } } @@ -52,7 +53,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect return new VoidType(); } - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } } diff --git a/src/Type/Php/StrSplitFunctionThrowTypeExtension.php b/src/Type/Php/StrSplitFunctionThrowTypeExtension.php index 10891dbc717..1d5167c36a1 100644 --- a/src/Type/Php/StrSplitFunctionThrowTypeExtension.php +++ b/src/Type/Php/StrSplitFunctionThrowTypeExtension.php @@ -8,8 +8,10 @@ use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\DynamicFunctionThrowTypeExtension; use PHPStan\Type\IntegerRangeType; +use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\VoidType; +use ValueError; use function count; /** @@ -24,7 +26,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo return $functionReflection->getName() === 'str_split'; } - public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type + public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): Type { if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) { return new VoidType(); @@ -33,7 +35,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect $args = $funcCall->getArgs(); foreach ($args as $arg) { if ($arg->unpack || $arg->name !== null) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } } @@ -45,7 +47,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect return new VoidType(); } - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } } diff --git a/src/Type/Php/VersionCompareFunctionDynamicThrowTypeExtension.php b/src/Type/Php/VersionCompareFunctionDynamicThrowTypeExtension.php index 96ae2961c44..135c32a779a 100644 --- a/src/Type/Php/VersionCompareFunctionDynamicThrowTypeExtension.php +++ b/src/Type/Php/VersionCompareFunctionDynamicThrowTypeExtension.php @@ -7,7 +7,9 @@ use PHPStan\DependencyInjection\AutowiredService; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\DynamicFunctionThrowTypeExtension; +use PHPStan\Type\ObjectType; use PHPStan\Type\Type; +use ValueError; #[AutowiredService] final class VersionCompareFunctionDynamicThrowTypeExtension implements DynamicFunctionThrowTypeExtension @@ -35,7 +37,7 @@ public function getThrowTypeFromFunctionCall( $operatorStrings = $scope->getType($args[2]->value)->getConstantStrings(); if (VersionCompareFunctionDynamicReturnTypeExtension::mightBeInvalidOperator($operatorStrings)) { - return $functionReflection->getThrowType(); + return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class); } return null; diff --git a/tests/PHPStan/Rules/Exceptions/data/unthrown-exception-php-version-range.php b/tests/PHPStan/Rules/Exceptions/data/unthrown-exception-php-version-range.php index 812e2c652a4..521998e8dfe 100644 --- a/tests/PHPStan/Rules/Exceptions/data/unthrown-exception-php-version-range.php +++ b/tests/PHPStan/Rules/Exceptions/data/unthrown-exception-php-version-range.php @@ -74,4 +74,85 @@ public function rangeWithUnknownBoundary(int $i): void } } + public function arrayChunk(int $length): void + { + try { + array_chunk([1], $length); + } catch (\ValueError $e) { // a length below 1 throws on PHP 8 + + } + } + + public function strSplit(string $s, int $length): void + { + try { + str_split($s, $length); + } catch (\ValueError $e) { // a length below 1 throws on PHP 8 + + } + } + + public function minOfArray(array $a): void + { + try { + min($a); + } catch (\ValueError $e) { // an empty array throws on PHP 8 + + } + } + + public function maxOfArray(array $a): void + { + try { + max($a); + } catch (\ValueError $e) { // an empty array throws on PHP 8 + + } + } + + public function sprintfWithUnknownFormat(string $format): void + { + try { + sprintf($format, 1); + } catch (\ValueError $e) { // an invalid format throws on PHP 8 + + } + } + + public function vprintfWithUnknownFormat(string $format, array $values): void + { + try { + vprintf($format, $values); + } catch (\ValueError $e) { // an invalid format throws on PHP 8 + + } + } + + public function versionCompareWithUnknownOperator(string $operator): void + { + try { + version_compare('1.0', '2.0', $operator); + } catch (\ValueError $e) { // an invalid operator throws on PHP 8 + + } + } + + public function arrayCombine(array $keys, array $values): void + { + try { + array_combine($keys, $values); + } catch (\ValueError $e) { // arrays of different sizes throw on PHP 8 + + } + } + + public function roundWithUnknownMode(float $f, int $mode): void + { + try { + round($f, 0, $mode); + } catch (\ValueError $e) { // an invalid mode throws on PHP 8.4+ + + } + } + }