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

Not null affectation does not define variable #4662

Open
alterphp opened this issue Mar 4, 2021 · 6 comments
Open

Not null affectation does not define variable #4662

alterphp opened this issue Mar 4, 2021 · 6 comments
Labels

Comments

@alterphp
Copy link

alterphp commented Mar 4, 2021

Bug report

In a if statement, I first check that affectation of a variable is not NULL, and then try to use the variable in a second part of the condition. PHPStan output an error like Undefined variable: $affectedVar.

It could be related to #4357.

Code snippet that reproduces the problem

https://phpstan.org/r/43c76836-a07b-40ef-826f-e72c889b9024

Expected output

In the above example, PHPStan should not output that $programStartDate is undefined at line 22.

@ondrejmirtes
Copy link
Member

Yeah, this is problematic, but you can work around this with arguably more readable code: https://phpstan.org/r/2fa5189a-4e20-4f7a-8cad-6f83c1fff0c7

@alterphp alterphp changed the title Not null affectation does not more define variable Not null affectation does not define variable Mar 4, 2021
@alterphp
Copy link
Author

alterphp commented Mar 4, 2021

Actually I was pretty sure it worked before, but I cannot make it work from v0.12.60. Maybe I just dreamt about it ;-)

Thanks for hard work!

@phpstan-bot
Copy link
Contributor

@alterphp After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
+ 8: Property Foo::$programStartDate is never written, only read.
 22: Undefined variable: $programStartDate
Full report
Line Error
8 Property Foo::$programStartDate is never written, only read.
22 Undefined variable: $programStartDate

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+8: Property Foo::$programStartDate is never written, only read.
Full report
Line Error
8 Property Foo::$programStartDate is never written, only read.

@phpstan-bot
Copy link
Contributor

@alterphp After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
+ 8: Property Foo::$programStartDate is never written, only read.
 22: Undefined variable: $programStartDate
Full report
Line Error
8 Property Foo::$programStartDate is never written, only read.
22 Undefined variable: $programStartDate

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+8: Property Foo::$programStartDate is never written, only read.
Full report
Line Error
8 Property Foo::$programStartDate is never written, only read.

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

No branches or pull requests

3 participants