1.2.322. DI Cyclic Dependencies

When injecting dependencies, classes that mutually depend on each other is a code smell.

Dependency injection should be organized as an acyclic tree-like structure

<?php

// Classes A and B depends on each other.
class A {
    protected $b;

    public function __construct(B $b) {
        $this->b = $b;
    }
}

class B {
    public $a;

    protected function setA(A $a) {
        $this->a = $a;
    }
}
?>

See also Dependency Injection Smells.

1.2.322.1. Specs

Short name

Classes/TypehintCyclicDependencies

Rulesets

All, Changed Behavior

Exakat since

0.11.6

PHP Version

All

Severity

Time To Fix

Precision

Very high

Features

injection

Available in

Entreprise Edition, Exakat Cloud