Skip to content

ArrayIsListCanBeUsed ​

info on by default quick-fix PHP ≥ 8.0

Group: Language level migration · PhpStorm name: ArrayIsListCanBeUsedInspection

Hand-written "is this array a list?" checks — comparing array_values($a) with $a, or array_keys($a) with range(0, count($a) - 1) — are better expressed with the built-in array_is_list($a).

Example ​

php
<?php
function inspect(array $cfg, array $other) {
    return [
        array_values($cfg) === $cfg,
        $cfg !== array_values($cfg),
        \array_values($cfg) !== $cfg,
        array_keys($cfg) === range(0, count($cfg) - 1),
        range(0, count($cfg) - 1) !== array_keys($cfg),

        array_values($cfg) === $other,
        array_keys($cfg) === range(1, count($cfg)),
        array_keys($cfg) === range(0, count($other) - 1),
        array_keys($cfg, 'on') === range(0, count($cfg) - 1),
        (array_values($cfg)) === $cfg,
        array_values($cfg) == $cfg,
    ];
}
php
<?php
function inspect(array $cfg, array $other) {
    return [
        array_is_list($cfg),
        !array_is_list($cfg),
        !\array_is_list($cfg),
        array_is_list($cfg),
        !array_is_list($cfg),

        array_values($cfg) === $other,
        array_keys($cfg) === range(1, count($cfg)),
        array_keys($cfg) === range(0, count($other) - 1),
        array_keys($cfg, 'on') === range(0, count($cfg) - 1),
        (array_values($cfg)) === $cfg,
        array_values($cfg) == $cfg,
    ];
}

Reported:

  • line 4: Replace with 'array_is_list($cfg)'.
  • line 5: Replace with '!array_is_list($cfg)'.
  • line 6: Replace with '!\array_is_list($cfg)'.
  • line 7: Replace with 'array_is_list($cfg)'.
  • line 8: Replace with '!array_is_list($cfg)'.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore ArrayIsListCanBeUsed

/**
 * @noinspection ArrayIsListCanBeUsedInspection
 */

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