-
Notifications
You must be signed in to change notification settings - Fork 471
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 integer #3748
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType | |
} | ||
|
||
if ($type->isConstantArray()->yes() && $type->isIterableAtLeastOnce()->no()) { | ||
// @phpstan-ignore equal.notAllowed, equal.invalid | ||
// @phpstan-ignore equal.notAllowed, equal.invalid, equal.alwaysFalse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ignores
|
||
return new ConstantBooleanType($this->getValue() == []); // phpcs:ignore | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,7 +135,7 @@ public static function createStdClass(): \stdClass | |
class Baz | ||
{ | ||
|
||
public function doFoo(string $a, int $b, float $c): void | ||
public function doFoo(string $a, float $c): void | ||
{ | ||
$nullableA = $a; | ||
if (rand(0, 1)) { | ||
|
@@ -152,7 +152,6 @@ public function doFoo(string $a, int $b, float $c): void | |
assertType('false', 'a' != 'a'); | ||
assertType('true', 'a' != 'b'); | ||
|
||
assertType('bool', $b == 'a'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved this case into the loose-comparison-* files, as it depends on php version |
||
assertType('bool', $a == 1); | ||
assertType('true', 1 == 1); | ||
assertType('false', 1 == 0); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use function array_merge; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
|
@@ -142,7 +143,7 @@ public function testTreatPhpDocTypesAsCertain(bool $treatPhpDocTypesAsCertain, a | |
|
||
public function testBug11694(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/bug-11694.php'], [ | ||
$expectedErrors = [ | ||
[ | ||
'Loose comparison using == between 3 and int<10, 20> will always evaluate to false.', | ||
17, | ||
|
@@ -173,6 +174,24 @@ public function testBug11694(): void | |
27, | ||
'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%</>.', | ||
], | ||
]; | ||
|
||
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%</>.', | ||
], | ||
]); | ||
} | ||
Comment on lines
+179
to
+192
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
$expectedErrors = array_merge($expectedErrors, [ | ||
[ | ||
'Loose comparison using == between \' 3\' and int<10, 20> will always evaluate to false.', | ||
32, | ||
|
@@ -204,6 +223,8 @@ public function testBug11694(): void | |
'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%</>.', | ||
], | ||
]); | ||
|
||
$this->analyse([__DIR__ . '/data/bug-11694.php'], $expectedErrors); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have a new todo for PHPStan 3.x: adjust all
Type
methods which depend onPhpVersion
and turn them into usingPhpVersions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.