Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Type/Php/ReflectionMethodInvokeMethodThrowTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodThrowTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use ReflectionMethod;
use Throwable;
use function in_array;

#[AutowiredService]
final class ReflectionMethodInvokeMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension
{

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(), ['invoke', 'invokeArgs'], true)
&& $methodReflection->getDeclaringClass()->getName() === ReflectionMethod::class;
}

public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
return new ObjectType(Throwable::class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-7719.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace Bug7719;

class Endpoint {
public function run(int $id): int {
return $id;
}
}

class HelloWorld
{
public function sayHello(Endpoint $endpoint, string $methodName): void
{
try {
$methodResponse = (new \ReflectionMethod($endpoint, $methodName))->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);
}
}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-9267.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Bug9267;

class FooException extends \Exception {}

function bar(\ReflectionMethod $r): void {
try {
$r->invokeArgs(new C, array());
}
catch (FooException $e) {
print "CAUGHT FOO!\n";
}
}

class C {
/** @return never */
public function test() {
throw new FooException("");
}
}
Loading