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

Foreach overwrites $variable with its value variable. - false positive? #194

Open
lulco opened this issue Nov 22, 2022 · 2 comments
Open

Comments

@lulco
Copy link

lulco commented Nov 22, 2022

I can't reproduce this issue on https://phpstan.org/try, is the rule overwriteVariablesWithLoop set to true there?

If I use foreach twice I got error on second run Foreach overwrites $var with its value variable. But $var is local variable here so it shouldn't be reported imho.

foreach ($variable as $var) {
    // do something
}

foreach ($variable as $var) {
    // do something else
}
@schlndh
Copy link

schlndh commented May 12, 2023

I found this possibly related issue: https://phpstan.org/r/ac2b69f1-1e28-4b22-a3c9-7171bae6c540

Surprisingly, changing it to if (count($arr) > 0), or removing the if and adding non-empty-array<object> $arr makes the issue disappear.

@mvorisek
Copy link

I am facing the same issue. It is definitely a bug.

Repro: https://phpstan.org/r/c4a83ee0-e49b-4ac8-8222-17203cd19da2

The bug is present under very specific condition:

  • count($taskIds) === 0 between the same foreach loops
  • with return statement between
  • and the variable $taskIds has to be within the 1st loop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants