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

Psalter --issues=UnusedVariable tries to fix PossiblyUndefinedVariable #9359

Closed
edsrzf opened this issue Feb 21, 2023 · 2 comments · Fixed by #9400
Closed

Psalter --issues=UnusedVariable tries to fix PossiblyUndefinedVariable #9359

edsrzf opened this issue Feb 21, 2023 · 2 comments · Fixed by #9400

Comments

@edsrzf
Copy link
Contributor

edsrzf commented Feb 21, 2023

If I start with this code: (psalm.dev link)

<?php

function foo(bool $b): void {
    if ($b) {
        $v = 'hi';
    }

    if ($b) {
        echo $v;
    }
}

it correctly flags $v as possibly undefined. If I run psalter --issues=UnusedVariable, it alters the file to:

<?php

function foo(bool $b): void {
    if ($b) {
    }

    if ($b) {
        echo $v;
    }
}

This is pretty unexpected, as:

  1. It's altered the code incorrectly. Now the variable is definitely undefined!
  2. It's tried to fix a PossiblyUndefinedVariable issue, when I only told it to fix UnusedVariable issues.
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/7dad7e88bc
<?php

function foo(bool $b): void {
    if ($b) {
        $v = 'hi';
    }

    if ($b) {
        echo $v;
    }
}
Psalm output (using commit 28db105):

INFO: PossiblyUndefinedVariable - 9:14 - Possibly undefined variable $v, first seen on line 5

INFO: MixedArgument - 9:14 - Argument 1 of echo cannot be mixed, expecting string

@orklah
Copy link
Collaborator

orklah commented Feb 23, 2023

What I can say with certitude is that removing this condition:


stops Psalter from fixing the issue, so it's made here. However, this block only triggers on UnusedVariable.

I tried to find what's going on exactly but I couldn't.

What I could see is that Psalm will only try to emit the UnusedVariable when in Psalter mode. That means there's something wrongly triggering this issue and then Psalter tries to fix it on the go

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.

3 participants