Skip to content

Commit 9e423bc

Browse files
staabmondrejmirtes
authored andcommitted
Prevent overly greedy $scope->getType() calls in Comparison*Rules
1 parent 93a741e commit 9e423bc

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/Rules/Comparison/ConstantLooseComparisonRule.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,22 @@ public function processNode(Node $node, Scope $scope): array
3636
return [];
3737
}
3838

39-
$leftType = $scope->getType($node->left);
40-
$rightType = $scope->getType($node->right);
41-
4239
if (!$nodeType->getValue()) {
4340
return [
4441
RuleErrorBuilder::message(sprintf(
4542
'Loose comparison using %s between %s and %s will always evaluate to false.',
4643
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=',
47-
$leftType->describe(VerbosityLevel::value()),
48-
$rightType->describe(VerbosityLevel::value()),
44+
$scope->getType($node->left)->describe(VerbosityLevel::value()),
45+
$scope->getType($node->right)->describe(VerbosityLevel::value()),
4946
))->build(),
5047
];
5148
} elseif ($this->checkAlwaysTrueLooseComparison) {
5249
return [
5350
RuleErrorBuilder::message(sprintf(
5451
'Loose comparison using %s between %s and %s will always evaluate to true.',
5552
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=',
56-
$leftType->describe(VerbosityLevel::value()),
57-
$rightType->describe(VerbosityLevel::value()),
53+
$scope->getType($node->left)->describe(VerbosityLevel::value()),
54+
$scope->getType($node->right)->describe(VerbosityLevel::value()),
5855
))->build(),
5956
];
6057
}

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ public function findSpecifiedType(
154154
}
155155
} elseif ($functionName === 'method_exists' && $argsCount >= 2) {
156156
$objectType = $scope->getType($node->getArgs()[0]->value);
157-
$methodType = $scope->getType($node->getArgs()[1]->value);
158157

159158
if ($objectType instanceof ConstantStringType
160159
&& !$this->reflectionProvider->hasClass($objectType->getValue())
161160
) {
162161
return false;
163162
}
164163

164+
$methodType = $scope->getType($node->getArgs()[1]->value);
165165
if ($methodType instanceof ConstantStringType) {
166166
if ($objectType instanceof ConstantStringType) {
167167
$objectType = new ObjectType($objectType->getValue());

src/Rules/Comparison/UsageOfVoidMatchExpressionRule.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ public function getNodeType(): string
2020

2121
public function processNode(Node $node, Scope $scope): array
2222
{
23-
$matchResultType = $scope->getType($node);
24-
if (
25-
$matchResultType->isVoid()->yes()
26-
&& !$scope->isInFirstLevelStatement()
27-
) {
28-
return [RuleErrorBuilder::message('Result of match expression (void) is used.')->build()];
23+
if (!$scope->isInFirstLevelStatement()) {
24+
$matchResultType = $scope->getType($node);
25+
if ($matchResultType->isVoid()->yes()) {
26+
return [RuleErrorBuilder::message('Result of match expression (void) is used.')->build()];
27+
}
2928
}
3029

3130
return [];

0 commit comments

Comments
 (0)