ClassMethodNameMatchesFieldName
info on by defaultGroup: Confusing constructs · PhpStorm name: ClassMethodNameMatchesFieldNameInspection
When a method and a property share a name, $obj->name() and ($obj->name)() mean different things. This is especially confusing when the property holds a callable, and when the property's type is unknown the reader cannot tell which one is meant.
Example
php
<?php
class Account {
private $owner;
public function owner() {}
}
class Hooks {
/* @var callable */
protected $onSave;
/** @var \Closure|null */
protected $onLoad;
public function onSave() {}
public static function onLoad() {}
}
class Child extends Account {
public function owner() {}
}
class Counter {
private $total = 0;
private ?string $label;
const size = 3;
public function total() {}
public function label() {}
public function size() {}
}
interface Named {
public function name();
}Reported:
- line 4: A property with this name exists and its type is unknown; rename the method or type the property.
- line 12: A callable property with this name exists; rename the method (for example with a get/is/has prefix).
- line 13: A callable property with this name exists; rename the method (for example with a get/is/has prefix).
- line 17: A property with this name exists and its type is unknown; rename the method or type the property.
Configure
In custos.json:
json
{
"rules": {
"ClassMethodNameMatchesFieldName": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore ClassMethodNameMatchesFieldName
/**
* @noinspection ClassMethodNameMatchesFieldNameInspection
*/