Skip to content

DuplicateArrayKeys ​

warning on by default

Group: Probable bugs · PhpStorm name: DuplicateArrayKeysInspection

In an array literal a repeated string key silently overwrites the earlier entry. Either the earlier entry is dead (different value) or the whole entry is a redundant copy (same value).

Example ​

php
<?php
$settings = [
    7 => 'a',
    '7' => 'a',
    'retries' => 3,
    "retries" => 3,
    'hosts' => ['db1'],
    'hosts' => ['db1'],
    'timeout' => 10,
    'timeout' => 20,
    'timeout' => 10,
    'label' => $prefix . 'x',
    'label' => $prefix.'x',
    "slot-$n" => 1,
    "slot-$n" => 1,
    'mode' => 'fast',
    'nested' => ['mode' => 'slow'],
];

Reported:

  • line 4: Same key and value already present; remove this entry.
  • line 6: Same key and value already present; remove this entry.
  • line 8: Key already used earlier; the earlier entry is overwritten.
  • line 10: Key already used earlier; the earlier entry is overwritten.
  • line 11: Key already used earlier; the earlier entry is overwritten.
  • line 13: Same key and value already present; remove this entry.

Configure ​

In custos.json:

json
{
  "rules": {
    "DuplicateArrayKeys": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore DuplicateArrayKeys

/**
 * @noinspection DuplicateArrayKeysInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.