ArrayPushMissUse
warning on by default quick-fixGroup: 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
| Option | Type | Default | Effect |
|---|---|---|---|
| REPORT_EXCESSIVE_COUNT_CALLS | bool | true | Enables 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
*/