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

Variable inside loop might not be defined. #6830

Open
hocker1 opened this issue Mar 15, 2022 · 3 comments · May be fixed by phpstan/phpstan-src#3598
Open

Variable inside loop might not be defined. #6830

hocker1 opened this issue Mar 15, 2022 · 3 comments · May be fixed by phpstan/phpstan-src#3598
Labels

Comments

@hocker1
Copy link

hocker1 commented Mar 15, 2022

Code snippet here:

https://phpstan.org/r/37400131-6bac-49dd-9088-5fa3614957bf

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.

@ondrejmirtes ondrejmirtes added this to the Dependent types milestone Mar 15, 2022
@hemberger
Copy link

hemberger commented Jan 16, 2023

The thing that was so curious to me about this issue is that dumpType does 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! ❤️

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
Copy link
Contributor

@hocker1 After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
+ 3: Function test() returns void but does not have any side effects.
 14: If condition is always true.
 14: Variable $x might not be defined.
Full report
Line Error
3 Function test() returns void but does not have any side effects.
14 If condition is always true.
14 Variable $x might not be defined.

@phpstan-bot
Copy link
Contributor

@hemberger After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
+ 4: Function test() returns void but does not have any side effects.
 10: Dumped type: 'foo'
 10: Variable $foo might not be defined.
Full report
Line Error
4 Function test() returns void but does not have any side effects.
10 Dumped type: 'foo'
10 Variable $foo might not be defined.

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

Successfully merging a pull request may close this issue.

4 participants