EfferentObjectCoupling
info off by defaultGroup: 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
| Option | Type | Default | Effect |
|---|---|---|---|
optionCouplingLimit | int | 20 | Minimum 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
*/