Skip to content

Commit d5a8d9e

Browse files
staabmondrejmirtes
authored andcommitted
Prevent overly greedy $scope->getType() calls in ImpossibleInstanceOfRule
1 parent 0f7d3d6 commit d5a8d9e

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/Rules/Classes/ImpossibleInstanceOfRule.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ public function getNodeType(): string
3434

3535
public function processNode(Node $node, Scope $scope): array
3636
{
37-
$instanceofType = $scope->getType($node);
38-
$expressionType = $scope->getType($node->expr);
39-
4037
if ($node->class instanceof Node\Name) {
4138
$className = $scope->resolveName($node->class);
4239
$classType = new ObjectType($className);
@@ -50,13 +47,14 @@ public function processNode(Node $node, Scope $scope): array
5047
return [
5148
RuleErrorBuilder::message(sprintf(
5249
'Instanceof between %s and %s results in an error.',
53-
$expressionType->describe(VerbosityLevel::typeOnly()),
50+
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
5451
$classType->describe(VerbosityLevel::typeOnly()),
5552
))->build(),
5653
];
5754
}
5855
}
5956

57+
$instanceofType = $scope->getType($node);
6058
if (!$instanceofType instanceof ConstantBooleanType) {
6159
return [];
6260
}
@@ -78,15 +76,15 @@ public function processNode(Node $node, Scope $scope): array
7876
return [
7977
$addTip(RuleErrorBuilder::message(sprintf(
8078
'Instanceof between %s and %s will always evaluate to false.',
81-
$expressionType->describe(VerbosityLevel::typeOnly()),
79+
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
8280
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
8381
)))->build(),
8482
];
8583
} elseif ($this->checkAlwaysTrueInstanceof) {
8684
return [
8785
$addTip(RuleErrorBuilder::message(sprintf(
8886
'Instanceof between %s and %s will always evaluate to true.',
89-
$expressionType->describe(VerbosityLevel::typeOnly()),
87+
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
9088
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
9189
)))->build(),
9290
];

0 commit comments

Comments
 (0)