Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive for undefined variable #5123

Open
zerkms opened this issue Jan 28, 2021 · 1 comment · May be fixed by #7688
Open

False positive for undefined variable #5123

zerkms opened this issue Jan 28, 2021 · 1 comment · May be fixed by #7688
Labels
bug hard problems Problems without an obvious easy solution

Comments

@zerkms
Copy link
Contributor

zerkms commented Jan 28, 2021

https://psalm.dev/r/1fcfcf14b9

What's interesting is that it's not reproducible if not inside a class method.

Code explanation: code is heavily stripped, but overall shape is like that. $fp under normal conditions is closed by a third-party library I don'e have control over, hence unset($fp) only in the main try flow.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1fcfcf14b9
<?php

class C
{
    public function consume(): array
    {
        $retries = 1;

        while (true) {
            try {
                $fp = fopen('/path/to/file', 'rb');

                unset($fp);

                return [];
            } catch (Throwable $e) {
                if ($retries++ >= 10) {
                    throw $e;
                }

                sleep(1);
            } finally {
                if (isset($fp) && is_resource($fp)) {
                    fclose($fp);
                    unset($fp);
                }
            }
        }

        return [];
    }
}
Psalm output (using commit 5759472):

ERROR: UndefinedVariable - 23:27 - Cannot find referenced variable $fp

@muglug muglug added bug hard problems Problems without an obvious easy solution labels Jan 28, 2021
@AndrolGenhald AndrolGenhald linked a pull request Feb 17, 2022 that will close this issue
@weirdan weirdan linked a pull request Feb 20, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug hard problems Problems without an obvious easy solution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants