Skip to content

EfferentObjectCoupling ​

info off by default

Group: Architecture · PhpStorm name: EfferentObjectCouplingInspection

A class that refers to many distinct other classes depends on too much of the code base; it is hard to change and to test in isolation. The rule counts the distinct class names a class mentions and flags it once the count reaches a limit.

Example ​

With optionCouplingLimit = 2:

php
<?php
namespace Shop;

use Psr\Log\LoggerInterface;

class Money {}

class Wallet {
    public function add(Money $m): void {}
}

class Checkout extends Wallet {
    /** @var \Some\DocOnly */
    private $log;
    public function __construct(LoggerInterface $log, int $tries) { $this->log = $log; }
    public function pay(Money $m): self { return $this; }
}

class Basket {
    public function total(int $cents): string { return (string) $cents; }
}

Reported:

  • line 12: Depends on 3 distinct classes; consider splitting it up.

Options ​

OptionTypeDefaultEffect
optionCouplingLimitint20Minimum number of distinct referenced classes that triggers a report (inclusive). The upstream conformance case runs with 2.

Configure ​

In custos.json:

json
{
  "rules": {
    "EfferentObjectCoupling": {
      "enabled": true,
      "options": {
        "optionCouplingLimit": 20
      }
    }
  }
}

Suppress ​

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

php
// @custos-ignore EfferentObjectCoupling

/**
 * @noinspection EfferentObjectCouplingInspection
 */

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