From 021e81820d3873e51fb53a3d56be25b4a0571fb7 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 20 Jul 2025 11:30:13 +0200 Subject: [PATCH 1/5] Resolve Static throw points --- src/Analyser/NodeScopeResolver.php | 47 ++++++++++ .../PHPStan/Rules/Exceptions/Bug11900Test.php | 41 +++++++++ tests/PHPStan/Rules/Exceptions/bug-11900.neon | 6 ++ .../Rules/Exceptions/data/bug-11900.php | 87 +++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 tests/PHPStan/Rules/Exceptions/Bug11900Test.php create mode 100644 tests/PHPStan/Rules/Exceptions/bug-11900.neon create mode 100644 tests/PHPStan/Rules/Exceptions/data/bug-11900.php diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index dbaa69adfb..b640223e2c 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -4260,6 +4260,18 @@ private function getMethodThrowPoint(MethodReflection $methodReflection, Paramet if ($throwType !== null) { if (!$throwType->isVoid()->yes()) { + if ($throwType instanceof StaticType) { + $classReflections = $scope->getType($methodCall->var)->getObjectClassReflections(); + if (count($classReflections) > 0) { + $types = []; + foreach ($classReflections as $classReflection) { + $types[] = $throwType->changeBaseClass($classReflection); + } + + $throwType = TypeCombinator::union(...$types); + } + } + return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true); } } elseif ($this->implicitThrows) { @@ -4297,6 +4309,10 @@ private function getConstructorThrowPoint(MethodReflection $constructorReflectio if ($constructorReflection->getThrowType() !== null) { $throwType = $constructorReflection->getThrowType(); if (!$throwType->isVoid()->yes()) { + if ($throwType instanceof StaticType && $this->reflectionProvider->hasClass($className->name)) { + $throwType = $throwType->changeBaseClass($this->reflectionProvider->getClass($className->name)); + } + return ThrowPoint::createExplicit($scope, $throwType, $new, true); } } elseif ($this->implicitThrows) { @@ -4329,6 +4345,25 @@ private function getStaticMethodThrowPoint(MethodReflection $methodReflection, P if ($methodReflection->getThrowType() !== null) { $throwType = $methodReflection->getThrowType(); if (!$throwType->isVoid()->yes()) { + if ($throwType instanceof StaticType) { + if ($methodCall->class instanceof Name) { + if ($this->reflectionProvider->hasClass($methodCall->class->name)) { + $throwType = $throwType->changeBaseClass($this->reflectionProvider->getClass($methodCall->class->name)); + } + } else { + $classReflections = $scope->getType($methodCall->class)->getObjectClassReflections(); + + if (count($classReflections) > 0) { + $types = []; + foreach ($classReflections as $classReflection) { + $types[] = $throwType->changeBaseClass($classReflection); + } + + $throwType = TypeCombinator::union(...$types); + } + } + } + return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true); } } elseif ($this->implicitThrows) { @@ -4409,6 +4444,18 @@ private function getThrowPointsFromPropertyHook( if ($throwType !== null) { if (!$throwType->isVoid()->yes()) { + if ($throwType instanceof StaticType) { + $classReflections = $scope->getType($propertyFetch->var)->getObjectClassReflections(); + if (count($classReflections) > 0) { + $types = []; + foreach ($classReflections as $classReflection) { + $types[] = $throwType->changeBaseClass($classReflection); + } + + $throwType = TypeCombinator::union(...$types); + } + } + return [ThrowPoint::createExplicit($scope, $throwType, $propertyFetch, true)]; } } elseif ($this->implicitThrows) { diff --git a/tests/PHPStan/Rules/Exceptions/Bug11900Test.php b/tests/PHPStan/Rules/Exceptions/Bug11900Test.php new file mode 100644 index 0000000000..c8e615b473 --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/Bug11900Test.php @@ -0,0 +1,41 @@ + + */ +class Bug11900Test extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new MissingCheckedExceptionInMethodThrowsRule( + new MissingCheckedExceptionInThrowsCheck(new DefaultExceptionTypeResolver( + self::createReflectionProvider(), + [], + [], + [], + [], + )), + ); + } + + #[RequiresPhp('>= 8.4')] + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/bug-11900.php'], []); + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/bug-11900.neon', + ]; + } + +} diff --git a/tests/PHPStan/Rules/Exceptions/bug-11900.neon b/tests/PHPStan/Rules/Exceptions/bug-11900.neon new file mode 100644 index 0000000000..93a3c1512a --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/bug-11900.neon @@ -0,0 +1,6 @@ +parameters: + exceptions: + implicitThrows: false + check: + missingCheckedExceptionInThrows: true + tooWideThrowType: true diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-11900.php b/tests/PHPStan/Rules/Exceptions/data/bug-11900.php new file mode 100644 index 0000000000..74671427fb --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-11900.php @@ -0,0 +1,87 @@ += 8.4 + +namespace Bug11900; + +use Exception; +use Throwable; + +abstract class ADataException extends Exception +{ + public int $i { + /** @throws static */ + get { + if (rand(0, 1)) { + throw new static(); + } + + return 42; + } + } + + /** @throws static */ + public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) + { + if (rand(0, 1)) { + throw new static(); + } + + parent::__construct($message, $code, $previous); + } + + /** + * @return void + * @throws static + */ + public function throw1(): void + { + throw $this; + } + + /** + * @return void + * @throws static + */ + public static function throw2(): void + { + throw new static(); + } +} + +final class TestDataException extends ADataException +{ +} + +class TestPhpStan +{ + /** + * @throws TestDataException + */ + public function validate(TestDataException $e): void + { + $e->throw1(); + } + + /** + * @throws TestDataException + */ + public function validate2(): void + { + TestDataException::throw2(); + } + + /** + * @throws TestDataException + */ + public function validate3(TestDataException $e): void + { + $e->i; + } + + /** + * @throws TestDataException + */ + public function validate4(): void + { + new TestDataException(); + } +} From 98b340b4a9e23e0c246ef043e3debbbad4a41094 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 22 Jul 2025 23:54:35 +0200 Subject: [PATCH 2/5] Improve --- src/Analyser/NodeScopeResolver.php | 31 ------------------- .../Dummy/ChangedTypeMethodReflection.php | 3 +- ...ackUnresolvedMethodPrototypeReflection.php | 5 +++ ...ypeUnresolvedMethodPrototypeReflection.php | 5 +++ 4 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index b640223e2c..cc2ef735af 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -4260,18 +4260,6 @@ private function getMethodThrowPoint(MethodReflection $methodReflection, Paramet if ($throwType !== null) { if (!$throwType->isVoid()->yes()) { - if ($throwType instanceof StaticType) { - $classReflections = $scope->getType($methodCall->var)->getObjectClassReflections(); - if (count($classReflections) > 0) { - $types = []; - foreach ($classReflections as $classReflection) { - $types[] = $throwType->changeBaseClass($classReflection); - } - - $throwType = TypeCombinator::union(...$types); - } - } - return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true); } } elseif ($this->implicitThrows) { @@ -4345,25 +4333,6 @@ private function getStaticMethodThrowPoint(MethodReflection $methodReflection, P if ($methodReflection->getThrowType() !== null) { $throwType = $methodReflection->getThrowType(); if (!$throwType->isVoid()->yes()) { - if ($throwType instanceof StaticType) { - if ($methodCall->class instanceof Name) { - if ($this->reflectionProvider->hasClass($methodCall->class->name)) { - $throwType = $throwType->changeBaseClass($this->reflectionProvider->getClass($methodCall->class->name)); - } - } else { - $classReflections = $scope->getType($methodCall->class)->getObjectClassReflections(); - - if (count($classReflections) > 0) { - $types = []; - foreach ($classReflections as $classReflection) { - $types[] = $throwType->changeBaseClass($classReflection); - } - - $throwType = TypeCombinator::union(...$types); - } - } - } - return ThrowPoint::createExplicit($scope, $throwType, $methodCall, true); } } elseif ($this->implicitThrows) { diff --git a/src/Reflection/Dummy/ChangedTypeMethodReflection.php b/src/Reflection/Dummy/ChangedTypeMethodReflection.php index ade4e1c4aa..fc4b21656b 100644 --- a/src/Reflection/Dummy/ChangedTypeMethodReflection.php +++ b/src/Reflection/Dummy/ChangedTypeMethodReflection.php @@ -26,6 +26,7 @@ public function __construct( private array $variants, private ?array $namedArgumentsVariants, private ?Type $selfOutType, + private ?Type $throwType, ) { } @@ -122,7 +123,7 @@ public function isBuiltin(): TrinaryLogic public function getThrowType(): ?Type { - return $this->reflection->getThrowType(); + return $this->throwType; } public function hasSideEffects(): TrinaryLogic diff --git a/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php b/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php index 980a3f293f..2874a465fd 100644 --- a/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php +++ b/src/Reflection/Type/CallbackUnresolvedMethodPrototypeReflection.php @@ -125,6 +125,10 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass, $namedArgumentVariants = $namedArgumentVariants !== null ? array_map($variantFn, $namedArgumentVariants) : null; + $throwType = $method->getThrowType(); + $throwType = $throwType !== null + ? $this->transformStaticType($throwType) + : null; return new ChangedTypeMethodReflection( $declaringClass, @@ -132,6 +136,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass, $variants, $namedArgumentVariants, $selfOutType, + $throwType, ); } diff --git a/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php b/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php index c78a435583..44709d32ae 100644 --- a/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php +++ b/src/Reflection/Type/CalledOnTypeUnresolvedMethodPrototypeReflection.php @@ -120,6 +120,10 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass, $namedArgumentsVariants = $namedArgumentsVariants !== null ? array_map($variantFn, $namedArgumentsVariants) : null; + $throwType = $method->getThrowType(); + $throwType = $throwType !== null + ? $this->transformStaticType($throwType) + : null; return new ChangedTypeMethodReflection( $declaringClass, @@ -127,6 +131,7 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass, $variants, $namedArgumentsVariants, $selfOutType, + $throwType, ); } From af6d594e94934cd3e53a6b54b4c0d47eb963f160 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 23 Jul 2025 08:37:11 +0200 Subject: [PATCH 3/5] Remove failing cases --- src/Analyser/NodeScopeResolver.php | 16 ---------------- .../PHPStan/Rules/Exceptions/data/bug-11900.php | 16 ---------------- 2 files changed, 32 deletions(-) diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index cc2ef735af..dbaa69adfb 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -4297,10 +4297,6 @@ private function getConstructorThrowPoint(MethodReflection $constructorReflectio if ($constructorReflection->getThrowType() !== null) { $throwType = $constructorReflection->getThrowType(); if (!$throwType->isVoid()->yes()) { - if ($throwType instanceof StaticType && $this->reflectionProvider->hasClass($className->name)) { - $throwType = $throwType->changeBaseClass($this->reflectionProvider->getClass($className->name)); - } - return ThrowPoint::createExplicit($scope, $throwType, $new, true); } } elseif ($this->implicitThrows) { @@ -4413,18 +4409,6 @@ private function getThrowPointsFromPropertyHook( if ($throwType !== null) { if (!$throwType->isVoid()->yes()) { - if ($throwType instanceof StaticType) { - $classReflections = $scope->getType($propertyFetch->var)->getObjectClassReflections(); - if (count($classReflections) > 0) { - $types = []; - foreach ($classReflections as $classReflection) { - $types[] = $throwType->changeBaseClass($classReflection); - } - - $throwType = TypeCombinator::union(...$types); - } - } - return [ThrowPoint::createExplicit($scope, $throwType, $propertyFetch, true)]; } } elseif ($this->implicitThrows) { diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-11900.php b/tests/PHPStan/Rules/Exceptions/data/bug-11900.php index 74671427fb..c38f6bf2ab 100644 --- a/tests/PHPStan/Rules/Exceptions/data/bug-11900.php +++ b/tests/PHPStan/Rules/Exceptions/data/bug-11900.php @@ -68,20 +68,4 @@ public function validate2(): void { TestDataException::throw2(); } - - /** - * @throws TestDataException - */ - public function validate3(TestDataException $e): void - { - $e->i; - } - - /** - * @throws TestDataException - */ - public function validate4(): void - { - new TestDataException(); - } } From 34e979b8b122afc0d479e8da9325ad98e057b670 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 23 Jul 2025 09:33:42 +0200 Subject: [PATCH 4/5] Simplify --- .../Rules/Exceptions/data/bug-11900.php | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-11900.php b/tests/PHPStan/Rules/Exceptions/data/bug-11900.php index c38f6bf2ab..f470ca5dfd 100644 --- a/tests/PHPStan/Rules/Exceptions/data/bug-11900.php +++ b/tests/PHPStan/Rules/Exceptions/data/bug-11900.php @@ -7,27 +7,6 @@ abstract class ADataException extends Exception { - public int $i { - /** @throws static */ - get { - if (rand(0, 1)) { - throw new static(); - } - - return 42; - } - } - - /** @throws static */ - public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) - { - if (rand(0, 1)) { - throw new static(); - } - - parent::__construct($message, $code, $previous); - } - /** * @return void * @throws static From b78d39578fb6160824914b4917ade8e8b79bb327 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 24 Jul 2025 21:24:15 +0200 Subject: [PATCH 5/5] More fixes --- src/Analyser/MutatingScope.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 395626e1ce..a3e61e82e8 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3125,7 +3125,7 @@ public function enterClassMethod( $this->getParameterAttributes($classMethod), $this->transformStaticType($this->getFunctionType($classMethod->returnType, false, false)), $phpDocReturnType !== null ? $this->transformStaticType(TemplateTypeHelper::toArgument($phpDocReturnType)) : null, - $throwType, + $throwType !== null ? $this->transformStaticType(TemplateTypeHelper::toArgument($throwType)) : null, $deprecatedDescription, $isDeprecated, $isInternal, @@ -3212,7 +3212,7 @@ public function enterPropertyHook( $this->getParameterAttributes($hook), $realReturnType, $phpDocReturnType, - $throwType, + $throwType !== null ? $this->transformStaticType(TemplateTypeHelper::toArgument($throwType)) : null, $deprecatedDescription, $isDeprecated, false,