Same Method Field Names
ID |
php.same_method_field_names |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Php |
Tags |
code_smell, naming |
Description
Reports a property whose name collides with a method declared on the same class. Both regular and constructor-promoted properties are checked, and the comparison is case-insensitive.
Rationale
PHP keeps methods and properties in separate symbol tables, so the same identifier may name
both at once. The collision is legal but confusing: at a use site $obj→name reads the
property while $obj→name() calls the method, and the two differ only by a pair of
parentheses. Readers must keep both members in mind and the documentation has to explain which
name is intended, so the shared name is a maintenance burden with no upside.
<?php
class Widget
{
private int $value; // FLAW — shares its name with the method value()
private string $label; // OK — no method named label
public function value(): int
{
return $this->value;
}
}
Remediation
Rename one of the colliding members so the property and the method have distinct names, for
example keep the method value() and rename the property to $currentValue.