Skip to content

Commit 6bb6d92

Browse files
VincentLangletstaabm
authored andcommitted
Improve loose comparison on numeric string
1 parent 684c599 commit 6bb6d92

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ public function isScalar(): TrinaryLogic
327327

328328
public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
329329
{
330-
if ($type->isString()->yes() && $type->isLowercaseString()->no()) {
330+
if (
331+
$type->isString()->yes()
332+
&& $type->isLowercaseString()->no()
333+
&& ($type->isNumericString()->no() || $this->isNumericString()->no())
334+
) {
331335
return new ConstantBooleanType(false);
332336
}
333337

src/Type/Accessory/AccessoryUppercaseStringType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ public function isScalar(): TrinaryLogic
327327

328328
public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
329329
{
330-
if ($type->isString()->yes() && $type->isUppercaseString()->no()) {
330+
if (
331+
$type->isString()->yes()
332+
&& $type->isUppercaseString()->no()
333+
&& ($type->isNumericString()->no() || $this->isNumericString()->no())
334+
) {
331335
return new ConstantBooleanType(false);
332336
}
333337

tests/PHPStan/Analyser/nsrt/loose-comparisons.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,16 @@ public function sayIntersection(
716716
int $intRange,
717717
): void
718718
{
719+
// https://3v4l.org/q8OP2
720+
assertType('true', '1e2' == '1E2');
721+
assertType('false', '1e2' === '1E2');
722+
719723
assertType('bool', '' == $upper);
720724
assertType('bool', '0' == $upper);
721725
assertType('false', 'a' == $upper);
722726
assertType('false', 'abc' == $upper);
723727
assertType('false', 'aBc' == $upper);
728+
assertType('bool', '1e2' == $upper);
724729
assertType('bool', strtoupper($s) == $upper);
725730
assertType('bool', strtolower($s) == $upper);
726731
assertType('bool', $upper == $lower);
@@ -729,6 +734,7 @@ public function sayIntersection(
729734
assertType('false', 'A' == $lower);
730735
assertType('false', 'ABC' == $lower);
731736
assertType('false', 'AbC' == $lower);
737+
assertType('bool', '1E2' == $lower);
732738
assertType('bool', strtoupper($s) == $lower);
733739
assertType('bool', strtolower($s) == $lower);
734740
assertType('bool', $lower == $upper);

0 commit comments

Comments
 (0)