SenselessMethodDuplication
info on by default quick-fixGroup: Unused · PhpStorm name: SenselessMethodDuplicationInspection
A child class method whose body is a copy of the inherited method's body is dead weight: if the visibility is the same the override can simply be deleted; if only the visibility differs, the override should delegate to parent:: instead of duplicating the code.
Example
php
<?php
class Repo
{
protected function load($id)
{
$row = ['id' => $id];
return strtoupper(json_encode($row));
}
protected function save($row, $flag)
{
$this->log($row);
}
protected function count()
{
return 1;
}
protected function secret()
{
return $this->hidden();
}
private function hidden()
{
return 0;
}
public function log($x)
{
echo $x;
}
}
class MiddleRepo extends Repo {}
class UserRepo extends MiddleRepo
{
/**
* Copied from Repo.
*/
protected function load($key)
{
// same code, different comments
$row = ['id' => $id];
/** stray doc block */
return strtoupper(json_encode( $row ));
}
public function save($row, $flag)
{
$this->log($row);
}
protected function count()
{
return 2;
}
protected function secret()
{
return $this->hidden();
}
}php
<?php
class Repo
{
protected function load($id)
{
$row = ['id' => $id];
return strtoupper(json_encode($row));
}
protected function save($row, $flag)
{
$this->log($row);
}
protected function count()
{
return 1;
}
protected function secret()
{
return $this->hidden();
}
private function hidden()
{
return 0;
}
public function log($x)
{
echo $x;
}
}
class MiddleRepo extends Repo {}
class UserRepo extends MiddleRepo
{
public function save($row, $flag)
{
parent::save($row, $flag);
}
protected function count()
{
return 2;
}
protected function secret()
{
return $this->hidden();
}
}Reported:
- line 43: Method 'load' duplicates the inherited implementation; remove it.
- line 52: Method 'save' duplicates the inherited implementation; delegate to parent::save() instead.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
MAX_METHOD_SIZE | int | 20 | Methods with more top-level statements than this are skipped. |
Configure
In custos.json:
json
{
"rules": {
"SenselessMethodDuplication": {
"enabled": false,
"options": {
"MAX_METHOD_SIZE": 20
}
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore SenselessMethodDuplication
/**
* @noinspection SenselessMethodDuplicationInspection
*/