Skip to content

ReflectionMethod->invoke/->invokeArgs() does not assume exceptions can be thrown #9267

@thg2k

Description

@thg2k

Bug report

When using ReflectionMethod methods invoke() or invokeArgs(), PHPStan does not consider that exceptions can be thrown, resulting in the "dead catch" error. This happens with both instance methods and static methods (first parameter set to null).

This does apply for ReflectionFunction which correctly assumes that any exception can be thrown.

Tested on version 1.10.14 and with the online tool with bleeding edge option.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class FooException extends Exception {}

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

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

bar(new ReflectionMethod("C", "test"));

https://phpstan.org/r/9ec6f070-c552-40fa-bbdb-b0b9888906c0

Expected output

No errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions