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, 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, ); } 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..f470ca5dfd --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-11900.php @@ -0,0 +1,50 @@ += 8.4 + +namespace Bug11900; + +use Exception; +use Throwable; + +abstract class ADataException extends Exception +{ + /** + * @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(); + } +}