Skip to content

ArrayPushMissUse ​

warning on by default quick-fix

Group: Performance · PhpStorm name: ArrayPushMissUseInspection

Appending a single element with array_push($list, $item); costs a function call; the append operator $list[] = $item; does the same faster. Likewise, $list[count($list)] = $item; computes an index that the append operator would produce anyway (for list-shaped arrays).

Example ​

php
<?php
$queue = [];
array_push($queue, $job);

class Log {
    /** @var string[] */
    private $lines = [];
    private array $rows = [];

    public function add(array $items, string $line) {
        \array_push($this->lines, $line);
        array_push($this->rows, [1, 2]);
        array_push($items, $line);
    }
}

$queue[count($queue)] = $job;
$this->items[count($this->items)] = $job;
$queue[\count($queue)] = &$job;
php
<?php
$queue = [];
$queue[] = $job;

class Log {
    /** @var string[] */
    private $lines = [];
    private array $rows = [];

    public function add(array $items, string $line) {
        $this->lines[] = $line;
        $this->rows[] = [1, 2];
        $items[] = $line;
    }
}

$queue[count($queue)] = $job;
$this->items[count($this->items)] = $job;
$queue[\count($queue)] = &$job;

Reported:

  • line 3: Use '$queue[] = $job' instead; it avoids a function call.
  • line 11: Use '$this->lines[] = $line' instead; it avoids a function call.
  • line 12: Use '$this->rows[] = [1, 2]' instead; it avoids a function call.
  • line 13: Use '$items[] = $line' instead; it avoids a function call.

Options ​

OptionTypeDefaultEffect
REPORT_EXCESSIVE_COUNT_CALLSbooltrueEnables part B (C[count(C)] = …).

Configure ​

In custos.json:

json
{
  "rules": {
    "ArrayPushMissUse": {
      "enabled": false,
      "options": {
        "REPORT_EXCESSIVE_COUNT_CALLS": true
      }
    }
  }
}

Suppress ​

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

php
// @custos-ignore ArrayPushMissUse

/**
 * @noinspection ArrayPushMissUseInspection
 */

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.