forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvalidIncDecOperationRule.php
More file actions
180 lines (159 loc) · 4.73 KB
/
InvalidIncDecOperationRule.php
File metadata and controls
180 lines (159 loc) · 4.73 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
173
174
175
176
177
178
179
180
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Operators;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function get_class;
use function sprintf;
/**
* @implements Rule<Node\Expr>
*/
#[RegisteredRule(level: 0)]
final class InvalidIncDecOperationRule implements Rule
{
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private PhpVersion $phpVersion,
)
{
}
public function getNodeType(): string
{
return Node\Expr::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (
!$node instanceof Node\Expr\PreInc
&& !$node instanceof Node\Expr\PostInc
&& !$node instanceof Node\Expr\PreDec
&& !$node instanceof Node\Expr\PostDec
) {
return [];
}
switch (get_class($node)) {
case Node\Expr\PreInc::class:
$nodeType = 'preInc';
break;
case Node\Expr\PostInc::class:
$nodeType = 'postInc';
break;
case Node\Expr\PreDec::class:
$nodeType = 'preDec';
break;
case Node\Expr\PostDec::class:
$nodeType = 'postDec';
break;
default:
throw new ShouldNotHappenException();
}
$operatorString = $node instanceof Node\Expr\PreInc || $node instanceof Node\Expr\PostInc ? '++' : '--';
if (
!$node->var instanceof Node\Expr\Variable
&& !$node->var instanceof Node\Expr\ArrayDimFetch
&& !$node->var instanceof Node\Expr\PropertyFetch
&& !$node->var instanceof Node\Expr\StaticPropertyFetch
) {
return [
RuleErrorBuilder::message(sprintf(
'Cannot use %s on a non-variable.',
$operatorString,
))
->line($node->var->getStartLine())
->identifier(sprintf('%s.expr', $nodeType))
->build(),
];
}
$string = new StringType();
$deprecatedString = false;
if (
(
$this->phpVersion->deprecatesDecOnNonNumericString()
&& (
$node instanceof Node\Expr\PreDec
|| $node instanceof Node\Expr\PostDec
)
) || (
$this->phpVersion->deprecatesIncOnNonNumericString()
&& (
$node instanceof Node\Expr\PreInc
|| $node instanceof Node\Expr\PostInc
)
)
) {
$string = new IntersectionType([$string, new AccessoryNumericStringType()]);
$deprecatedString = true;
}
$allowedTypes = [new FloatType(), new IntegerType(), $string, new ObjectType('SimpleXMLElement')];
$deprecatedNull = false;
if (
!$this->phpVersion->deprecatesDecOnNonNumericString()
|| $node instanceof Node\Expr\PreInc
|| $node instanceof Node\Expr\PostInc
) {
$allowedTypes[] = new NullType();
} else {
$deprecatedNull = true;
}
$deprecatedBool = false;
if (!$this->phpVersion->deprecatesDecOnNonNumericString()) {
$allowedTypes[] = new BooleanType();
} else {
$deprecatedBool = true;
}
if ($this->phpVersion->supportsBcMathNumberOperatorOverloading()) {
$allowedTypes[] = new ObjectType('BcMath\Number');
}
$allowedTypes = new UnionType($allowedTypes);
$varType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->var,
'',
static fn (Type $type): bool => $allowedTypes->isSuperTypeOf($type)->yes(),
)->getType();
if ($varType instanceof ErrorType || $allowedTypes->isSuperTypeOf($varType)->yes()) {
return [];
}
$errorBuilder = RuleErrorBuilder::message(sprintf(
'Cannot use %s on %s.',
$operatorString,
$varType->describe(VerbosityLevel::value()),
))
->line($node->var->getStartLine())
->identifier(sprintf('%s.type', $nodeType));
if (!$varType->isString()->no() && $deprecatedString) {
$errorBuilder->addTip(sprintf(
'Operator %s is deprecated for non-numeric-strings. Either narrow the type to numeric-string, or use %s().',
$operatorString,
$node instanceof Node\Expr\PreDec || $node instanceof Node\Expr\PostDec ? 'str_decrement' : 'str_increment',
));
}
if (!$varType->isNull()->no() && $deprecatedNull) {
$errorBuilder->addTip(sprintf('Operator %s is deprecated for null.', $operatorString));
}
if (!$varType->isBoolean()->no() && $deprecatedBool) {
$errorBuilder->addTip(sprintf('Operator %s is deprecated for %s.', $operatorString, TypeCombinator::intersect($varType, new BooleanType())->describe(VerbosityLevel::value())));
}
return [
$errorBuilder->build(),
];
}
}