Skip to content

Commit

Permalink
Prevent overly greedy $scope->getType() calls in Comparison*Rules
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 13, 2022
1 parent 0f7d3d6 commit e3873d8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
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

0 comments on commit e3873d8

Please sign in to comment.