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 = [];
$log = [];
array_push($queue, $job);
\array_push($log, [1, 2]);
array_push($unknown, $job);

array_push($queue, $a, $b);
array_push($queue, ... $more);
$size = array_push($queue, $job);
while (array_push($queue, $job) < 10) {}

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

$queue[count($queue) - 1] = $job;
$queue[count($other)] = $job;
$last = $queue[count($queue)];
$queue[count($queue)] .= 'x';
php
<?php
$queue = [];
$log = [];
$queue[] = $job;
$log[] = [1, 2];
array_push($unknown, $job);

array_push($queue, $a, $b);
array_push($queue, ... $more);
$size = array_push($queue, $job);
while (array_push($queue, $job) < 10) {}

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

$queue[count($queue) - 1] = $job;
$queue[count($other)] = $job;
$last = $queue[count($queue)];
$queue[count($queue)] .= 'x';

Reported:

  • line 4: Use '$queue[] = $job' instead; it avoids a function call.
  • line 5: Use '$log[] = [1, 2]' instead; it avoids a function call.
  • line 13: The index is redundant here; use '[]' to append.
  • line 14: The index is redundant here; use '[]' to append.

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. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.