1.2.310. Courier Anti-Pattern

The courier anti-pattern is the storage of a dependency by a class, in order to create an instance that requires this dependency.

The class itself doesn’t actually need this dependency, but has a dependency to a class that requires it. The alternative here is to inject Foo instead of Bar.

<?php

// The foo class requires bar
class Foo {
    public function __construct(Bar $b) {
    }
}

// Class A doesn't depends on Bar, but depends on Foo
// Class A never uses Bar, but only uses Foo.
class A {
    private $courier;

    public function __construct(Bar $courier) {
        $this->courier = $courier;
    }

    public function Afoo() {
        $b = new Foo($this->courier);
    }

}

?>

See also Courier Anti-pattern.

1.2.310.1. Specs

Short name

Patterns/CourrierAntiPattern

Rulesets

All, Appinfo, CE, Changed Behavior

Exakat since

0.11.6

PHP Version

All

Severity

Time To Fix

Precision

Very high

Features

pattern

Available in

Entreprise Edition, Community Edition, Exakat Cloud