Skip to content

Calling methods from a catch inside a try/catch/finally block #5299

@Pnoexz

Description

@Pnoexz

Bug report

I'm getting a false positive for "variable might not be defined" when catching an application-level exception in a try/catch/finally block. It's very similar to #3302 but in their case, they weren't calling any function, just assigning a value to $a. Not only it can't detect public methods from the exception, it also can't access methods from the same class.

Code snippet that reproduces the problem

https://phpstan.org/r/9fa72d5e-9236-4d55-a931-fdea130a6aeb (reproduction steps in the comments).

This is only an issue if there is a finally block, as can be seen in this playground: https://phpstan.org/r/32dd9fd1-5aa9-4f92-b519-586e85a21193

Expected output

This isn't an actual issue, so it shouldn't be reported.

Did PHPStan help you today? Did it make you happy in any way?

Absolutely it did, as it has been for many years now. Thanks to you, I was able to put a number to what every developer in the project already knew: our codebase sucks. Now we are getting the much needed time to fix them all. We fixed all of them up to level 2, fixed the critical ones at level 5 (and baselined the not-so-critical ones), and gave up on the rest for now (around 8500, and counting). As a good note, all new projects start at 8 from the very beginning now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions