Completed
Push — 2.x ( 07ed38...d68955 )
by Akihito
03:36
created

AbstractModule::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the Ray.Di package.
6
 *
7
 * @license http://opensource.org/licenses/MIT MIT
8
 */
9
namespace Ray\Di;
10
11
use Ray\Aop\AbstractMatcher;
12
use Ray\Aop\Matcher;
13
use Ray\Aop\PriorityPointcut;
14
15
abstract class AbstractModule
16
{
17
    /**
18
     * @var Matcher
19
     */
20
    protected $matcher;
21
22
    /**
23
     * @var AbstractModule|null
24
     */
25
    protected $lastModule;
26
27
    /**
28
     * @var Container
29
     */
30
    private $container;
31
32 50
    public function __construct(
33
        self $module = null
34
    ) {
35 50
        $this->lastModule = $module;
0 ignored issues
show
Documentation Bug introduced by
It seems like $module can also be of type object<self>. However, the property $lastModule is declared as type object<Ray\Di\AbstractModule>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36 50
        $this->activate();
37 49
        if ($module instanceof self) {
38 5
            $this->container->merge($module->getContainer());
39
        }
40 49
    }
41
42
    public function __toString()
43
    {
44
        $log = [];
45
        foreach ($this->getContainer()->getContainer() as $dependencyIndex => $dependency) {
46
            $log[] = sprintf(
47
                '%s => %s',
48
                $dependencyIndex,
49
                (string) $dependency
50
            );
51
        }
52
        sort($log);
53
54
        return implode(PHP_EOL, $log);
55
    }
56
57
    /**
58
     * Install module
59
     */
60 41
    public function install(self $module)
61
    {
62 41
        $this->getContainer()->merge($module->getContainer());
63 41
    }
64
65
    /**
66
     * Override module
67
     */
68 1
    public function override(self $module)
69
    {
70 1
        $module->getContainer()->merge($this->container);
71 1
        $this->container = $module->getContainer();
72 1
    }
73
74
    /**
75
     * Return container
76
     *
77
     * @return Container
78
     */
79 51
    public function getContainer() : Container
80
    {
81 51
        if (! $this->container) {
82 1
            $this->activate();
83
        }
84
85 51
        return $this->container;
86
    }
87
88
    /**
89
     * Bind interceptor
90
     *
91
     * @param AbstractMatcher $classMatcher
92
     * @param AbstractMatcher $methodMatcher
93
     * @param string[]        $interceptors
94
     */
95 40
    public function bindInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors)
96
    {
97 40
        $pointcut = new Pointcut($classMatcher, $methodMatcher, $interceptors);
98 40
        $this->container->addPointcut($pointcut);
99 40
        foreach ($interceptors as $interceptor) {
100 40
            (new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
101
        }
102 40
    }
103
104
    /**
105
     * Bind interceptor early
106
     *
107
     * @param AbstractMatcher $classMatcher
108
     * @param AbstractMatcher $methodMatcher
109
     * @param array           $interceptors
110
     */
111 1
    public function bindPriorityInterceptor(AbstractMatcher $classMatcher, AbstractMatcher $methodMatcher, array $interceptors)
112
    {
113 1
        $pointcut = new PriorityPointcut($classMatcher, $methodMatcher, $interceptors);
114 1
        $this->container->addPointcut($pointcut);
115 1
        foreach ($interceptors as $interceptor) {
116 1
            (new Bind($this->container, $interceptor))->to($interceptor)->in(Scope::SINGLETON);
117
        }
118 1
    }
119
120
    /**
121
     * Rename binding name
122
     *
123
     * @param string $interface       Interface
124
     * @param string $newName         New binding name
125
     * @param string $sourceName      Original binding name
126
     * @param string $targetInterface Original interface
127
     */
128 1
    public function rename(string $interface, string $newName, string $sourceName = Name::ANY, string $targetInterface = '')
129
    {
130 1
        $targetInterface = $targetInterface ?: $interface;
131 1
        if ($this->lastModule instanceof self) {
132 1
            $this->lastModule->getContainer()->move($interface, $sourceName, $targetInterface, $newName);
133
        }
134 1
    }
135
136
    /**
137
     * Configure binding
138
     */
139
    abstract protected function configure();
140
141
    /**
142
     * Bind interface
143
     */
144 49
    protected function bind(string $interface = '') : Bind
145
    {
146 49
        return new Bind($this->getContainer(), $interface);
147
    }
148
149
    /**
150
     * Activate bindings
151
     */
152 51
    private function activate()
153
    {
154 51
        $this->container = new Container;
155 51
        $this->matcher = new Matcher;
156 51
        $this->configure();
157 50
    }
158
}
159