Fix containsNull() for union with (null&T of mixed) - #6390
Conversation
|
You've opened the pull request against the latest branch 2.3.x. PHPStan 2.3 is not going to be released for months. If your code is relevant on 2.2.x and you want it to be released sooner, please rebase your pull request and change its target to 2.2.x. |
b307c46 to
10cfb93
Compare
| { | ||
| if ($type instanceof UnionType) { | ||
| foreach ($type->getTypes() as $innerType) { | ||
| if ($innerType instanceof NullType) { |
There was a problem hiding this comment.
what about the same check in line 186?
the change fixes the initial error, because $innerType is a IntersectionType T&null
There was a problem hiding this comment.
@staabm I ran the test suite on code where only :186 is changed to isNull()->yes() check, and such code still fails. So this line :178 is definitely important.
I haven't changed :186 purely in order to be very conservative and not touch what I don't have to :). I'm happy to change it too if you see no adverse consequences.
|
Thank you! |
When
$varis union withnull, but thatnullis part of intersection,TypeCombinator::containsNullcurrently doesn't detect this null, leading to false negative.Fixes for example https://phpstan.org/r/1e3fa9bb-efec-4ba4-aec5-8d189e82c990.