Skip to content

LongInheritanceChain ​

warning off by default

Group: Architecture · PhpStorm name: LongInheritanceChainInspection

Deep extends chains spread behaviour over many levels and make a class hard to understand. The rule counts the ancestors of a class and complains when the chain reaches a threshold, with carve-outs for exception hierarchies, test cases and well-known framework base classes.

Example ​

Default threshold 3:

php
<?php
namespace App {
    class Node {}
    class Branch extends Node {}
    class Twig extends Branch {}
    class Leaf extends Twig {}

    /** @deprecated */
    class OldLeaf extends Twig {}
    class TwigException extends Twig {}

    abstract class Base extends Branch {}
    class Concrete extends Base {}

    class Err1 extends \RuntimeException {}
    class Err2Exception extends Err1 {}
    class Err3 extends Err2Exception {}
    class Err4 extends Err3 {}
}

namespace yii\base {
    class Component {}
}
namespace App\Widgets {
    class Panel extends \yii\base\Component {}
    class FancyPanel extends Panel {}
    class MegaPanel extends FancyPanel {}
}

Reported:

  • line 6: 3 levels of parent classes; prefer composition over deep inheritance.
  • line 27: 3 levels of parent classes; prefer composition over deep inheritance.

Options ​

OptionTypeDefaultEffect
COMPLAIN_THRESHOLDint3Minimum ancestor count (inclusive) that triggers a report.

Configure ​

In custos.json:

json
{
  "rules": {
    "LongInheritanceChain": {
      "enabled": true,
      "options": {
        "COMPLAIN_THRESHOLD": 3
      }
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore LongInheritanceChain

/**
 * @noinspection LongInheritanceChainInspection
 */

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