Skip to content

Commit abc79d5

Browse files
committed
Keep specifying types for CallLikes resolving to ConstantTypes
1 parent 29b9745 commit abc79d5

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ public function specifyTypesInCondition(
167167
return $this->create($exprNode, new ObjectWithoutClassType(), $context, false, $scope);
168168
}
169169
} elseif ($expr instanceof Node\Expr\BinaryOp\Identical) {
170-
if ($expr->left instanceof Expr\CallLike && $expr->right instanceof Expr\CallLike) {
171-
return new SpecifiedTypes();
172-
}
173-
174170
$expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr);
175171
if ($expressions !== null) {
176172
/** @var Expr $exprNode */
@@ -263,6 +259,12 @@ public function specifyTypesInCondition(
263259
);
264260
}
265261

262+
$exprLeftType = $scope->getType($expr->left);
263+
$exprRightType = $scope->getType($expr->right);
264+
if ($expr->left instanceof Expr\CallLike && $expr->right instanceof Expr\CallLike && !$exprLeftType instanceof ConstantType && !$exprRightType instanceof ConstantType) {
265+
return new SpecifiedTypes();
266+
}
267+
266268
if ($context->true()) {
267269
$type = TypeCombinator::intersect($scope->getType($expr->right), $scope->getType($expr->left));
268270
$leftTypes = $this->create($expr->left, $type, $context, false, $scope);
@@ -279,9 +281,6 @@ public function specifyTypesInCondition(
279281
return $leftTypes->unionWith($rightTypes);
280282
}
281283

282-
$exprLeftType = $scope->getType($expr->left);
283-
$exprRightType = $scope->getType($expr->right);
284-
285284
$types = null;
286285

287286
if (
@@ -333,10 +332,6 @@ public function specifyTypesInCondition(
333332
$context,
334333
);
335334
} elseif ($expr instanceof Node\Expr\BinaryOp\Equal) {
336-
if ($expr->left instanceof Expr\CallLike && $expr->right instanceof Expr\CallLike) {
337-
return new SpecifiedTypes();
338-
}
339-
340335
$expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr);
341336
if ($expressions !== null) {
342337
/** @var Expr $exprNode */
@@ -362,6 +357,9 @@ public function specifyTypesInCondition(
362357

363358
$leftType = $scope->getType($expr->left);
364359
$rightType = $scope->getType($expr->right);
360+
if ($expr->left instanceof Expr\CallLike && $expr->right instanceof Expr\CallLike && !$leftType instanceof ConstantType && !$rightType instanceof ConstantType) {
361+
return new SpecifiedTypes();
362+
}
365363

366364
$leftBooleanType = $leftType->toBoolean();
367365
if ($leftBooleanType instanceof ConstantBooleanType && $rightType instanceof BooleanType) {

tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public function testRule(): void
8080
'Call to method ImpossibleMethodCall\Foo::isSame() with *NEVER* and stdClass will always evaluate to false.',
8181
84,
8282
],
83+
[
84+
'Call to method ImpossibleMethodCall\Foo::isSame() with \'foo\' and \'foo\' will always evaluate to true.',
85+
101,
86+
],
8387
]);
8488
}
8589

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ public function doDolor(\stdClass $std1, \stdClass $std2)
9898
if ($this->isNotSame(self::createStdClass('b'), self::createStdClass('b'))) {
9999

100100
}
101-
$std3 = new \stdClass();
102-
if ($this->isSame($std3, self::createStdClass('c'))) {
101+
if ($this->isSame(self::returnFoo(), self::returnFoo())) {
103102

104103
}
105-
$std4 = new \stdClass();
106-
if ($this->isNotSame($std4, self::createStdClass('d'))) {
104+
if ($this->isNotSame(self::returnFoo(), self::returnFoo())) {
107105

108106
}
109107
}
@@ -118,4 +116,12 @@ public static function createStdClass(string $foo): \stdClass
118116
return new \stdClass();
119117
}
120118

119+
/**
120+
* @return 'foo'
121+
*/
122+
public static function returnFoo(): string
123+
{
124+
return 'foo';
125+
}
126+
121127
}

0 commit comments

Comments
 (0)