Passed
Push — 2.x ( 6f8018...f6f39e )
by Akihito
02:47 queued 15s
created

ContainerFactory::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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