Skip to content

Commit d025c3f

Browse files
committed
Added support for ConstantString unions in DuplicateKeysInLiteralArraysRule
1 parent 0154b7e commit d025c3f

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\Type\Constant\ConstantIntegerType;
1212
use PHPStan\Type\ConstantScalarType;
13+
use PHPStan\Type\UnionType;
14+
use PHPStan\Type\VerbosityLevel;
1315
use function array_keys;
1416
use function count;
1517
use function implode;
@@ -79,29 +81,29 @@ public function processNode(Node $node, Scope $scope): array
7981
}
8082
}
8183

82-
if (!$keyType instanceof ConstantScalarType) {
84+
if (count($keyType->getConstantScalarValues()) === 0) {
8385
$autoGeneratedIndex = false;
8486
continue;
8587
}
8688

87-
$value = $keyType->getValue();
88-
$printedValue = $key !== null
89-
? $this->exprPrinter->printExpr($key)
90-
: $value;
89+
foreach ($keyType->getConstantScalarValues() as $value) {
90+
$printedValue = $key !== null
91+
? $this->exprPrinter->printExpr($key)
92+
: $value;
93+
$printedValues[$value][] = $printedValue;
9194

92-
$printedValues[$value][] = $printedValue;
95+
if (!isset($valueLines[$value])) {
96+
$valueLines[$value] = $item->getStartLine();
97+
}
9398

94-
if (!isset($valueLines[$value])) {
95-
$valueLines[$value] = $item->getStartLine();
96-
}
99+
$previousCount = count($values);
100+
$values[$value] = $printedValue;
101+
if ($previousCount !== count($values)) {
102+
continue;
103+
}
97104

98-
$previousCount = count($values);
99-
$values[$value] = $printedValue;
100-
if ($previousCount !== count($values)) {
101-
continue;
105+
$duplicateKeys[$value] = true;
102106
}
103-
104-
$duplicateKeys[$value] = true;
105107
}
106108

107109
$messages = [];

tests/PHPStan/Rules/Arrays/DuplicateKeysInLiteralArraysRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ public function testDuplicateKeys(): void
6161
'Array has 2 duplicate keys with value -41 (-41, -41).',
6262
76,
6363
],
64+
[
65+
'Array has 2 duplicate keys with value \'foo\' (\'foo\', $key).',
66+
102,
67+
],
68+
[
69+
'Array has 2 duplicate keys with value \'bar\' (\'bar\', $key).',
70+
103,
71+
],
72+
[
73+
'Array has 2 duplicate keys with value \'key\' (\'key\', $key2).',
74+
105,
75+
],
6476
]);
6577
}
6678

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,19 @@ public function doWithoutKeys(int $int)
9292
];
9393
}
9494

95+
/**
96+
* @param 'foo'|'bar' $key
97+
*/
98+
public function doUnionKeys(string $key): void
99+
{
100+
$key2 = 'key';
101+
$a = [
102+
'foo' => 'foo',
103+
'bar' => 'bar',
104+
$key => 'foo|bar',
105+
'key' => 'bar',
106+
$key2 => 'foo',
107+
];
108+
}
109+
95110
}

0 commit comments

Comments
 (0)