RedundantCondition and superglobals when saved to variables #10474
-
I'm working on enabling RedundantCondition checks and I've run into an issue with superglobals, specifically In the example, psalm is reporting that the value of As my example shows, there are some workarounds that I can use. I expect I am just not understanding how psalm handles superglobals. I would appreciate some clarification on why psalm behaves this way. Or if there are other suggestions on how to handle this case that would be appreciated. In trying to see if this has been discussed before, it looks like the type of superglobals was changed a little over a year ago here: #8473. Does there need to be more work done here to handle the possibly null cases? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
My understanding is that $_GET and $_POST would not be populated with null value, the closest being an '' in the case of ?value or ?value= hence the types being defined as they are. In the second example isset is acting as an array_key_exists, not actually checking for a null value. It's possible to set a value yourself which is null, but Psalm will report it at that point and continue to use the hinted type elsewhere. In this case Psalm is trying to provide safety for the 99%, and modifying superglobals is frowned upon I don't think your workarounds are workarounds, they are better code. as they won't trigger any undefined array key warnings. Hope that clears things up! |
Beta Was this translation helpful? Give feedback.
My understanding is that $_GET and $_POST would not be populated with null value, the closest being an '' in the case of ?value or ?value= hence the types being defined as they are.
In the second example isset is acting as an array_key_exists, not actually checking for a null value.
In the third the null coalesce provides the null value in the case of the key not existing and so it's doing the same thing as the second indirectly.
It's possible to set a value yourself which is null, but Psalm will report it at that point and continue to use the hinted type elsewhere. In this case Psalm is trying to provide safety for the 99%, and modifying superglobals is frowned upon
I don't think your wo…