Skip to content

Commit

Permalink
Prevent overly greedy $scope->getType() calls in ImpossibleInstanceOf…
Browse files Browse the repository at this point in the history
…Rule
  • Loading branch information
staabm committed Dec 13, 2022
1 parent c760b1f commit fe523bd
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Rules/Classes/ImpossibleInstanceOfRule.php
Expand Up @@ -34,9 +34,6 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$instanceofType = $scope->getType($node);
$expressionType = $scope->getType($node->expr);

if ($node->class instanceof Node\Name) {
$className = $scope->resolveName($node->class);
$classType = new ObjectType($className);
Expand All @@ -50,13 +47,14 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s results in an error.',
$expressionType->describe(VerbosityLevel::typeOnly()),
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::typeOnly()),
))->build(),
];
}
}

$instanceofType = $scope->getType($node);
if (!$instanceofType instanceof ConstantBooleanType) {
return [];
}
Expand All @@ -78,15 +76,15 @@ public function processNode(Node $node, Scope $scope): array
return [
$addTip(RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s will always evaluate to false.',
$expressionType->describe(VerbosityLevel::typeOnly()),
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
)))->build(),
];
} elseif ($this->checkAlwaysTrueInstanceof) {
return [
$addTip(RuleErrorBuilder::message(sprintf(
'Instanceof between %s and %s will always evaluate to true.',
$expressionType->describe(VerbosityLevel::typeOnly()),
$scope->getType($node->expr)->describe(VerbosityLevel::typeOnly()),
$classType->describe(VerbosityLevel::getRecommendedLevelByType($classType)),
)))->build(),
];
Expand Down

0 comments on commit fe523bd

Please sign in to comment.