Parameter Shadows Property
ID |
php.parameter_shadows_property |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Php |
Tags |
naming, reliability |
Description
Reports a method parameter whose name matches a property declared on the enclosing class.
Inside the body an unqualified reference to that name resolves to the parameter, so the
property can only be reached through $this→name.
Rationale
The shadow is silent: the code parses and runs, but every unqualified use of the name now
refers to the parameter rather than the property. The bug surfaces the moment the author
forgets the $this→ qualifier and unintentionally reads or writes the parameter where they
meant the property — a mistake that survives refactoring poorly and is easy to miss in review.
Renaming the parameter removes the ambiguity at the source.
<?php
class Counter {
private int $count = 0;
public function bump(int $count): int { // FLAW — parameter shadows the property
return $count + 1; // reads the parameter, not $this->count
}
public function bumpBy(int $delta): int { // OK — distinct name, no ambiguity
return $this->count + $delta;
}
}
Remediation
Rename the parameter to a name distinct from every property of the enclosing class (for example a more specific or abbreviated form), so that unqualified references inside the method are unambiguous.