An array literal gives the same key twice

tx0/php:duplicate-array-keydefaultwarningreliability/collectionsdepth fastbug

What it finds

['id' => 1, 'name' => $n, 'id' => 2]: when an array literal names the same key twice, the later value silently replaces the earlier one. Keys are compared as PHP compares them: the string '1' and the integer 1 are the same key.

From the rule's source, duplicate_array_key.rs.

Example from the rule's tests
<?php$config = [    'host' => 'localhost',    'port' => 3306,    'host' => '127.0.0.1',tx0 reports: the key 'host' is given twice in this array];$numbers = array(1 => 'a', '1' => 'b');tx0 reports: the key '1' is given twice in this array$quoted = ['it\'s' => 1, "it's" => 2];tx0 reports: is given twice in this array$fine = ['a' => 1, 'b' => 2, $key => 3, $key => 4, '01' => 5, 1 => 6, '-0' => 7, 0 => 8];['x' => $first, 'x' => $again] = $pair;
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/php:duplicate-array-key" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/php:duplicate-array-key] reason="..."

tx0 explain tx0/php:duplicate-array-key shows the same in the terminal. Found a false positive? Report it.