forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberComparisonOperatorsConstantConditionRule.php
More file actions
109 lines (94 loc) · 3.11 KB
/
NumberComparisonOperatorsConstantConditionRule.php
File metadata and controls
109 lines (94 loc) · 3.11 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\VerbosityLevel;
use function get_class;
use function sprintf;
/**
* @implements Rule<Node\Expr\BinaryOp>
*/
#[RegisteredRule(level: 4)]
final class NumberComparisonOperatorsConstantConditionRule implements Rule
{
public function __construct(
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')]
private bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return BinaryOp::class;
}
public function processNode(
Node $node,
Scope $scope,
): array
{
if (
!$node instanceof BinaryOp\Greater
&& !$node instanceof BinaryOp\GreaterOrEqual
&& !$node instanceof BinaryOp\Smaller
&& !$node instanceof BinaryOp\SmallerOrEqual
) {
return [];
}
if (TraitContextHelper::isBinaryOpDependentOnTraitContext($scope, $node->left, $node->right)) {
return [];
}
$exprType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node) : $scope->getNativeType($node);
if ($exprType instanceof ConstantBooleanType) {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
$booleanNativeType = $scope->getNativeType($node);
if ($booleanNativeType instanceof ConstantBooleanType) {
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
$ruleErrorBuilder = $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
};
switch (get_class($node)) {
case BinaryOp\Greater::class:
$nodeType = 'greater';
break;
case BinaryOp\GreaterOrEqual::class:
$nodeType = 'greaterOrEqual';
break;
case BinaryOp\Smaller::class:
$nodeType = 'smaller';
break;
case BinaryOp\SmallerOrEqual::class:
$nodeType = 'smallerOrEqual';
break;
default:
throw new ShouldNotHappenException();
}
return [
$addTip(RuleErrorBuilder::message(sprintf(
'Comparison operation "%s" between %s and %s is always %s.',
$node->getOperatorSigil(),
$scope->getType($node->left)->describe(VerbosityLevel::value()),
$scope->getType($node->right)->describe(VerbosityLevel::value()),
$exprType->getValue() ? 'true' : 'false',
)))->identifier(sprintf('%s.always%s', $nodeType, $exprType->getValue() ? 'True' : 'False'))->build(),
];
}
return [];
}
}