-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathDuplicateArrayKeyPlugin.php
More file actions
376 lines (358 loc) · 15.8 KB
/
DuplicateArrayKeyPlugin.php
File metadata and controls
376 lines (358 loc) · 15.8 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
declare(strict_types=1);
use ast\Node;
use Phan\AST\ASTHasher;
use Phan\AST\ASTReverter;
use Phan\AST\UnionTypeVisitor;
use Phan\Issue;
use Phan\Parse\ParseVisitor;
use Phan\PluginV3;
use Phan\PluginV3\PluginAwarePostAnalysisVisitor;
use Phan\PluginV3\PostAnalyzeNodeCapability;
/**
* Checks for duplicate/equivalent array keys and case statements, as well as arrays mixing `key => value, with `value,`.
*
* @see DollarDollarPlugin for generic plugin documentation.
*/
class DuplicateArrayKeyPlugin extends PluginV3 implements PostAnalyzeNodeCapability
{
/**
* @return string - name of PluginAwarePostAnalysisVisitor subclass
* @override
*/
public static function getPostAnalyzeNodeVisitorClassName(): string
{
return DuplicateArrayKeyVisitor::class;
}
}
/**
* This class has visitArray called on all array literals in files to check for potential problems with keys.
*
* When __invoke on this class is called with a node, a method
* will be dispatched based on the `kind` of the given node.
*
* Visitors such as this are useful for defining lots of different
* checks on a node based on its kind.
*/
class DuplicateArrayKeyVisitor extends PluginAwarePostAnalysisVisitor
{
private const HASH_PREFIX = "\x00__phan_dnu_";
// Do not define the visit() method unless a plugin has code and needs to visit most/all node types.
/**
* @param Node $node
* A switch statement's case statement(AST_SWITCH_LIST) node to analyze
* @override
*/
public function visitSwitchList(Node $node): void
{
$children = $node->children;
if (count($children) <= 1) {
// This plugin will never emit errors if there are 0 or 1 elements.
return;
}
$case_constant_set = [];
$values_to_check = [];
foreach ($children as $i => $case_node) {
if (!$case_node instanceof Node) {
throw new AssertionError("Switch list must contain nodes");
}
$case_cond = $case_node->children['cond'];
if ($case_cond === null) {
continue; // This is `default:`. php --syntax-check already checks for duplicates.
}
// Skip array entries without literal keys. (Do it before resolving the key value)
if (!is_scalar($case_cond)) {
$original_case_cond = $case_cond;
$case_cond = UnionTypeVisitor::unionTypeFromNode($this->code_base, $this->context, $case_cond)->asSingleScalarValueOrNullOrSelf();
if (is_object($case_cond)) {
$case_cond = $original_case_cond;
}
}
if (is_string($case_cond)) {
$cond_key = "s$case_cond";
$values_to_check[$i] = $case_cond;
} elseif (is_int($case_cond)) {
$cond_key = $case_cond;
$values_to_check[$i] = $case_cond;
} elseif (is_bool($case_cond)) {
$cond_key = $case_cond ? "T" : "F";
$values_to_check[$i] = $case_cond;
} else {
// could be literal null?
$cond_key = ASTHasher::hash($case_cond);
if (!is_object($case_cond)) {
$values_to_check[$i] = $case_cond;
}
}
if (isset($case_constant_set[$cond_key])) {
$normalized_case_cond = is_object($case_cond) ? ASTReverter::toShortString($case_cond) : self::normalizeSwitchKey($case_cond);
$this->emitPluginIssue(
$this->code_base,
(clone $this->context)->withLineNumberStart($case_node->lineno),
'PhanPluginDuplicateSwitchCase',
"Duplicate/Equivalent switch case({STRING_LITERAL}) detected in switch statement - the later entry will be ignored in favor of case {CODE} at line {LINE}.",
[$normalized_case_cond, ASTReverter::toShortString($case_constant_set[$cond_key]->children['cond']), $case_constant_set[$cond_key]->lineno],
Issue::SEVERITY_NORMAL,
Issue::REMEDIATION_A,
15071
);
// Add a fake value to indicate loose equality checks are redundant
$values_to_check[-1] = true;
}
$case_constant_set[$cond_key] = $case_node;
}
if (!isset($values_to_check[-1]) && count($values_to_check) > 1 && !self::areAllSwitchCasesTheSameType($values_to_check)) {
// @phan-suppress-next-line PhanPartialTypeMismatchArgument array keys are integers for switch
$this->extendedLooseEqualityCheck($values_to_check, $children);
}
}
/**
* @param array<mixed,mixed> $values_to_check scalar constant values of case statements
*/
private static function areAllSwitchCasesTheSameType(array $values_to_check): bool
{
$categories = 0;
foreach ($values_to_check as $value) {
if (is_int($value)) {
$categories |= 1;
if ($categories !== 1) {
return false;
}
} elseif (is_string($value)) {
if (is_numeric($value)) {
// This includes float-like strings such as `"1e0"`, which adds ambiguity ("1e0" == "1")
return false;
}
$categories |= 2;
if ($categories !== 2) {
return false;
}
} else {
return false;
}
}
return true;
}
/**
* Perform a heuristic check if any element is `==` a previous element.
*
* This is intended to perform well for large arrays.
*
* TODO: Do a better job for small arrays.
* @param array<mixed, mixed> $values_to_check
* @param list<mixed> $children an array of scalars
*/
private function extendedLooseEqualityCheck(array $values_to_check, array $children): void
{
$numeric_set = [];
$fuzzy_numeric_set = [];
foreach ($values_to_check as $i => $value) {
if (is_numeric($value)) {
if (is_int($value)) {
$old_index = $numeric_set[$value] ?? $fuzzy_numeric_set[$value] ?? null;
$numeric_set[$value] = $i;
} else {
// For `"1"`, search for `"1foo"`, `"1bar"`, etc.
$original_value = $value;
$value = is_float($value) ? (string)$value : (string)filter_var($value, FILTER_VALIDATE_FLOAT);
$old_index = $numeric_set[$value] ?? null;
if ($value === (string)$original_value) {
$old_index ??= $fuzzy_numeric_set[$value] ?? null;
$numeric_set[$value] = $i;
} else {
$fuzzy_numeric_set[$value] = $i;
}
}
} else {
$value = (float)$value;
// For `"1foo"`, search for `1` but not `"1bar"`
$old_index = $numeric_set[$value] ?? null;
// @phan-suppress-next-line PhanTypeMismatchDimAssignment
$fuzzy_numeric_set[$value] = $i;
}
if ($old_index !== null) {
$this->emitPluginIssue(
$this->code_base,
(clone $this->context)->withLineNumberStart($children[$i]->lineno),
'PhanPluginDuplicateSwitchCaseLooseEquality',
"Switch case({STRING_LITERAL}) is loosely equivalent (==) to an earlier case ({STRING_LITERAL}) in switch statement - the earlier entry may be chosen instead.",
[self::normalizeSwitchKey($values_to_check[$i]), self::normalizeSwitchKey($values_to_check[$old_index])],
Issue::SEVERITY_NORMAL,
Issue::REMEDIATION_A,
15072
);
}
}
}
/**
* @param Node $node
* A match expressions's arms list (AST_MATCH_ARM_LIST) node to analyze
* @override
* @suppress PhanPossiblyUndeclaredProperty
*/
public function visitMatchArmList(Node $node): void
{
$children = $node->children;
if (!$children) {
// This plugin will never emit errors if there are 0 elements.
return;
}
$arm_expr_constant_set = [];
foreach ($children as $arm_node) {
foreach ($arm_node->children['cond']->children ?? [] as $arm_expr_cond) {
if ($arm_expr_cond === null) {
continue; // This is `default:`. php --syntax-check already checks for duplicates.
}
$lineno = $arm_expr_cond->lineno ?? $arm_node->lineno;
// Skip array entries without literal keys. (Do it before resolving the key value)
if (is_object($arm_expr_cond) && ParseVisitor::isConstExpr($arm_expr_cond, ParseVisitor::CONSTANT_EXPRESSION_FORBID_NEW_EXPRESSION)) {
// Only infer the value for values not affected by conditions - that will change after the expressions are analyzed
$original_cond = $arm_expr_cond;
$arm_expr_cond = UnionTypeVisitor::unionTypeFromNode($this->code_base, $this->context, $arm_expr_cond)->asSingleScalarValueOrNullOrSelf();
if (is_object($arm_expr_cond)) {
$arm_expr_cond = $original_cond;
}
}
if (is_string($arm_expr_cond)) {
$cond_key = "s$arm_expr_cond";
} elseif (is_int($arm_expr_cond)) {
$cond_key = $arm_expr_cond;
} elseif (is_bool($arm_expr_cond)) {
$cond_key = $arm_expr_cond ? "T" : "F";
} else {
// TODO: This seems like it'd be flaky with ast\Node->flags and lineno?
$cond_key = ASTHasher::hash($arm_expr_cond);
}
if (isset($arm_expr_constant_set[$cond_key])) {
$normalized_arm_expr_cond = ASTReverter::toShortString($arm_expr_cond);
$this->emitPluginIssue(
$this->code_base,
(clone $this->context)->withLineNumberStart($lineno),
'PhanPluginDuplicateMatchArmExpression',
"Duplicate match arm expression({STRING_LITERAL}) detected in match expression - the later entry will be ignored in favor of expression {CODE} at line {LINE}.",
[$normalized_arm_expr_cond, ASTReverter::toShortString($arm_expr_constant_set[$cond_key][0]), $arm_expr_constant_set[$cond_key][1]],
Issue::SEVERITY_NORMAL,
Issue::REMEDIATION_A,
15071
);
}
$arm_expr_constant_set[$cond_key] = [$arm_expr_cond, $arm_node->lineno];
}
}
}
/**
* @param Node $node
* An array literal(AST_ARRAY) node to analyze
* @override
*/
public function visitArray(Node $node): void
{
$children = $node->children;
if (count($children) <= 1) {
// This plugin will never emit errors if there are 0 or 1 elements.
return;
}
$has_entry_without_key = false;
$key_set = [];
foreach ($children as $entry) {
if (!($entry instanceof Node)) {
continue; // Triggered by code such as `list(, $a) = $expr`
}
$key = $entry->children['key'] ?? null;
// Skip array entries without literal keys. (Do it before resolving the key value)
if (is_null($key)) {
$has_entry_without_key = true;
continue;
}
if (is_object($key)) {
$key = UnionTypeVisitor::unionTypeFromNode($this->code_base, $this->context, $key)->asSingleScalarValueOrNullOrSelf();
if (is_object($key)) {
$key = self::HASH_PREFIX . ASTHasher::hash($entry->children['key']);
}
}
if (isset($key_set[$key])) {
// @phan-suppress-next-line PhanTypeMismatchDimFetchNullable
$this->warnAboutDuplicateArrayKey($entry, $key, $key_set[$key]);
}
// @phan-suppress-next-line PhanTypeMismatchDimAssignment
$key_set[$key] = $entry;
}
if ($has_entry_without_key && count($key_set) > 0) {
// This is probably a typo in most codebases. (e.g. ['foo' => 'bar', 'baz'])
// In phan, InternalFunctionSignatureMap.php does this deliberately with the first parameter being the return type.
$this->emit(
'PhanPluginMixedKeyNoKey',
"Should not mix array entries of the form [key => value,] with entries of the form [value,].",
[],
Issue::SEVERITY_NORMAL,
Issue::REMEDIATION_A,
15071
);
}
}
/**
* @param int|string|float|bool|null $key
*/
private function warnAboutDuplicateArrayKey(Node $entry, bool|float|int|null|string $key, Node $old_entry): void
{
if (is_string($key) && strncmp($key, self::HASH_PREFIX, strlen(self::HASH_PREFIX)) === 0) {
$this->emitPluginIssue(
$this->code_base,
(clone $this->context)->withLineNumberStart($entry->lineno),
'PhanPluginDuplicateArrayKeyExpression',
"Duplicate dynamic array key expression ({CODE}) detected in array - the earlier entry at line {LINE} will be ignored if the expression had the same value.",
[ASTReverter::toShortString($entry->children['key']), $old_entry->lineno],
Issue::SEVERITY_NORMAL,
Issue::REMEDIATION_A,
15071
);
return;
}
$normalized_key = self::normalizeKey($key);
$this->emitPluginIssue(
$this->code_base,
(clone $this->context)->withLineNumberStart($entry->lineno),
'PhanPluginDuplicateArrayKey',
"Duplicate/Equivalent array key value({STRING_LITERAL}) detected in array - the earlier entry {CODE} at line {LINE} will be ignored.",
[$normalized_key, ASTReverter::toShortString($old_entry->children['key']), $old_entry->lineno],
Issue::SEVERITY_NORMAL,
Issue::REMEDIATION_A,
15071
);
}
/**
* Converts a key to the value it would be if used as a case.
* E.g. 0, 0.5, and "0" all become the same value(0) when used as an array key.
*
* @param int|string|float|bool|null $key - The array key literal to be normalized.
* @return string - The normalized representation.
*/
private static function normalizeSwitchKey(bool|float|int|null|string $key): string
{
if (is_int($key)) {
return (string)$key;
} elseif (!is_string($key)) {
return (string)json_encode($key);
}
$tmp = [$key => true];
return ASTReverter::toShortString(key($tmp));
}
/**
* Converts a key to the value it would be if used as an array key.
* E.g. 0, 0.5, and "0" all become the same value(0) when used as an array key.
*
* @param int|string|float|bool|null $key - The array key literal to be normalized.
* @return string - The normalized representation.
*/
private static function normalizeKey(bool|float|int|null|string $key): string
{
if (is_int($key)) {
return (string)$key;
}
$tmp = [$key => true];
return ASTReverter::toShortString(key($tmp));
}
}
// Every plugin needs to return an instance of itself at the
// end of the file in which it's defined.
return new DuplicateArrayKeyPlugin();