Repository navigation
Expand file tree
/
Copy pathInvalidBinaryOperationRule.php
More file actions
196 lines (172 loc) · 5.42 KB
/
Copy pathInvalidBinaryOperationRule.php
File metadata and controls
196 lines (172 loc) · 5.42 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Operators;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\MultipleNodeTypesRule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
use function strlen;
use function strpos;
use function substr;
/**
* @implements MultipleNodeTypesRule<Node\Expr>
*/
#[RegisteredRule(level: 2)]
final class InvalidBinaryOperationRule implements MultipleNodeTypesRule
{
public function __construct(
private ExprPrinter $exprPrinter,
private RuleLevelHelper $ruleLevelHelper,
private PhpVersion $phpVersion,
)
{
}
public function getNodeTypes(): array
{
return [Node\Expr\BinaryOp::class, Node\Expr\AssignOp::class];
}
public function getNodeType(): string
{
return Node\Expr::class;
}
public function processNode(Node $node, Scope $scope): array
{
$errors = $this->checkInvalidOperation($node, $scope);
if ($errors !== [] || !$node instanceof Node\Expr\BinaryOp\Mod || $scope->getType($node) instanceof ErrorType) {
return $errors;
}
return $this->checkModImplicitConversion($node, $scope);
}
/**
* @return list<IdentifierRuleError>
*/
private function checkInvalidOperation(Node $node, Scope $scope): array
{
if (
!$node instanceof Node\Expr\BinaryOp
&& !$node instanceof Node\Expr\AssignOp
) {
return [];
}
if ($node instanceof Node\Expr\AssignOp) {
$identifier = 'assignOp';
$left = $node->var;
$right = $node->expr;
} else {
$identifier = 'binaryOp';
$left = $node->left;
$right = $node->right;
}
if ($node instanceof Node\Expr\AssignOp\Concat || $node instanceof Node\Expr\BinaryOp\Concat) {
$callback = static fn (Type $type): bool => !$type->toString() instanceof ErrorType;
} elseif ($node instanceof Node\Expr\AssignOp\Plus || $node instanceof Node\Expr\BinaryOp\Plus) {
$callback = static fn (Type $type): bool => !$type->toNumber() instanceof ErrorType || $type->isArray()->yes();
} else {
$callback = static fn (Type $type): bool => !$type->toNumber() instanceof ErrorType;
}
$leftType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$left,
'',
$callback,
)->getType();
if ($leftType instanceof ErrorType) {
return [];
}
$rightType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$right,
'',
$callback,
)->getType();
if ($rightType instanceof ErrorType) {
return [];
}
// on the very operand types the walk saw, the operation's own type is the
// answer - the walk already priced it
if (
$leftType->equals($scope->getType($left))
&& $rightType->equals($scope->getType($right))
&& !$scope->getType($node) instanceof ErrorType
) {
return [];
}
if ($node instanceof Node\Expr\AssignOp) {
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$newNode->var = new TypeExpr($leftType);
$newNode->expr = new TypeExpr($rightType);
$newLeft = $newNode->var;
$newRight = $newNode->expr;
} else {
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$newNode->left = new TypeExpr($leftType);
$newNode->right = new TypeExpr($rightType);
$newLeft = $newNode->left;
$newRight = $newNode->right;
}
// getScopeType(): the node was synthesized right here, so NodeScopeResolver
// never visits it and there is no before-scope to wait for
if (!$scope->getScopeType($newNode) instanceof ErrorType) {
return [];
}
$leftPrinted = $this->exprPrinter->printExpr($newLeft);
$rightPrinted = $this->exprPrinter->printExpr($newRight);
$opLeftSideTrimmed = substr($this->exprPrinter->printExpr($newNode), strlen($leftPrinted) + 1);
$pos = strpos($opLeftSideTrimmed, $rightPrinted);
if ($pos === false) {
throw new ShouldNotHappenException();
}
return [
RuleErrorBuilder::message(sprintf(
'Binary operation "%s" between %s and %s results in an error.',
substr($opLeftSideTrimmed, 0, $pos - 1),
$scope->getType($left)->describe(VerbosityLevel::value()),
$scope->getType($right)->describe(VerbosityLevel::value()),
))
->line($left->getStartLine())
->identifier(sprintf('%s.invalid', $identifier))
->build(),
];
}
/**
* `%` converts both operands to int: a float operand loses precision,
* which PHP 8.1+ reports as deprecated.
*
* @return list<IdentifierRuleError>
*/
private function checkModImplicitConversion(Node\Expr\BinaryOp\Mod $node, Scope $scope): array
{
if (!$this->phpVersion->deprecatesImplicitlyFloatConversionToInt()) {
return [];
}
$callback = static fn (Type $type): bool => !$type->isFloat()->no();
foreach ([$node->left, $node->right] as $operand) {
$type = $this->ruleLevelHelper->findTypeToCheck($scope, $operand, '', $callback)->getType();
if ($type instanceof ErrorType || $type->toNumber()->isFloat()->no()) {
continue;
}
return [
RuleErrorBuilder::message(sprintf(
'Deprecated in PHP 8.1: Implicit conversion from %s to int loses precision.',
$type->describe(VerbosityLevel::value()),
))
->line($node->left->getStartLine())
->identifier('binaryOp.mod.implicitConversion')
->build(),
];
}
return [];
}
}