Skip to content

Greater and smaller expressions combined with null in a BooleanOr are incorrectly specified #6672

@herndlm

Description

@herndlm

Bug report

E.g. $b > 17 || $b === null

Trying to narrow down comparison expressions like that (where the variable is int|null) seems to be making problems if combined with a BooleanOr to add e.g. a null type.

I tried debugging this, the only thing that I found out so far is that the Greater is transformed to a Smaller expression which results in a MixedType with subtracted types. And when that left type is unionized with the right type (null) then it looses the subtracted types somewhere, basically ending up with only a mixed and therefore not adding the expected IntegerRangeType of the left part.
So, I assume this is either a bug in the TypeSpecifier in regard to the subtractable MixedType or one in TypeCombinator::union (which calls compareTypesInUnion which calls intersectWithSubtractedType). I might continue debugging this. Maybe it can "simply" add back the NullType that it substracted on the left side somehow.
Also need to check how it behaves with Smaller, that might reveal more and be easier to debug actually..

The strict comparison error that is reported is also odd, but might be fixed if the specified types are. UPDATE: those are actually fine, the < includes null of course, my fault :)

Context: Noticed while implementing phpstan/phpstan-webmozart-assert#107

Btw. I did check and my previous adaptions (phpstan/phpstan-src@7abb7c9 and phpstan/phpstan-src@c6b5430) are not related, this happened before already ;)

Code snippet that reproduces the problem

https://phpstan.org/r/658e4af2-416b-4d3e-86cf-01197edae4c5

Expected output

No errors.

Did PHPStan help you today? Did it make you happy in any way?

Not in particular today, but I'm sure soon again :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions