Skip to content

Commit

Permalink
Understand == between ConstantScalarType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 27, 2022
1 parent 6bf335c commit 5c85200
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,10 @@ private function resolveEqualType(Type $leftType, Type $rightType): BooleanType
return $this->resolveIdenticalType($leftType, $rightType);
}

if ($leftType instanceof ConstantScalarType && $rightType instanceof ConstantScalarType) {
return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore
}

if ($leftType instanceof ConstantArrayType && $rightType instanceof ConstantArrayType) {
return $this->resolveConstantArrayTypeComparison($leftType, $rightType, fn ($leftValueType, $rightValueType): BooleanType => $this->resolveEqualType($leftValueType, $rightValueType));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/constant-array-type-identical.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function doFoo(string $s): void
assertType('false', [1] != [1]);
assertType('false', [1] == [2]);
assertType('true', [1] != [2]);
assertType('bool', [1] == ["1"]);
assertType('bool', [1] != ["1"]);
assertType('true', [1] == ["1"]);
assertType('false', [1] != ["1"]);

assertType('false', [1] === [2]);
assertType('false', [1] !== [1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public function testRule(): void
'Call to method ImpossibleMethodCall\Foo::isNotSame() with array{1, 3} and array{1, 3} will always evaluate to false.',
122,
],
[
'Call to method ImpossibleMethodCall\Foo::isSame() with array{\'a\', \'b\'} and array{1, 2} will always evaluate to false.',
139,
],
[
'Call to method ImpossibleMethodCall\Foo::isNotSame() with array{\'a\', \'b\'} and array{1, 2} will always evaluate to true.',
142,
],
[
'Call to method ImpossibleMethodCall\Foo::isSame() with \'\' and \'\' will always evaluate to true.',
174,
Expand Down

2 comments on commit 5c85200

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @herndlm This will most likely mean you can keep the eq asserts :)

@herndlm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep that's enough indeed. now only null == new stdClass() is left, but I'm unsure about that one

Please sign in to comment.