2020-03-19 12:32:49 -04:00
|
|
|
# DuplicateArrayKey
|
|
|
|
|
|
|
|
Emitted when an array has a key more than once
|
|
|
|
|
|
|
|
```php
|
2020-03-20 19:13:46 -04:00
|
|
|
<?php
|
|
|
|
|
2020-03-19 12:32:49 -04:00
|
|
|
$arr = [
|
2020-03-21 10:13:11 -04:00
|
|
|
'a' => 'one',
|
|
|
|
'b' => 'two',
|
|
|
|
'c' => 'this text will be overwritten by the next line',
|
|
|
|
'c' => 'three',
|
2020-03-19 12:32:49 -04:00
|
|
|
];
|
|
|
|
```
|
2020-03-20 19:13:46 -04:00
|
|
|
|
2021-03-29 14:10:49 -05: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-20 19:13:46 -04:00
|
|
|
## How to fix
|
|
|
|
|
|
|
|
Remove the offending duplicates:
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$arr = [
|
2020-03-21 10:13:11 -04:00
|
|
|
'a' => 'one',
|
|
|
|
'b' => 'two',
|
|
|
|
'c' => 'three',
|
2020-03-20 19:13:46 -04:00
|
|
|
];
|
|
|
|
```
|
|
|
|
|
2020-08-08 14:09:41 +02:00
|
|
|
The first matching `'c'` key was removed to prevent a change in behaviour (any new duplicate keys overwrite the values of previous ones).
|