-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathConstantLooseComparisonRule.php
90 lines (74 loc) · 2.79 KB
/
ConstantLooseComparisonRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Parser\LastConditionVisitor;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
/**
* @implements Rule<Node\Expr\BinaryOp>
*/
final class ConstantLooseComparisonRule implements Rule
{
public function __construct(
private bool $treatPhpDocTypesAsCertain,
private bool $reportAlwaysTrueInLastCondition,
private bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return Node\Expr\BinaryOp::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node instanceof Node\Expr\BinaryOp\Equal && !$node instanceof Node\Expr\BinaryOp\NotEqual) {
return [];
}
$nodeType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node);
if (!$nodeType->isTrue()->yes() && !$nodeType->isFalse()->yes()) {
return [];
}
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $ruleErrorBuilder;
}
$instanceofTypeWithoutPhpDocs = $scope->getNativeType($node);
if ($instanceofTypeWithoutPhpDocs->isTrue()->yes() || $instanceofTypeWithoutPhpDocs->isFalse()->yes()) {
return $ruleErrorBuilder;
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $ruleErrorBuilder;
}
return $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
};
if ($nodeType->isFalse()->yes()) {
return [
$addTip(RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to false.',
$node->getOperatorSigil(),
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
)))->identifier(sprintf('%s.alwaysFalse', $node instanceof Node\Expr\BinaryOp\Equal ? 'equal' : 'notEqual'))->build(),
];
}
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Loose comparison using %s between %s and %s will always evaluate to true.',
$node->getOperatorSigil(),
$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.');
}
$errorBuilder->identifier(sprintf('%s.alwaysTrue', $node instanceof Node\Expr\BinaryOp\Equal ? 'equal' : 'notEqual'));
return [$errorBuilder->build()];
}
}