Skip to content

Commit 5c85200

Browse files
committed
Understand == between ConstantScalarType
1 parent 6bf335c commit 5c85200

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/Analyser/MutatingScope.php

+4
Original file line numberDiff line numberDiff line change
@@ -2358,6 +2358,10 @@ private function resolveEqualType(Type $leftType, Type $rightType): BooleanType
23582358
return $this->resolveIdenticalType($leftType, $rightType);
23592359
}
23602360

2361+
if ($leftType instanceof ConstantScalarType && $rightType instanceof ConstantScalarType) {
2362+
return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore
2363+
}
2364+
23612365
if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) {
23622366
return $this->resolveConstantArrayTypeComparison($leftType, $rightType, fn ($leftValueType, $rightValueType): BooleanType => $this->resolveEqualType($leftValueType, $rightValueType));
23632367
}

tests/PHPStan/Analyser/data/constant-array-type-identical.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public function doFoo(string $s): void
1515
assertType('false', [1] != [1]);
1616
assertType('false', [1] == [2]);
1717
assertType('true', [1] != [2]);
18-
assertType('bool', [1] == ["1"]);
19-
assertType('bool', [1] != ["1"]);
18+
assertType('true', [1] == ["1"]);
19+
assertType('false', [1] != ["1"]);
2020

2121
assertType('false', [1] === [2]);
2222
assertType('false', [1] !== [1]);

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleEqualsTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public function testRule(): void
9292
'Call to method ImpossibleMethodCall\Foo::isNotSame() with array{1, 3} and array{1, 3} will always evaluate to false.',
9393
122,
9494
],
95+
[
96+
'Call to method ImpossibleMethodCall\Foo::isSame() with array{\'a\', \'b\'} and array{1, 2} will always evaluate to false.',
97+
139,
98+
],
99+
[
100+
'Call to method ImpossibleMethodCall\Foo::isNotSame() with array{\'a\', \'b\'} and array{1, 2} will always evaluate to true.',
101+
142,
102+
],
95103
[
96104
'Call to method ImpossibleMethodCall\Foo::isSame() with \'\' and \'\' will always evaluate to true.',
97105
174,

0 commit comments

Comments
 (0)