Skip to content

ArrayIsListCanBeUsed ​

info on by default quick-fix PHP ≥ 8.1

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($this->rows['a']) === $this->rows[ 'a' ],
    ];
}

function wrapped(array $cfg): bool
{
    return $cfg && !(array_keys($cfg) === range(0, count($cfg) - 1))
        || array_keys($cfg) !== range(0, count($cfg) - 1) === false;
}
php
<?php
function inspect(array $cfg, array $other) {
    return [
        array_is_list($cfg),
        !array_is_list($cfg),
        !\array_is_list($cfg),
        $cfg !== [] && array_is_list($cfg),
        $cfg === [] || !array_is_list($cfg),
        array_values($this->rows['a']) === $this->rows[ 'a' ],
    ];
}

function wrapped(array $cfg): bool
{
    return $cfg && !($cfg !== [] && array_is_list($cfg))
        || ($cfg === [] || !array_is_list($cfg)) === false;
}

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 '$cfg !== [] && array_is_list($cfg)'.
  • line 8: Replace with '$cfg === [] || !array_is_list($cfg)'.
  • line 15: Replace with '$cfg !== [] && array_is_list($cfg)'.
  • line 16: Replace with '$cfg === [] || !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. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.