1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-05 21:19:03 +01:00
psalm/docs/running_psalm/issues/DuplicateArrayKey.md

41 lines
730 B
Markdown
Raw Normal View History

2020-03-19 17:32:49 +01:00
# DuplicateArrayKey
Emitted when an array has a key more than once
```php
2020-03-21 00:13:46 +01:00
<?php
2020-03-19 17:32:49 +01:00
$arr = [
2020-03-21 15:13:11 +01:00
'a' => 'one',
'b' => 'two',
'c' => 'this text will be overwritten by the next line',
'c' => 'three',
2020-03-19 17:32:49 +01:00
];
```
2020-03-21 00:13:46 +01:00
This can be caused by variadic arguments if `@no-named-arguments` is not specified:
```php
<?php
function foo($bar, ...$baz): array
{
return [$bar, ...$baz]; // $baz is array<array-key, mixed> since it can have named arguments
}
```
2020-03-21 00:13:46 +01:00
## How to fix
Remove the offending duplicates:
```php
<?php
$arr = [
2020-03-21 15:13:11 +01:00
'a' => 'one',
'b' => 'two',
'c' => 'three',
2020-03-21 00:13:46 +01:00
];
```
The first matching `'c'` key was removed to prevent a change in behaviour (any new duplicate keys overwrite the values of previous ones).