Skip to content

Nothing is lower than null and object is never equal to null #4131

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

Merged
merged 1 commit into from
Jul 21, 2025

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Jul 20, 2025

Closes phpstan/phpstan#10719
Closes phpstan/phpstan#9141

$a < null is always false.
$a <= null is always when $a is an object.

It needs #4132 first.

@@ -13,11 +13,19 @@ trait UndecidedComparisonCompoundTypeTrait

public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($otherType->isNull()->yes() && $this->isObject()->yes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This add some logic in the trait but I didn't find another way except creating an ObjectComparisonCompoundTypeTrait and an ObjectComparisonTypeTrait.

Do you prefer to create two new traits ?

@VincentLanglet VincentLanglet marked this pull request as ready for review July 20, 2025 16:37
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -110,6 +110,10 @@ public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryL
return $otherType->isGreaterThan($this, $phpVersion);
}

if ($otherType->isObject()->yes()) {
Copy link
Member

Choose a reason for hiding this comment

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

The PR title says "Nothing is lower than null" but this change sounds to me like the opposite.

The method asks "is null smaller than object?" and you return "yes".

Maybe I'm running on fumes with only half the brain but I'd like this to be explained. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, I missed some explanation.

Nothing is lower then null so
null <= $anything is always true
$anything < null is always false

And an object cannot be equal to null so
null < $object is always true
$object <= null is always false

@VincentLanglet VincentLanglet changed the title Nothing is lower than null Nothing is lower than null and object is never equal to null Jul 21, 2025
@ondrejmirtes ondrejmirtes merged commit 09e30b4 into phpstan:2.1.x Jul 21, 2025
101 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

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.

NULL is greater than an object of type DateTime, according to phpstan Unreachable statement - nullable
3 participants