-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathBooleanAndConstantConditionRule.php
More file actions
172 lines (148 loc) · 6.61 KB
/
BooleanAndConstantConditionRule.php
File metadata and controls
172 lines (148 loc) · 6.61 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\BooleanAndNode;
use PHPStan\Parser\LastConditionVisitor;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use function count;
use function sprintf;
/**
* @implements Rule<BooleanAndNode>
*/
#[RegisteredRule(level: 4)]
final class BooleanAndConstantConditionRule implements Rule
{
public function __construct(
private ConstantConditionRuleHelper $helper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
private bool $reportAlwaysTrueInLastCondition,
#[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')]
private bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return BooleanAndNode::class;
}
public function processNode(
Node $node,
Scope $scope,
): array
{
$errors = [];
$originalNode = $node->getOriginalNode();
$nodeText = $originalNode->getOperatorSigil();
$leftType = $this->helper->getBooleanType($scope, $originalNode->left);
$identifierType = $originalNode instanceof Node\Expr\BinaryOp\BooleanAnd ? 'booleanAnd' : 'logicalAnd';
if ($leftType instanceof ConstantBooleanType) {
$addTipLeft = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode->left, $ruleErrorBuilder);
}
$booleanNativeType = $this->helper->getNativeBooleanType($scope, $originalNode->left);
if ($booleanNativeType instanceof ConstantBooleanType) {
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode->left, $ruleErrorBuilder);
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode->left, $ruleErrorBuilder);
}
$ruleErrorBuilder = $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode->left, $ruleErrorBuilder);
};
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if (!$leftType->getValue() || $isLast !== true || $this->reportAlwaysTrueInLastCondition) {
$errorBuilder = $addTipLeft(RuleErrorBuilder::message(sprintf(
'Left side of %s is always %s.',
$nodeText,
$leftType->getValue() ? 'true' : 'false',
)))
->identifier(sprintf('%s.leftAlways%s', $identifierType, $leftType->getValue() ? 'True' : 'False'))
->line($originalNode->left->getStartLine());
if ($leftType->getValue() && $isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
$errors[] = $errorBuilder->build();
}
}
$rightScope = $node->getRightScope();
$rightType = $this->helper->getBooleanType(
$rightScope,
$originalNode->right,
);
if ($rightType instanceof ConstantBooleanType && !$scope->isInFirstLevelStatement()) {
$addTipRight = function (RuleErrorBuilder $ruleErrorBuilder) use ($rightScope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $this->possiblyImpureTipHelper->addTip($rightScope, $originalNode->right, $ruleErrorBuilder);
}
$booleanNativeType = $this->helper->getNativeBooleanType(
$rightScope,
$originalNode->right,
);
if ($booleanNativeType instanceof ConstantBooleanType) {
return $this->possiblyImpureTipHelper->addTip($rightScope, $originalNode->right, $ruleErrorBuilder);
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $this->possiblyImpureTipHelper->addTip($rightScope, $originalNode->right, $ruleErrorBuilder);
}
$ruleErrorBuilder = $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
return $this->possiblyImpureTipHelper->addTip($rightScope, $originalNode->right, $ruleErrorBuilder);
};
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if (!$rightType->getValue() || $isLast !== true || $this->reportAlwaysTrueInLastCondition) {
$errorBuilder = $addTipRight(RuleErrorBuilder::message(sprintf(
'Right side of %s is always %s.',
$nodeText,
$rightType->getValue() ? 'true' : 'false',
)))
->identifier(sprintf('%s.rightAlways%s', $identifierType, $rightType->getValue() ? 'True' : 'False'))
->line($originalNode->right->getStartLine());
if ($rightType->getValue() && $isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
$errors[] = $errorBuilder->build();
}
}
if (count($errors) === 0 && !$scope->isInFirstLevelStatement()) {
$nodeType = $this->treatPhpDocTypesAsCertain ? $scope->getType($originalNode) : $scope->getNativeType($originalNode);
if ($nodeType instanceof ConstantBooleanType) {
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $originalNode): RuleErrorBuilder {
if (!$this->treatPhpDocTypesAsCertain) {
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode, $ruleErrorBuilder);
}
$booleanNativeType = $scope->getNativeType($originalNode);
if ($booleanNativeType instanceof ConstantBooleanType) {
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode, $ruleErrorBuilder);
}
if (!$this->treatPhpDocTypesAsCertainTip) {
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode, $ruleErrorBuilder);
}
$ruleErrorBuilder = $ruleErrorBuilder->treatPhpDocTypesAsCertainTip();
return $this->possiblyImpureTipHelper->addTip($scope, $originalNode, $ruleErrorBuilder);
};
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if (!$nodeType->getValue() || $isLast !== true || $this->reportAlwaysTrueInLastCondition) {
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Result of %s is always %s.',
$nodeText,
$nodeType->getValue() ? 'true' : 'false',
)));
if ($nodeType->getValue() && $isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->tip('Remove remaining cases below this one and this error will disappear too.');
}
$errorBuilder->identifier(sprintf('%s.always%s', $identifierType, $nodeType->getValue() ? 'True' : 'False'));
$errors[] = $errorBuilder->build();
}
}
}
return $errors;
}
}