Skip to content

Commit 8a53ffe

Browse files
Detect more duplicate array keys
1 parent 5878035 commit 8a53ffe

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function processNode(Node $node, Scope $scope): array
8383
}
8484
}
8585

86-
$keyValues = $keyType->getConstantScalarValues();
86+
$keyValues = $keyType->toArrayKey()->getConstantScalarValues();
8787
if (count($keyValues) === 0) {
8888
$autoGeneratedIndex = false;
8989
continue;

tests/PHPStan/Rules/Arrays/DuplicateKeysInLiteralArraysRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public function testDuplicateKeys(): void
9393
"Array has 2 duplicate keys with value 'baz' (\$key, 'baz').",
9494
171,
9595
],
96+
[
97+
"Array has 5 duplicate keys with value 1 (1, '1', true, 1.0, 1.1).",
98+
179,
99+
],
96100
]);
97101
}
98102

tests/PHPStan/Rules/Arrays/data/duplicate-keys.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,15 @@ public function sureDuplicate4(string $key): void
172172
'baz' => 'baz',
173173
];
174174
}
175+
176+
public function duplicateWithCast(): void
177+
{
178+
$a = [
179+
1 => 'foo',
180+
'1' => 'bar',
181+
true => 'baz',
182+
1.0 => 'some',
183+
1.1 => 'thing'
184+
];
185+
}
175186
}

0 commit comments

Comments
 (0)