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

Incorrect variable may not be defined #1865

Closed
1ed opened this issue Feb 2, 2019 · 4 comments
Closed

Incorrect variable may not be defined #1865

1ed opened this issue Feb 2, 2019 · 4 comments

Comments

@1ed
Copy link

1ed commented Feb 2, 2019

Summary of a problem or a feature request

"Variable $key might not be defined." reported incorrectly. The $key variable is a scalar bool value (not a reference) and it is assigned only once.

Code snippet that reproduces the problem

https://phpstan.org/r/a8eba03f-946a-426f-90af-7575659a6a9e

Expected output

Do not report this.

@ondrejmirtes
Copy link
Member

Hi, this is very analyser-unfriendly code. It wouldn't work in other languages either. I group similar issues here: https://github.com/phpstan/phpstan/milestone/12

The main problem is that the value of $a can change meanwhile. For example if it changes from false to true, you'll get "undefined variable" PHP error. Variable can change in many ways - it can be passed by reference somewhere, or it can be overwritten directly or for example by an extract() call.

I usually solve this with an isset: https://phpstan.org/r/1795fe9c-8125-4a8b-818a-682652ec3b08

@MacGritsch
Copy link

If extract() is not used in whole (analyzed) code and the variable is not passed to any other function (and even if it is passed, the analyzer should know if it is by reference) and not modified in code, the false positive error could be suppressed. I think that could cover more than 95%.
Otherwise the "solution" for most projects is to ignore that error completly :-/

@ondrejmirtes
Copy link
Member

Implemented: phpstan/phpstan-src@54284c8

ondrejmirtes added a commit that referenced this issue Dec 17, 2020
@github-actions
Copy link

github-actions bot commented May 4, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants