Skip to content

Fix containsNull() for union with (null&T of mixed) - #6390

Merged
staabm merged 1 commit into
phpstan:2.2.xfrom
vojtech-dobes:fix-593c71fe
Sep 8, 2026
Merged

staabm merged 1 commit into
phpstan:2.2.xfrom
vojtech-dobes:fix-593c71fe

Conversation

@vojtech-dobes

@vojtech-dobes vojtech-dobes commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

When $var is union with null, but that null is part of intersection, TypeCombinator::containsNull currently doesn't detect this null, leading to false negative.

Fixes for example https://phpstan.org/r/1e3fa9bb-efec-4ba4-aec5-8d189e82c990.

@phpstan-bot

Copy link
Copy Markdown
Collaborator

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.

@vojtech-dobes
vojtech-dobes force-pushed the fix-593c71fe branch 2 times, most recently from b307c46 to 10cfb93 Compare September 7, 2026 07:48
@vojtech-dobes
vojtech-dobes changed the base branch from 2.3.x to 2.2.x September 7, 2026 07:49
{
if ($type instanceof UnionType) {
foreach ($type->getTypes() as $innerType) {
if ($innerType instanceof NullType) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the same check in line 186?

the change fixes the initial error, because $innerType is a IntersectionType T&null

@vojtech-dobes vojtech-dobes Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@staabm
staabm merged commit 9aa319a into phpstan:2.2.x Sep 8, 2026
489 of 496 checks passed
@staabm

staabm commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Thank you!

@vojtech-dobes
vojtech-dobes deleted the fix-593c71fe branch September 8, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants