You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The variable is "always true" and "might not be defined" at the same time, which doesn't make any sense to me. Without the "foreach" phpstan handles the code correctly.
P.S.: Thank You for awesome tool (phpstan), which helps me with refactoring legacy code a lot.
The thing that was so curious to me about this issue is that dumpTypedoes infer the correct type in a lot of cases, but the "might not be defined" warning is still emitted (I misunderstood dumpType -- it seems that certainty of the type doesn't guarantee that it's defined).
I have a lot of logic like this in templating code, which can't always be reorganized to circumvent the warning. I'm crossing my fingers for a fix some day from one of our amazing maintainers! ❤️
@@ @@-10: Dumped type: 'foo'-10: Variable $foo might not be defined.+ 4: Function test() returns void but does not have any side effects.+10: Dumped type: 'foo'
Full report
Line
Error
4
Function test() returns void but does not have any side effects.
Activity
hemberger commentedon Jan 16, 2023
The thing that was so curious to me about this issue is that(I misunderstooddumpTypedoes infer the correct type in a lot of cases, but the "might not be defined" warning is still emitteddumpType-- it seems that certainty of the type doesn't guarantee that it's defined).I have a lot of logic like this in templating code, which can't always be reorganized to circumvent the warning. I'm crossing my fingers for a fix some day from one of our amazing maintainers! ❤️
I'll add my example here, though it is much the same as the original:
https://phpstan.org/r/96490bfa-916f-487b-a915-30c752ef65d3
phpstan-bot commentedon Sep 24, 2024
@hocker1 After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
Full report
Function test() returns void but does not have any side effects.If condition is always true.Variable $x might not be defined.phpstan-bot commentedon Sep 24, 2024
@hemberger After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:
Full report
Function test() returns void but does not have any side effects.Dumped type: 'foo'Variable $foo might not be defined.phpstan-bot commentedon Mar 4, 2026
@hemberger After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:
Full report
Function test() returns void but does not have any side effects.Dumped type: 'foo'Fix phpstan/phpstan#6830: Variable inside loop might not be defined
Fix variable defined before loop reported as maybe undefined inside loop
Fix phpstan/phpstan#6830: Variable inside loop might not be defined
hemberger commentedon Mar 17, 2026
This has been fixed for my use case in the 2.1.41 release. Thank you for all your hard work!
Union one-sided conditional holders that share a target and guard set…
github-actions commentedon Oct 13, 2026
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.