CRITICAL
Rule Definition
Changing a class (or interface) requires prior understanding, which, in turn, is more complicated for classes (or interfaces) with many methods. Classes (or interfaces) that are deep down in the class (or interface) hierarchy potentially inherit many methods from super-classes (or super-interfaces). Moreover, the definitions of inherited methods are not local to the class making it even harder to understand.
Complete testing requires coverage of all execution paths. The number of possible execution paths of a class increases with the number of methods and their control flow complexity. Classes (or interfaces) that are deep down in the classes hierarchy potentially inherit many methods from super-classes (super-interfaces). Due to late binding, super-class (or super-interfaces) methods need to be tested again in the sub-class (or sub-interfaces) context. This makes it potentially harder to test classes (interfaces) deep down in the classes (interfaces) hierarchy.
Technical or framework classes which are evolving and will not be changed often should not be concerned by this rule.
Remediation
Review the architecture of the application to reduce the depth of the Inheritence Tree.
Violation Code Sample
class ClassTest1{
public function __construct() {
}
}
class ClassTest2 extends ClassTest1{{
public function __construct(ArrayObject $arrayObj, $number = 0) {
/* do stuff with $arrayObj and $number */
}
}
class ClassTest2 extends ClassTest3 {
public function __construct() {
// do stuff
}
}
//........................
class ClassTest10 extends ClassTest11 {
public function __construct() {
// do stuff
}
}
class ClassTest11 extends ClassTest12 {
public function __construct() {
// do stuff
}
}
Reference
PHPMD - http://phpmd.org/rules/design.html#depthofinheritance
Related Technologies
Technical Criterion
CWE-1074 - Class with Excessively Deep Inheritance
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.