-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathDuplicateKeysInLiteralArraysRule.php
121 lines (104 loc) · 3.07 KB
/
DuplicateKeysInLiteralArraysRule.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Arrays;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\LiteralArrayNode;
use PHPStan\Node\Printer\ExprPrinter;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use function array_keys;
use function count;
use function implode;
use function is_int;
use function max;
use function sprintf;
use function var_export;
/**
* @implements Rule<LiteralArrayNode>
*/
final class DuplicateKeysInLiteralArraysRule implements Rule
{
public function __construct(
private ExprPrinter $exprPrinter,
)
{
}
public function getNodeType(): string
{
return LiteralArrayNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$values = [];
$duplicateKeys = [];
$printedValues = [];
$valueLines = [];
/**
* @var int|false|null $autoGeneratedIndex
* - An int value represent the biggest integer used as array key.
* When no key is provided this value + 1 will be used.
* - Null is used as initializer instead of 0 to avoid issue with negative keys.
* - False means a non-scalar value was encountered and we cannot be sure of the next keys.
*/
$autoGeneratedIndex = null;
foreach ($node->getItemNodes() as $itemNode) {
$item = $itemNode->getArrayItem();
if ($item === null) {
$autoGeneratedIndex = false;
continue;
}
$key = $item->key;
if ($key === null) {
if ($autoGeneratedIndex === false) {
continue;
}
if ($autoGeneratedIndex === null) {
$autoGeneratedIndex = 0;
$keyType = new ConstantIntegerType(0);
} else {
$keyType = new ConstantIntegerType(++$autoGeneratedIndex);
}
} else {
$keyType = $itemNode->getScope()->getType($key);
$arrayKeyValues = $keyType->toArrayKey()->getConstantScalarValues();
if (count($arrayKeyValues) === 1 && is_int($arrayKeyValues[0])) {
$autoGeneratedIndex = $autoGeneratedIndex === null
? $arrayKeyValues[0]
: max($autoGeneratedIndex, $arrayKeyValues[0]);
}
}
$keyValues = $keyType->getConstantScalarValues();
if (count($keyValues) === 0) {
$autoGeneratedIndex = false;
continue;
}
foreach ($keyValues as $value) {
$printedValue = $key !== null
? $this->exprPrinter->printExpr($key)
: $value;
$printedValues[$value][] = $printedValue;
if (!isset($valueLines[$value])) {
$valueLines[$value] = $item->getStartLine();
}
$previousCount = count($values);
$values[$value] = $printedValue;
if ($previousCount !== count($values)) {
continue;
}
$duplicateKeys[$value] = true;
}
}
$messages = [];
foreach (array_keys($duplicateKeys) as $value) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Array has %d %s with value %s (%s).',
count($printedValues[$value]),
count($printedValues[$value]) === 1 ? 'duplicate key' : 'duplicate keys',
var_export($value, true),
implode(', ', $printedValues[$value]),
))->identifier('array.duplicateKey')->line($valueLines[$value])->build();
}
return $messages;
}
}