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 positives after "continue" or rethrown exception #6342

Open
discordier opened this issue Aug 19, 2021 · 1 comment · May be fixed by #7688
Open

False positives after "continue" or rethrown exception #6342

discordier opened this issue Aug 19, 2021 · 1 comment · May be fixed by #7688

Comments

@discordier
Copy link
Contributor

discordier commented Aug 19, 2021

When we define a variable within a try catch within a loop and then either continue the loop or rethrow the catched exception, psalm thinks the variable might be unset.

I fail to see any path that could lead to the variable being undefined.

Reproducer for global scope:
https://psalm.dev/r/fd6cc07cc7

Reproducer for function scope:
https://psalm.dev/r/6f91712fe8

@psalm-github-bot
Copy link

psalm-github-bot bot commented Aug 19, 2021

I found these snippets:

https://psalm.dev/r/fd6cc07cc7
<?php

function getData(int $test): array {
    if (rand(0, $test) > 5) {
        throw new \RuntimeException('Help', rand(0, 10));
    }
    return [];
}

$array = [];
foreach ([0, 1, 2] as $test) {
    try {
        $data = getData($test);
    } catch (\RuntimeException $exception) {
        if ($exception->getCode() > 5) {
            continue;
        }
        throw $exception;
    }
    // Why is data possibly undefined? There is no way to reach here 
    $array[] = $data;
}

if (empty($array)) {
    echo 'empty';
}
Psalm output (using commit 9e1f7ad):

INFO: PossiblyUndefinedGlobalVariable - 21:16 - Possibly undefined global variable $data defined in try block
https://psalm.dev/r/6f91712fe8
<?php

function getData(int $test): array {
    if (rand(0, $test) > 5) {
        throw new \RuntimeException('Help', rand(0, 10));
    }
    return [];
}

function test(): void {
    $array = [];
    foreach ([0, 1, 2] as $test) {
        try {
            $data = getData($test);
        } catch (\RuntimeException $exception) {
            if ($exception->getCode() > 5) {
                continue;
            }
            throw $exception;
        }
        // Why is data possibly undefined? There is no way to reach here 
        $array[] = $data;
    }

    if (empty($array)) {
        echo 'empty';
    }
}
Psalm output (using commit 9e1f7ad):

INFO: PossiblyUndefinedVariable - 22:20 - Possibly undefined variable $data defined in try block

@AndrolGenhald AndrolGenhald linked a pull request Feb 18, 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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants