Skip to content

ReflectionMethod with invokeArgs: Dead catch - exception is never thrown in the try block. #7719

@janbarasek

Description

@janbarasek

Bug report

When calling a method dynamically based on a name and a variable, it is not safe to check whether an exception will be thrown or not. Therefore, PhpStan cannot report the error that an exception will never be thrown, because it is not true.

Code snippet that reproduces the problem

https://phpstan.org/r/3a219790-5e24-4d7e-b245-cc9e9dbc510d

<?php declare(strict_types = 1);

class HelloWorld
{
	public function sayHello(Endpoint $endpoint, string $methodName): void
	{
		try {
			$methodResponse = (new \ReflectionMethod($endpoint, $methodName))->invokeArgs($endpoint, ['id' => 2]);
		} catch (\RuntimeException $e) { // Dead catch - RuntimeException is never thrown in the try block.
			echo $e->getMessage();
			die;
		}
		var_dump($methodResponse);
	}
}

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

Expected output

No error for ReflectionMethod with invokeArgs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions