Skip to content

Commit

Permalink
ConstantLooseComparisonRule - tip about treatPhpDocTypesAsCertain
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 29, 2023
1 parent 72472dd commit d892c34
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ parameters:

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
count: 1
count: 2
path: src/Rules/Comparison/ConstantLooseComparisonRule.php

-
Expand Down Expand Up @@ -515,7 +515,7 @@ parameters:

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantBooleanType is error\\-prone and deprecated\\. Use Type\\:\\:isTrue\\(\\) or Type\\:\\:isFalse\\(\\) instead\\.$#"
count: 1
count: 2
path: src/Rules/Comparison/StrictComparisonOfDifferentTypesRule.php

-
Expand Down
21 changes: 17 additions & 4 deletions src/Rules/Comparison/ConstantLooseComparisonRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,40 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}

$instanceofTypeWithoutPhpDocs = $scope->getNativeType($node);
if ($instanceofTypeWithoutPhpDocs instanceof ConstantBooleanType) {
return $ruleErrorBuilder;
}

return $ruleErrorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
};

if (!$nodeType->getValue()) {
return [
RuleErrorBuilder::message(sprintf(
$addTip(RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to false.',
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=',
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
))->build(),
)))->build(),
];
} elseif ($this->checkAlwaysTrueLooseComparison) {
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}

$errorBuilder = RuleErrorBuilder::message(sprintf(
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to true.',
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=',
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
));
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function testRule(): void
"Loose comparison using == between 0 and '1' will always evaluate to false.",
33,
],
[
'Loose comparison using != between 3 and 3 will always evaluate to false.',
48,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}

Expand Down Expand Up @@ -67,6 +72,11 @@ public function testRuleAlwaysTrue(): void
35,
'Remove remaining cases below this one and this error will disappear too.',
],
[
'Loose comparison using != between 3 and 3 will always evaluate to false.',
48,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}

Expand Down Expand Up @@ -140,6 +150,7 @@ public function dataTreatPhpDocTypesAsCertain(): iterable
[
'Loose comparison using == between 3 and 3 will always evaluate to true.',
14,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]];
}
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/loose-comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ public function doFooBar(): void

}

/**
* @param 3 $i
*/
public function doLiteralIntInPhpDoc(int $i)
{
if ($i != 3) {
throw new \LogicException();
}
}

}

0 comments on commit d892c34

Please sign in to comment.