Skip to content

Commit 316f269

Browse files
committed
Scalars of the same type on both of Equal are delegated to Identical
1 parent 98c4d84 commit 316f269

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Analyser/MutatingScope.php

+28
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,20 @@ private function resolveType(Expr $node): Type
620620
) {
621621
return new ConstantBooleanType(true);
622622
}
623+
624+
$leftType = $this->getType($node->left);
625+
$rightType = $this->getType($node->right);
626+
627+
$stringType = new StringType();
628+
$integerType = new IntegerType();
629+
$floatType = new FloatType();
630+
if (
631+
($stringType->isSuperTypeOf($leftType)->yes() && $stringType->isSuperTypeOf($rightType)->yes())
632+
|| ($integerType->isSuperTypeOf($leftType)->yes() && $integerType->isSuperTypeOf($rightType)->yes())
633+
|| ($floatType->isSuperTypeOf($leftType)->yes() && $floatType->isSuperTypeOf($rightType)->yes())
634+
) {
635+
return $this->getType(new Expr\BinaryOp\Identical($node->left, $node->right));
636+
}
623637
}
624638

625639
if ($node instanceof Expr\BinaryOp\NotEqual) {
@@ -632,6 +646,20 @@ private function resolveType(Expr $node): Type
632646
) {
633647
return new ConstantBooleanType(false);
634648
}
649+
650+
$leftType = $this->getType($node->left);
651+
$rightType = $this->getType($node->right);
652+
653+
$stringType = new StringType();
654+
$integerType = new IntegerType();
655+
$floatType = new FloatType();
656+
if (
657+
($stringType->isSuperTypeOf($leftType)->yes() && $stringType->isSuperTypeOf($rightType)->yes())
658+
|| ($integerType->isSuperTypeOf($leftType)->yes() && $integerType->isSuperTypeOf($rightType)->yes())
659+
|| ($floatType->isSuperTypeOf($leftType)->yes() && $floatType->isSuperTypeOf($rightType)->yes())
660+
) {
661+
return $this->getType(new Expr\BinaryOp\NotIdentical($node->left, $node->right));
662+
}
635663
}
636664

637665
return new BooleanType();

tests/PHPStan/Analyser/data/equal.php

+34
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,37 @@ public static function createStdClass(): \stdClass
121121
}
122122

123123
}
124+
125+
class Baz
126+
{
127+
128+
public function doFoo(string $a, int $b, float $c): void
129+
{
130+
$nullableA = $a;
131+
if (rand(0, 1)) {
132+
$nullableA = null;
133+
}
134+
135+
assertType('bool', $a == $nullableA);
136+
assertType('bool', $a == 'a');
137+
assertType('true', 'a' == 'a');
138+
assertType('false', 'a' == 'b');
139+
140+
assertType('bool', $a != $nullableA);
141+
assertType('bool', $a != 'a');
142+
assertType('false', 'a' != 'a');
143+
assertType('true', 'a' != 'b');
144+
145+
assertType('bool', $b == 'a');
146+
assertType('bool', $a == 1);
147+
assertType('true', 1 == 1);
148+
assertType('false', 1 == 0);
149+
150+
assertType('bool', $c == 'a');
151+
assertType('bool', $c == 1);
152+
assertType('bool', $c == 1.2);
153+
assertType('true', 1.2 == 1.2);
154+
assertType('false', 1.2 == 1.3);
155+
}
156+
157+
}

0 commit comments

Comments
 (0)