Thanks to visit codestin.com
Credit goes to github.com

Skip to content

False positive "Variable might not be defined" in try/catch for loops? #6833

@xorik

Description

@xorik

Bug report

If I have foreach loop in try/catch block, I got false positive error: Variable might not be defined. for the iterated value in the catch block.

Why I think it's false positive:

  • If collection is empty, catch block is never triggered
  • If collection is not empty $file variable is always set in the catch block

Code snippet that reproduces the problem

https://phpstan.org/r/0271c658-e5d5-421f-a88c-f955eae727c6

Expected output

No error

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

Yep, it helped me to find some issues after updating code to php8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions