Page MenuHomePhabricator

A linter could report unneeded/unused pass-by-references in php code
Open, Needs TriagePublic

Description

If a variable is used with & for pass-by-references in a function argument or a foreach loop, that is only needed when the variable is overwritten with an assignment.

If the variable is an object and only a property of the object is changed, there is no need to use &.

This could be reported by a linter, maybe codesniffer or better phan (upstream?)

If this is useful in phan, than an upstream bug should be created or maybe only a config is missing?

See https://gerrit.wikimedia.org/r/c/mediawiki/extensions/Kartographer/+/673963 for an example patch set

Event Timeline

Daimona subscribed.

I think this goes well beyond the scope of PHPCS. Phan has similar functionality in VariableTrackerPlugin, but not this specific feature. https://github.com/phan/phan/issues/2092 asks for that, so I guess it's a matter of waiting for it to be implemented upstream.

Also, I think it's worth noting that PHPStorm has an inspection for this.

DannyS712 subscribed.

I think this goes well beyond the scope of PHPCS. Phan has similar functionality in VariableTrackerPlugin, but not this specific feature. https://github.com/phan/phan/issues/2092 asks for that, so I guess it's a matter of waiting for it to be implemented upstream.

Also, I think it's worth noting that PHPStorm has an inspection for this.

Untagging codesniffer accordingly