Skip to content
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

Improve loose comparison on IntegerRange containing zero #3764

Merged
merged 1 commit into from
Jan 4, 2025

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jan 2, 2025

relevant 3v4l.org links, as I think its pretty weird

@staabm
Copy link
Contributor Author

staabm commented Jan 2, 2025

//cc @VincentLanglet fyi, as you implemented loose comparison on int ranges

Copy link
Contributor

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

At first sight, it seems ok.

The recent discovery I made about loose comparison was the fact that <= and >= behavior changed with the PHP Version. But I don't remember exactly the cases where the behavior changed base on PHP version and if it impacts your current PR.

I tried to implements it with #3484 / #3485 but didn't got a good enough solution for ondrej to merge it. I'm not against your opinion on it @staabm. Since you implemented compareConstantScalars you might have advice about how to rework this method.

@staabm staabm marked this pull request as ready for review January 2, 2025 16:16
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@@ -315,6 +315,16 @@ public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryL
$maxIsSmaller = (new ConstantIntegerType($this->max))->isSmallerThan($otherType, $phpVersion);
}

// 0 can have different results in contrast to the interval edges, see https://3v4l.org/iGoti
Copy link
Contributor

Choose a reason for hiding this comment

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

This is expected, at least for me, better to document the -1 > null case.

I belive -1 > null is somethings that can be even fixed in php-src, as I would expect null to be compared as 0.

Copy link
Contributor Author

@staabm staabm Jan 2, 2025

Choose a reason for hiding this comment

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

feel free to open a php-src bug report. I guess they won't fix it because of BC concerns.

as it stands right now in PHPStan we should reflect php-src behaviour and don't invent our own rules for edge cases

Copy link
Contributor

Choose a reason for hiding this comment

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

That might be a good reason to report >, >=, <, <= operator with nullable integer in phpstan-strict-rule no ?
https://phpstan.org/r/ed5e293c-721b-4b14-b9ea-aa42876f8a2c

@staabm
Copy link
Contributor Author

staabm commented Jan 2, 2025

At first sight, it seems ok.

thanks for the review

The recent discovery I made about loose comparison was the fact that <= and >= behavior changed with the PHP Version. But I don't remember exactly the cases where the behavior changed base on PHP version and if it impacts your current PR.

I tried to implements it with #3484 / #3485 but didn't got a good enough solution for ondrej to merge it. I'm not against your opinion on it @staabm. Since you implemented compareConstantScalars you might have advice about how to rework this method.

had a quick look and it looks like this is a PHP 7.x only edge case.
as we have so many open PRs, this case feels like a low prio thing which from my POV, you should not spent time on.

low prio edge cases are pretty frustrating - especially when a lot of changes are needed - as we don't have enough resources to get them merged. I think your time is better spent somewhere else.

thats just my opinion though

@staabm
Copy link
Contributor Author

staabm commented Jan 4, 2025

conflicts resolved

@ondrejmirtes ondrejmirtes merged commit 44e2839 into phpstan:2.1.x Jan 4, 2025
422 of 424 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the loose-bool branch January 4, 2025 14:12
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.

5 participants