ReferencingObjects
warning on by default quick-fixGroup: Confusing constructs · PhpStorm name: ReferencingObjectsInspection
Since PHP 5, object handles are passed and assigned by value of the handle: the callee already works on the same object. Taking objects by reference (Foo &$x parameters, $x = &new Foo()) is a PHP 4 leftover that only confuses readers — unless the reference is really used to replace the caller's variable.
Example
php
<?php
$cache = & new ArrayObject([]);
$queue =&new SplQueue;
$plain = new SplStack();
function attach(DOMNode &$node, array &$list, &$any, int &$n) {}
function detach(?DOMNode& $node, ?string &$label, iterable &$it) {}
interface Visitor {
public function visit(
Tree & $tree,
mixed &$extra
);
}
function replace(DOMNode &$node) {
$node = new DOMText('x');
}
function check(DOMNode &$a, DOMNode &$b, DOMNode &$c, DOMNode &$d = null) {
if (($a)) {}
return !$b || ($c ? 1 : 0);
}
$fn = function (DOMNode &$node) {};php
<?php
$cache = new ArrayObject([]);
$queue = new SplQueue;
$plain = new SplStack();
function attach(DOMNode $node, array &$list, &$any, int &$n) {}
function detach(?DOMNode $node, ?string &$label, iterable &$it) {}
interface Visitor {
public function visit(
Tree $tree,
mixed &$extra
);
}
function replace(DOMNode &$node) {
$node = new DOMText('x');
}
function check(DOMNode &$a, DOMNode &$b, DOMNode &$c, DOMNode &$d = null) {
if (($a)) {}
return !$b || ($c ? 1 : 0);
}
$fn = function (DOMNode &$node) {};Reported:
- line 2: Objects are handed over by handle already; assign the new instance without '&'.
- line 3: Objects are handed over by handle already; assign the new instance without '&'.
- line 6: Objects are handed over by handle already; drop the '&' before '$node'.
- line 7: Objects are handed over by handle already; drop the '&' before '$node'.
- line 11: Objects are handed over by handle already; drop the '&' before '$tree'.
Configure
In custos.json:
json
{
"rules": {
"ReferencingObjects": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore ReferencingObjects
/**
* @noinspection ReferencingObjectsInspection
*/