Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
10 changes: 10 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionProperty;
use Throwable;
use Traversable;
Expand Down Expand Up @@ -4765,6 +4767,14 @@ private function getFunctionThrowPoint(

private function getMethodThrowPoint(MethodReflection $methodReflection, ParametersAcceptor $parametersAcceptor, MethodCall $methodCall, MutatingScope $scope): ?InternalThrowPoint
{
if (
$this->implicitThrows
&& in_array($methodReflection->getName(), ['invoke', 'invokeArgs'], true)
&& in_array($methodReflection->getDeclaringClass()->getName(), [ReflectionMethod::class, ReflectionFunction::class], true)
) {
return InternalThrowPoint::createImplicit($scope, $methodCall);
}

$normalizedMethodCall = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $methodCall);
if ($normalizedMethodCall !== null) {
foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicMethodThrowTypeExtensions() as $extension) {
Expand Down
21 changes: 20 additions & 1 deletion tests/PHPStan/Analyser/nsrt/bug-4821.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,26 @@ public function sayHello(): void
return;
} catch (\ReflectionException $e) {
assertVariableCertainty(TrinaryLogic::createYes(), $object);
assertVariableCertainty(TrinaryLogic::createMaybe(), $method);
assertVariableCertainty(TrinaryLogic::createNo(), $method);
}
}

public function sayHello2(): void
{
$method = rand(0, 1) ? 'nonExisting' : 'sayFoo';
try {
$object = new HelloWorld();
$method = new \ReflectionMethod($object, $method);
$method->invoke($object);
return;
} catch (\ReflectionException $e) {
assertVariableCertainty(TrinaryLogic::createYes(), $object);
assertVariableCertainty(TrinaryLogic::createYes(), $method);
}
}

public function sayFoo(): void
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,16 @@ public function testBug9568(): void
$this->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
{
Expand Down
56 changes: 56 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-7719.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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 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);
}
}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-9267.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug9267;

class FooException extends \Exception {}

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

function bar(\ReflectionMethod $r): void {
try {
$r->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";
}
}
Loading