Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent overly greedy $scope->getType() calls in ImpossibleInstanceOfRule #2098

Merged
merged 2 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Rules/Classes/ExistingClassInInstanceOfRule.php
Expand Up @@ -77,9 +77,10 @@ public function processNode(Node $node, Scope $scope): array
}

$classReflection = $this->reflectionProvider->getClass($name);
$expressionType = $scope->getType($node->expr);

if ($classReflection->isTrait()) {
$expressionType = $scope->getType($node->expr);

$errors[] = RuleErrorBuilder::message(sprintf(
'Instanceof between %s and trait %s will always evaluate to false.',
$expressionType->describe(VerbosityLevel::typeOnly()),
Expand Down
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