Skip to content

PassingByReferenceCorrectness ​

warning on by default

Group: Probable bugs · PhpStorm name: PassingByReferenceCorrectnessInspection

A parameter declared by reference (&$p) needs a variable-like argument. Passing the result of a call that does not return by reference, or a new expression, makes PHP emit a notice ("only variables should be passed by reference") and the callee's writes are lost.

Example ​

php
<?php
class Store
{
    public $items = [];
    public function &all() { return $this->items; }
    public function fill(&$target, $extra = null) { $target = []; }
    public function plain() { return []; }
    public static function make() { return []; }
}

function push_one(array &$list) { $list[] = 1; }

$s = new Store();
$s->fill($s->items, $s->plain());
$s->fill($buf);
$s->fill($s->all());
$s->fill($s->plain());
$s->fill(Store::make(), 1);
$s->fill(new ArrayObject(), 1);
push_one(array_values(...[$buf]));
push_one($s->items['k']);
push_one(unknown_fn());
count($s->plain());
key($s->plain());

Reported:

  • line 17: Pass a variable here: this parameter is taken by reference.
  • line 18: Pass a variable here: this parameter is taken by reference.
  • line 19: Pass a variable here: this parameter is taken by reference.
  • line 20: Pass a variable here: this parameter is taken by reference.

Configure ​

In custos.json:

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

Suppress ​

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

php
// @custos-ignore PassingByReferenceCorrectness

/**
 * @noinspection PassingByReferenceCorrectnessInspection
 */

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