class A {}
try {
echo "hello";
} catch (A $a) {}
should fail with InvalidCatch
A note about fixing occurences of InvalidCatch: InvalidCatch may be seen when a class implements an interface (my_interface_exception).
Zend PHP allows you to have catch (my_interface_exception $e) {}, and will catch an interface.
Aside: HHVM doesn't allow you to do that.
This can easily be solved in PHP7 by adding interface my_interface_exception extends Throwable if my_interface_exception isn't already throwable.
yeah, that problem is implicitly referenced in this test: https://github.com/vimeo/psalm/blob/fdff250c4ab2105a84867a98fc6d82d57e074f50/tests/TryCatchTest.php#L28-L37
Also, docblock suppression needs to be on the try keyword (also shown by the test above).
@muglug why does psalm fail with InvalidCatch when catching a random interface? Shouldn't that be allowed?
@ndench that interface should extend \Throwable. It's known issue, for example with GuzzleException (interface).
@muglug is it possible to suppress InvalidCatch by configuration? referencedClass does not work.
@ndench @Wirone I've just made both InvalidClass and InvalidCatch both configurable by class.
This can easily be solved in PHP7 by adding
interface my_interface_exception extends Throwableif my_interface_exception isn't already throwable.
My earlier comment was incorrect: classes/interfaces can't (EDIT: directly) implement throwable, that causes a PHP error. Classes can only (EDIT: indirectly implement Throwable if they) extend Error/Exception
Classes can implement Throwable (or interfaces extending that) if they also extend Exception/Error.
Yeah, the exception I allow via the config is for 5.6-compatible exception-like interfaces. But I very much hope it鈥檚 a dwindling need
@muglug great news! when will it be released?
There was a 2.x release the day after - sorry, should have pinged this issue.
Most helpful comment
@ndench @Wirone I've just made both
InvalidClassandInvalidCatchboth configurable by class.