Skip to content

Commit c2aae08

Browse files
committed
Simplify Identical specification, add more tests
1 parent 95e510b commit c2aae08

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -303,24 +303,20 @@ public function specifyTypesInCondition(
303303
}
304304

305305
$identicalType = $scope->getType($expr);
306-
if ($identicalType instanceof ConstantBooleanType && $context->false()) {
306+
if ($identicalType instanceof ConstantBooleanType && !$context->null()) {
307307
$never = new NeverType();
308308
$contextForTypes = $identicalType->getValue() ? $context->negate() : $context;
309309
$leftTypes = $this->create($expr->left, $never, $contextForTypes, false, $scope);
310310
$rightTypes = $this->create($expr->right, $never, $contextForTypes, false, $scope);
311311
return $leftTypes->unionWith($rightTypes);
312312
}
313313

314-
if (
315-
$expr->left instanceof Expr\Variable || $expr->right instanceof Expr\Variable
316-
|| $expr->left instanceof Node\Scalar || $expr->right instanceof Node\Scalar
317-
) {
314+
if ($expr->left instanceof Expr\Variable || $expr->right instanceof Expr\Variable) {
318315
if ($context->true()) {
319316
$type = TypeCombinator::intersect($scope->getType($expr->right), $scope->getType($expr->left));
320317
$leftTypes = $this->create($expr->left, $type, $context, false, $scope);
321318
$rightTypes = $this->create($expr->right, $type, $context, false, $scope);
322319
return $leftTypes->unionWith($rightTypes);
323-
324320
}
325321

326322
return $this->create($expr->left, $leftType, $context, false, $scope)->normalize($scope)
@@ -445,10 +441,7 @@ public function specifyTypesInCondition(
445441
$leftTypes = $this->create($expr->left, $leftType, $context, false, $scope);
446442
$rightTypes = $this->create($expr->right, $rightType, $context, false, $scope);
447443

448-
if (
449-
$expr->left instanceof Expr\Variable || $expr->right instanceof Expr\Variable
450-
|| $expr->left instanceof Node\Scalar || $expr->right instanceof Node\Scalar
451-
) {
444+
if ($expr->left instanceof Expr\Variable || $expr->right instanceof Expr\Variable) {
452445
return $context->true()
453446
? $leftTypes->unionWith($rightTypes)
454447
: $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope));

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ public function testRule(): void
8888
'Call to method ImpossibleMethodCall\Foo::isNotSame() with \'foo\' and \'foo\' will always evaluate to false.',
8989
104,
9090
],
91+
[
92+
'Call to method ImpossibleMethodCall\Foo::isSame() with array{} and array{} will always evaluate to true.',
93+
113,
94+
],
95+
[
96+
'Call to method ImpossibleMethodCall\Foo::isNotSame() with array{} and array{} will always evaluate to false.',
97+
116,
98+
],
99+
[
100+
'Call to method ImpossibleMethodCall\Foo::isSame() with array{1, 3} and array{1, 3} will always evaluate to true.',
101+
119,
102+
],
103+
[
104+
'Call to method ImpossibleMethodCall\Foo::isNotSame() with array{1, 3} and array{1, 3} will always evaluate to false.',
105+
122,
106+
],
107+
[
108+
'Call to method ImpossibleMethodCall\Foo::isSame() with 1 and stdClass will always evaluate to false.',
109+
126,
110+
],
111+
[
112+
'Call to method ImpossibleMethodCall\Foo::isNotSame() with 1 and stdClass will always evaluate to true.',
113+
130,
114+
],
91115
]);
92116
}
93117

tests/PHPStan/Rules/Comparison/data/impossible-method-call.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,26 @@ public function doDolor(\stdClass $std1, \stdClass $std2)
109109
}
110110
if ($this->isNotSame(self::createStdClass('b')->foo, self::createStdClass('b')->foo)) {
111111

112+
}
113+
if ($this->isSame([], [])) {
114+
115+
}
116+
if ($this->isNotSame([], [])) {
117+
118+
}
119+
if ($this->isSame([1, 3], [1, 3])) {
120+
121+
}
122+
if ($this->isNotSame([1, 3], [1, 3])) {
123+
124+
}
125+
$std3 = new \stdClass();
126+
if ($this->isSame(1, $std3)) {
127+
128+
}
129+
$std4 = new \stdClass();
130+
if ($this->isNotSame(1, $std4)) {
131+
112132
}
113133
}
114134

0 commit comments

Comments
 (0)