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

PHPStan doesn't detect variables in nested loop which will surely be initialized #2805

Closed
paljinov opened this issue Jan 4, 2020 · 4 comments

Comments

@paljinov
Copy link

paljinov commented Jan 4, 2020

PHPStan doesn't detect variables in nested loop which will surely be initialized.

Version being used:
PHPStan - PHP Static Analysis Tool 0.12.3

Code snippet that reproduces the problem

<?php

for ($i = 1; $i < 2; $i++) {
    for ($j = 1; $j < 2; $j++) {
    }
}

echo $i * $j;

Output

+------------------------------------------+
| Line | test.php                          |
+------------------------------------------+
| 8    | Variable $j might not be defined. |
+------------------------------------------+

 [ERROR] Found 1 error 

Expected output

 [OK] No errors
@muglug
Copy link
Contributor

muglug commented Jan 4, 2020

I just implemented this in Psalm – you could presumably PR something similar for PHPStan, though it may require #2806 to be fixed.

@ondrejmirtes
Copy link
Member

This is the place that needs fixing in PHPStan: https://github.com/phpstan/phpstan-src/blob/master/src/Analyser/NodeScopeResolver.php#L781-L846

The while loop handling already looks at the condition so it knows when it iterates at least once: https://github.com/phpstan/phpstan-src/blob/master/src/Analyser/NodeScopeResolver.php#L701-L702

@ondrejmirtes
Copy link
Member

This one is already fixed.

@github-actions
Copy link

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 Mar 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants