Skip to content

SenselessMethodDuplication ​

info on by default quick-fix

Group: 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 ​

OptionTypeDefaultEffect
MAX_METHOD_SIZEint20Methods 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
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.