diff --git a/src/Type/Php/UnserializeFunctionThrowTypeExtension.php b/src/Type/Php/UnserializeFunctionThrowTypeExtension.php index 56cfb20301e..22cb029aea7 100644 --- a/src/Type/Php/UnserializeFunctionThrowTypeExtension.php +++ b/src/Type/Php/UnserializeFunctionThrowTypeExtension.php @@ -4,6 +4,7 @@ use PhpParser\Node\Expr\FuncCall; use PHPStan\Analyser\Scope; +use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\AutowiredService; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\Constant\ConstantArrayType; @@ -13,19 +14,30 @@ use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\VoidType; +use Throwable; use TypeError; use function count; use function preg_match; /** - * unserialize() throws TypeError and ValueError for invalid $options, and - * TypeError when the data does not fit a typed property. Objects of classes - * that are not allowed are unserialized as __PHP_Incomplete_Class. + * unserialize() can propagate throwables from __wakeup() or __unserialize() + * methods of classes instantiated during deserialization. + * + * It also throws TypeError and ValueError for invalid $options, and TypeError + * when the data does not fit a typed property. Objects of classes that are not + * allowed are unserialized as __PHP_Incomplete_Class. */ #[AutowiredService] final class UnserializeFunctionThrowTypeExtension implements DynamicFunctionThrowTypeExtension { + public function __construct( + #[AutowiredParameter(ref: '%exceptions.implicitThrows%')] + private bool $implicitThrows, + ) + { + } + public function isFunctionSupported(FunctionReflection $functionReflection): bool { return $functionReflection->getName() === 'unserialize'; @@ -33,30 +45,56 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type { - if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) { - return new VoidType(); - } + $throwsValueErrorForInternalFunctions = !$scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no(); $args = $funcCall->getArgs(); foreach ($args as $arg) { if ($arg->unpack || $arg->name !== null) { + if ($this->implicitThrows) { + return new ObjectType(Throwable::class); + } + + if (!$throwsValueErrorForInternalFunctions) { + return new VoidType(); + } + return $functionReflection->getThrowType(); } } if (count($args) < 2) { + if ($this->implicitThrows) { + return new ObjectType(Throwable::class); + } + + if (!$throwsValueErrorForInternalFunctions) { + return new VoidType(); + } + return new ObjectType(TypeError::class); } $optionsType = $scope->getNativeType($args[1]->value); $constantArrays = $optionsType->getConstantArrays(); if (!$optionsType->isArray()->yes() || count($constantArrays) === 0) { + if ($this->implicitThrows) { + return new ObjectType(Throwable::class); + } + + if (!$throwsValueErrorForInternalFunctions) { + return new VoidType(); + } + return $functionReflection->getThrowType(); } $allowsNoClasses = true; foreach ($constantArrays as $constantArray) { if (!$this->areOptionsValid($constantArray)) { + if (!$throwsValueErrorForInternalFunctions) { + return new VoidType(); + } + return $functionReflection->getThrowType(); } @@ -71,6 +109,14 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect return new VoidType(); } + if ($this->implicitThrows) { + return new ObjectType(Throwable::class); + } + + if (!$throwsValueErrorForInternalFunctions) { + return new VoidType(); + } + return new ObjectType(TypeError::class); } diff --git a/tests/PHPStan/Rules/Exceptions/AbilityToDisableImplicitThrowsTest.php b/tests/PHPStan/Rules/Exceptions/AbilityToDisableImplicitThrowsTest.php index 91276cd48e3..7ff12ae7b35 100644 --- a/tests/PHPStan/Rules/Exceptions/AbilityToDisableImplicitThrowsTest.php +++ b/tests/PHPStan/Rules/Exceptions/AbilityToDisableImplicitThrowsTest.php @@ -135,4 +135,19 @@ public static function getAdditionalConfigFiles(): array ); } + #[RequiresPhp('>= 8.0.0')] + public function testUnserializeThrowType(): void + { + $this->analyse([__DIR__ . '/data/bug-15329.php'], [ + [ + 'Dead catch - RuntimeException is never thrown in the try block.', + 31, + ], + [ + 'Dead catch - LogicException is never thrown in the try block.', + 41, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php index 4cda7f9c7e8..ae8f2460a71 100644 --- a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php @@ -1488,14 +1488,16 @@ public function testDsMapVoidThrowType(): void ]); } + #[RequiresPhp('>= 8.0.0')] + public function testBug15329(): void + { + $this->analyse([__DIR__ . '/data/bug-15329.php'], []); + } + #[RequiresPhp('>= 8.0.0')] public function testUnserializeThrowType(): void { $this->analyse([__DIR__ . '/data/unserialize-throw-type.php'], [ - [ - 'Dead catch - ValueError is never thrown in the try block.', - 15, - ], [ 'Dead catch - TypeError is never thrown in the try block.', 27, @@ -1504,10 +1506,6 @@ public function testUnserializeThrowType(): void 'Dead catch - TypeError is never thrown in the try block.', 33, ], - [ - 'Dead catch - ValueError is never thrown in the try block.', - 39, - ], ]); } @@ -1515,14 +1513,6 @@ public function testUnserializeThrowType(): void public function testUnserializeThrowTypeBeforePhp8(): void { $this->analyse([__DIR__ . '/data/unserialize-throw-type.php'], [ - [ - 'Dead catch - ValueError is never thrown in the try block.', - 15, - ], - [ - 'Dead catch - TypeError is never thrown in the try block.', - 21, - ], [ 'Dead catch - TypeError is never thrown in the try block.', 27, @@ -1531,14 +1521,6 @@ public function testUnserializeThrowTypeBeforePhp8(): void 'Dead catch - TypeError is never thrown in the try block.', 33, ], - [ - 'Dead catch - ValueError is never thrown in the try block.', - 39, - ], - [ - 'Dead catch - TypeError is never thrown in the try block.', - 45, - ], [ 'Dead catch - ValueError is never thrown in the try block.', 51, @@ -1551,14 +1533,6 @@ public function testUnserializeThrowTypeBeforePhp8(): void 'Dead catch - TypeError is never thrown in the try block.', 63, ], - [ - 'Dead catch - ValueError is never thrown in the try block.', - 69, - ], - [ - 'Dead catch - TypeError is never thrown in the try block.', - 75, - ], [ 'Dead catch - ValueError is never thrown in the try block.', 81, diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-15329.php b/tests/PHPStan/Rules/Exceptions/data/bug-15329.php new file mode 100644 index 00000000000..fd35f78aea7 --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-15329.php @@ -0,0 +1,43 @@ +