Improve loose comparison on integer#3748
Merged
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom Dec 21, 2024
Merged
Conversation
staabm
commented
Dec 21, 2024
| assertType('false', 'a' != 'a'); | ||
| assertType('true', 'a' != 'b'); | ||
|
|
||
| assertType('bool', $b == 'a'); |
Contributor
Author
There was a problem hiding this comment.
moved this case into the loose-comparison-* files, as it depends on php version
staabm
commented
Dec 21, 2024
|
|
||
| if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) { | ||
| // @phpstan-ignore equal.notAllowed, equal.invalid | ||
| // @phpstan-ignore equal.notAllowed, equal.invalid, equal.alwaysFalse |
Contributor
Author
There was a problem hiding this comment.
ignores
------ -------------------------------------------------------------------------------------------------------------
Line src/Type/Traits/ConstantScalarTypeTrait.php (in context of class PHPStan\Type\Constant\ConstantIntegerType)
------ -------------------------------------------------------------------------------------------------------------
62 Loose comparison using == between int and array{} will always evaluate to false.
🪪 equal.alwaysFalse
------ -------------------------------------------------------------------------------------------------------------
staabm
commented
Dec 21, 2024
Comment on lines
+179
to
+192
| if (PHP_VERSION_ID >= 80000) { | ||
| $expectedErrors = array_merge($expectedErrors, [ | ||
| [ | ||
| "Loose comparison using == between '13foo' and int<10, 20> will always evaluate to false.", | ||
| 29, | ||
| 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', | ||
| ], | ||
| [ | ||
| "Loose comparison using == between int<10, 20> and '13foo' will always evaluate to false.", | ||
| 30, | ||
| 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', | ||
| ], | ||
| ]); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
Collaborator
|
This pull request has been marked as ready for review. |
staabm
commented
Dec 21, 2024
| @@ -139,6 +140,18 @@ public function isScalar(): TrinaryLogic | |||
|
|
|||
| public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType | |||
Contributor
Author
There was a problem hiding this comment.
I think we have a new todo for PHPStan 3.x: adjust all Type methods which depend on PhpVersion and turn them into using PhpVersions
Member
There was a problem hiding this comment.
Yeah, this should be possible. It's possible to make PhpVersions from PhpVersion (without losing precision), but not vice-versa.
So places that only have PhpVersion could still call these methods by transforming it into PhpVersions first.
Member
|
Awesome, thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
see https://www.php.net/manual/en/types.comparisons.php
see https://3v4l.org/395hr