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
|
|
|
|
2021-03-29 21:10:49 +02: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
|
|
|
];
|
|
|
|
```
|
|
|
|
|
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).
|