Skip to content

Commit 03cb092

Browse files
committed
faster
1 parent 672ea35 commit 03cb092

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function processNode(Node $node, Scope $scope): array
5252
*/
5353
$autoGeneratedIndex = null;
5454
$seenKeys = [];
55+
$seenUnions = [];
5556
foreach ($node->getItemNodes() as $itemNode) {
5657
$item = $itemNode->getArrayItem();
5758
if ($item === null) {
@@ -88,29 +89,42 @@ public function processNode(Node $node, Scope $scope): array
8889
}
8990

9091
$duplicate = false;
91-
$newValuesType = $keyType;
92+
$newValuesType = $keyValues;
9293
foreach ($seenKeys as $seenKey) {
93-
if ($seenKey instanceof UnionType) {
94-
continue;
94+
$offset = array_search($seenKey, $newValuesType, true);
95+
if ($offset !== false) {
96+
unset($newValuesType[$offset]);
9597
}
96-
$newValuesType = TypeCombinator::remove($newValuesType, $seenKey);
9798

9899
if (
99-
$newValuesType instanceof NeverType
100-
|| $newValuesType->isSuperTypeOf($seenKey)->yes()
100+
$newValuesType === []
101101
) {
102102
$duplicate = true;
103103
break;
104104
}
105105
}
106106

107-
if (!$newValuesType instanceof NeverType) {
108-
if (!$newValuesType instanceof UnionType) {
109-
foreach ($seenKeys as $k => $seenKey) {
110-
$seenKeys[$k] = TypeCombinator::remove($seenKey, $newValuesType);
107+
if (
108+
$newValuesType !== []
109+
) {
110+
if (count($newValuesType) === 1) {
111+
$newValue = $newValuesType[array_key_first($newValuesType)];
112+
foreach ($seenUnions as $k => $seenKey) {
113+
$offset = array_search($newValue, $seenKey, true);
114+
if ($offset !== false) {
115+
unset($seenUnions[$k][$offset]);
116+
117+
if (count($seenUnions[$k]) === 1) {
118+
$ukey = array_key_first($seenUnions[$k]);
119+
$seenKeys[] = $seenUnions[$k][$ukey];
120+
unset($seenUnions[$k]);
121+
}
122+
}
111123
}
124+
$seenKeys[] = $newValue;
125+
} else {
126+
$seenUnions[] = $newValuesType;
112127
}
113-
$seenKeys[] = $newValuesType;
114128
}
115129

116130
foreach ($keyValues as $value) {

0 commit comments

Comments
 (0)