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 Comparison*Rules #2099

Merged
merged 1 commit 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
11 changes: 4 additions & 7 deletions src/Rules/Comparison/ConstantLooseComparisonRule.php
Expand Up @@ -36,25 +36,22 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

if (!$nodeType->getValue()) {
return [
RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to false.',
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=',
$leftType->describe(VerbosityLevel::value()),
$rightType->describe(VerbosityLevel::value()),
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
))->build(),
];
} elseif ($this->checkAlwaysTrueLooseComparison) {
return [
RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to true.',
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=',
$leftType->describe(VerbosityLevel::value()),
$rightType->describe(VerbosityLevel::value()),
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
))->build(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Expand Up @@ -154,14 +154,14 @@ public function findSpecifiedType(
}
} elseif ($functionName === 'method_exists' && $argsCount >= 2) {
$objectType = $scope->getType($node->getArgs()[0]->value);
$methodType = $scope->getType($node->getArgs()[1]->value);

if ($objectType instanceof ConstantStringType
&& !$this->reflectionProvider->hasClass($objectType->getValue())
) {
return false;
}

$methodType = $scope->getType($node->getArgs()[1]->value);
if ($methodType instanceof ConstantStringType) {
if ($objectType instanceof ConstantStringType) {
$objectType = new ObjectType($objectType->getValue());
Expand Down
11 changes: 5 additions & 6 deletions src/Rules/Comparison/UsageOfVoidMatchExpressionRule.php
Expand Up @@ -20,12 +20,11 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$matchResultType = $scope->getType($node);
if (
$matchResultType->isVoid()->yes()
&& !$scope->isInFirstLevelStatement()
) {
return [RuleErrorBuilder::message('Result of match expression (void) is used.')->build()];
if (!$scope->isInFirstLevelStatement()) {
$matchResultType = $scope->getType($node);
if ($matchResultType->isVoid()->yes()) {
return [RuleErrorBuilder::message('Result of match expression (void) is used.')->build()];
}
}

return [];
Expand Down