diff --git a/src/Type/Php/ReflectionMethodInvokeMethodThrowTypeExtension.php b/src/Type/Php/ReflectionMethodInvokeMethodThrowTypeExtension.php new file mode 100644 index 0000000000..8e8e3e96f4 --- /dev/null +++ b/src/Type/Php/ReflectionMethodInvokeMethodThrowTypeExtension.php @@ -0,0 +1,31 @@ +getName(), ['invoke', 'invokeArgs'], true) + && $methodReflection->getDeclaringClass()->getName() === ReflectionMethod::class; + } + + public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type + { + return new ObjectType(Throwable::class); + } + +} diff --git a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php index 272c010d97..39364dc041 100644 --- a/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php +++ b/tests/PHPStan/Rules/Exceptions/CatchWithUnthrownExceptionRuleTest.php @@ -643,4 +643,14 @@ public function testPropertyHooks(): void ]); } + public function testBug7719(): void + { + $this->analyse([__DIR__ . '/data/bug-7719.php'], []); + } + + public function testBug9267(): void + { + $this->analyse([__DIR__ . '/data/bug-9267.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-7719.php b/tests/PHPStan/Rules/Exceptions/data/bug-7719.php new file mode 100644 index 0000000000..f187f99f67 --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-7719.php @@ -0,0 +1,34 @@ +invokeArgs($endpoint, ['id' => 2]); + } catch (\RuntimeException $e) { + echo $e->getMessage(); + die; + } + var_dump($methodResponse); + } + + public function sayHelloInvoke(Endpoint $endpoint, string $methodName): void + { + try { + $methodResponse = (new \ReflectionMethod($endpoint, $methodName))->invoke($endpoint, 2); + } catch (\RuntimeException $e) { + echo $e->getMessage(); + die; + } + var_dump($methodResponse); + } +} diff --git a/tests/PHPStan/Rules/Exceptions/data/bug-9267.php b/tests/PHPStan/Rules/Exceptions/data/bug-9267.php new file mode 100644 index 0000000000..4ebc2bdf36 --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-9267.php @@ -0,0 +1,21 @@ +invokeArgs(new C, array()); + } + catch (FooException $e) { + print "CAUGHT FOO!\n"; + } +} + +class C { + /** @return never */ + public function test() { + throw new FooException(""); + } +}