Passed
Push — visitor ( 831732...36593a )
by Akihito
01:47
created

ContainerFactory::getModule()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 9.9666
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\Compiler;
8
use Ray\Compiler\CompileNullObject;
9
10
use function array_shift;
11
12
final class ContainerFactory
13
{
14
    /**
15
     * @param AbstractModule|non-empty-array<AbstractModule>|null $module Module(s)
0 ignored issues
show
Documentation Bug introduced by
The doc comment AbstractModule|non-empty...ay<AbstractModule>|null at position 2 could not be parsed: Unknown type name 'non-empty-array' at position 2 in AbstractModule|non-empty-array<AbstractModule>|null.
Loading history...
16
     */
17
    public function __invoke($module, string $classDir): Container
18
    {
19
        $oneModule = $this->getModule($module);
20
        // install built-in module
21
        $appModule = (new BuiltinModule())($oneModule);
22
        $container = $appModule->getContainer();
23
        // Compile null objects
24
        (new CompileNullObject())($container, $classDir);
25
        // Compile aspects
26
        $container->weaveAspects(new Compiler($classDir));
27
28
        return $container;
29
    }
30
31
    /**
32
     * @param AbstractModule|non-empty-array<AbstractModule>|null $module Module(s)
0 ignored issues
show
Documentation Bug introduced by
The doc comment AbstractModule|non-empty...ay<AbstractModule>|null at position 2 could not be parsed: Unknown type name 'non-empty-array' at position 2 in AbstractModule|non-empty-array<AbstractModule>|null.
Loading history...
33
     */
34
    private function getModule($module): AbstractModule
35
    {
36
        if ($module instanceof AbstractModule) {
37
            return $module;
38
        }
39
40
        if ($module === null) {
41
            return new NullModule();
42
        }
43
44
        $modules = $module;
45
        $oneModule = array_shift($modules);
46
        foreach ($modules as $module) {
0 ignored issues
show
introduced by
$module is overwriting one of the parameters of this function.
Loading history...
47
            $oneModule->install($module);
48
        }
49
50
        return $oneModule;
51
    }
52
}
53