diff --git a/src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php b/src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php index 0d32b21381..c05440f892 100644 --- a/src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php +++ b/src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php @@ -83,7 +83,7 @@ public function processNode(Node $node, Scope $scope): array } } - $keyValues = $keyType->getConstantScalarValues(); + $keyValues = $keyType->toArrayKey()->getConstantScalarValues(); if (count($keyValues) === 0) { $autoGeneratedIndex = false; continue; diff --git a/tests/PHPStan/Rules/Arrays/DuplicateKeysInLiteralArraysRuleTest.php b/tests/PHPStan/Rules/Arrays/DuplicateKeysInLiteralArraysRuleTest.php index a01e02424d..03a01b9250 100644 --- a/tests/PHPStan/Rules/Arrays/DuplicateKeysInLiteralArraysRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/DuplicateKeysInLiteralArraysRuleTest.php @@ -93,6 +93,10 @@ public function testDuplicateKeys(): void "Array has 2 duplicate keys with value 'baz' (\$key, 'baz').", 171, ], + [ + "Array has 5 duplicate keys with value 1 (1, '1', true, 1.0, 1.1).", + 179, + ], ]); } diff --git a/tests/PHPStan/Rules/Arrays/data/duplicate-keys.php b/tests/PHPStan/Rules/Arrays/data/duplicate-keys.php index 7a73af9da9..8e7725d5b8 100644 --- a/tests/PHPStan/Rules/Arrays/data/duplicate-keys.php +++ b/tests/PHPStan/Rules/Arrays/data/duplicate-keys.php @@ -172,4 +172,15 @@ public function sureDuplicate4(string $key): void 'baz' => 'baz', ]; } + + public function duplicateWithCast(): void + { + $a = [ + 1 => 'foo', + '1' => 'bar', + true => 'baz', + 1.0 => 'some', + 1.1 => 'thing' + ]; + } }