forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrictComparisonOfDifferentTypesRule.php
More file actions
163 lines (139 loc) · 5.61 KB
/
StrictComparisonOfDifferentTypesRule.php
File metadata and controls
163 lines (139 loc) · 5.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Comparison;
use PhpParser\Node;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\RicherScopeGetTypeHelper;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Parser\LastConditionVisitor;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;
/**
* @implements Rule<Node\Expr\BinaryOp>
*/
#[RegisteredRule(level: 4)]
final class StrictComparisonOfDifferentTypesRule implements Rule
{
public function __construct(
private RicherScopeGetTypeHelper $richerScopeGetTypeHelper,
private PossiblyImpureTipHelper $possiblyImpureTipHelper,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
#[AutowiredParameter]
private bool $reportAlwaysTrueInLastCondition,
#[AutowiredParameter(ref: '%tips.treatPhpDocTypesAsCertain%')]
private bool $treatPhpDocTypesAsCertainTip,
)
{
}
public function getNodeType(): string
{
return Node\Expr\BinaryOp::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}
if ($node instanceof Node\Expr\BinaryOp\Identical) {
$nodeTypeResult = $this->richerScopeGetTypeHelper->getIdenticalResult($this->treatPhpDocTypesAsCertain ? $scope : $scope->doNotTreatPhpDocTypesAsCertain(), $node);
} elseif ($node instanceof Node\Expr\BinaryOp\NotIdentical) {
$nodeTypeResult = $this->richerScopeGetTypeHelper->getNotIdenticalResult($this->treatPhpDocTypesAsCertain ? $scope : $scope->doNotTreatPhpDocTypesAsCertain(), $node);
} else {
return [];
}
if (TraitContextHelper::isBinaryOpDependentOnTraitContext($scope, $node->left, $node->right)) {
return [];
}
$nodeType = $nodeTypeResult->type;
if (!$nodeType instanceof ConstantBooleanType) {
return [];
}
$leftType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->left) : $scope->getNativeType($node->left);
$rightType = $this->treatPhpDocTypesAsCertain ? $scope->getType($node->right) : $scope->getNativeType($node->right);
$addTip = function (RuleErrorBuilder $ruleErrorBuilder) use ($scope, $node, $nodeTypeResult): RuleErrorBuilder {
$reasons = $nodeTypeResult->reasons;
if (count($reasons) > 0) {
$ruleErrorBuilder = $ruleErrorBuilder->acceptsReasonsTip($reasons);
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
if (!$this->treatPhpDocTypesAsCertain) {
return $this->possiblyImpureTipHelper->addTip($scope, $node, $ruleErrorBuilder);
}
$instanceofTypeWithoutPhpDocs = $scope->getNativeType($node);
if ($instanceofTypeWithoutPhpDocs 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);
};
$verbosity = VerbosityLevel::value();
if (
(
$leftType->isConstantScalarValue()->yes()
&& !$leftType->isString()->no()
&& !$rightType->isConstantScalarValue()->yes()
&& !$rightType->isString()->no()
&& (
TrinaryLogic::extremeIdentity($leftType->isLowercaseString(), $rightType->isLowercaseString())->maybe()
|| TrinaryLogic::extremeIdentity($leftType->isUppercaseString(), $rightType->isUppercaseString())->maybe()
)
) || (
$rightType->isConstantScalarValue()->yes()
&& !$rightType->isString()->no()
&& !$leftType->isConstantScalarValue()->yes()
&& !$leftType->isString()->no()
&& (
TrinaryLogic::extremeIdentity($leftType->isLowercaseString(), $rightType->isLowercaseString())->maybe()
|| TrinaryLogic::extremeIdentity($leftType->isUppercaseString(), $rightType->isUppercaseString())->maybe()
)
)
) {
$verbosity = VerbosityLevel::precise();
}
if (!$nodeType->getValue()) {
return [
$addTip(RuleErrorBuilder::message(sprintf(
'Strict comparison using %s between %s and %s will always evaluate to false.',
$node->getOperatorSigil(),
$leftType->describe($verbosity),
$rightType->describe($verbosity),
)))->identifier(sprintf('%s.alwaysFalse', $node instanceof Node\Expr\BinaryOp\Identical ? 'identical' : 'notIdentical'))->build(),
];
}
$isLast = $node->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME);
if ($isLast === true && !$this->reportAlwaysTrueInLastCondition) {
return [];
}
$errorBuilder = $addTip(RuleErrorBuilder::message(sprintf(
'Strict comparison using %s between %s and %s will always evaluate to true.',
$node->getOperatorSigil(),
$leftType->describe($verbosity),
$rightType->describe($verbosity),
)));
if ($isLast === false && !$this->reportAlwaysTrueInLastCondition) {
$errorBuilder->addTip('Remove remaining cases below this one and this error will disappear too.');
}
if (
$leftType->isEnum()->yes()
&& $rightType->isEnum()->yes()
&& $node->getAttribute(LastConditionVisitor::ATTRIBUTE_IS_MATCH_NAME, false) !== true
) {
$errorBuilder->addTip('Use match expression instead. PHPStan will report unhandled enum cases.');
}
$errorBuilder->identifier(sprintf('%s.alwaysTrue', $node instanceof Node\Expr\BinaryOp\Identical ? 'identical' : 'notIdentical'));
return [
$errorBuilder->build(),
];
}
}