diff --git a/conf/config.neon b/conf/config.neon index 19056af1ab..c0bd1d45f3 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -125,6 +125,7 @@ parameters: - ../stubs/Redis.stub - ../stubs/ReflectionAttribute.stub - ../stubs/ReflectionClassConstant.stub + - ../stubs/ReflectionFunction.stub - ../stubs/ReflectionFunctionAbstract.stub - ../stubs/ReflectionMethod.stub - ../stubs/ReflectionParameter.stub diff --git a/stubs/ReflectionFunction.stub b/stubs/ReflectionFunction.stub new file mode 100644 index 0000000000..3b2a530dba --- /dev/null +++ b/stubs/ReflectionFunction.stub @@ -0,0 +1,16 @@ +analyse([__DIR__ . '/data/bug-9568.php'], []); } + public function testBug7719(): void + { + $this->analyse([__DIR__ . '/data/bug-7719.php'], []); + } + + public function testBug9267(): void + { + $this->analyse([__DIR__ . '/data/bug-9267.php'], []); + } + #[RequiresPhp('>= 8.4')] public function testPropertyHooks(): void { 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..5da5979c90 --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-7719.php @@ -0,0 +1,56 @@ +invokeArgs($endpoint, ['id' => 2]); + } catch (\RuntimeException $e) { + echo $e->getMessage(); + die; + } + var_dump($methodResponse); + } + + public function sayHelloWithInvoke(Endpoint $endpoint, string $methodName): void + { + try { + $methodResponse = (new \ReflectionMethod($endpoint, $methodName))->invoke($endpoint, 2); + } catch (\RuntimeException $e) { + echo $e->getMessage(); + die; + } + var_dump($methodResponse); + } + + public function sayHelloWithFunction(string $functionName): void + { + try { + $result = (new \ReflectionFunction($functionName))->invokeArgs([1, 2]); + } catch (\RuntimeException $e) { + echo $e->getMessage(); + die; + } + var_dump($result); + } + + public function sayHelloWithFunctionInvoke(string $functionName): void + { + try { + $result = (new \ReflectionFunction($functionName))->invoke(1, 2); + } catch (\RuntimeException $e) { + echo $e->getMessage(); + die; + } + var_dump($result); + } +} 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..6ce0f7ef0c --- /dev/null +++ b/tests/PHPStan/Rules/Exceptions/data/bug-9267.php @@ -0,0 +1,30 @@ +invokeArgs(new C, array()); + } + catch (FooException $e) { + print "CAUGHT FOO!\n"; + } +} + +function baz(\ReflectionMethod $r): void { + try { + $r->invoke(new C); + } + catch (FooException $e) { + print "CAUGHT FOO!\n"; + } +}