1.2.536. Identical Methods

When the parent class and the child class have the same method, the child might omit it. This reduces code duplication.

Duplicate code in methods is often the results of code evolution, where a method was copied with the hierarchy, but the original wasn’t removed.

This doesn’t apply to private methods, which are reserved for one class.

<?php

class a {
    public function foo() {
        return rand(0, 100);
    }
}

class b extends a {
    public function foo() {
        return rand(0, 100);
    }
}

?>

1.2.536.1. Suggestions

  • Drop the method from the parent class, in particular if only one child uses the method.

  • Drop the method from the child class, in particular if there are several children class

  • Use an abstract method, and make sure every child has its own implementation

  • Modify one of the methods so they are different

1.2.536.2. Specs

Short name

Classes/IdenticalMethods

Rulesets

All

Exakat since

1.8.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Features

method

Available in

Entreprise Edition, Exakat Cloud