Completed
Push — 2.x ( ae2cb5...acdcc2 )
by Akihito
18s queued 13s
created

ContainerFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 30
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\Compiler;
8
use Ray\Di\MultiBinding\MultiBindingModule;
9
10
use function array_merge;
11
use function array_shift;
12
use function is_array;
13
14
final class ContainerFactory
15
{
16
    /**
17
     * @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...
18
     */
19
    public function __invoke($module, string $classDir): Container
20
    {
21
        $module = $module ?? new NullModule();
22
        $builtInModules = [
23
            new AssistedModule(),
24
            new ProviderSetModule(),
25
            new MultiBindingModule(),
26
        ];
27
        $modules = array_merge($builtInModules, is_array($module) ? $module : [$module]);
28
        $baseModule = array_shift($modules);
29
        foreach ($modules as $module) {
0 ignored issues
show
introduced by
$module is overwriting one of the parameters of this function.
Loading history...
30
            $baseModule->install($module);
31
        }
32
33
        $container = $baseModule->getContainer();
34
        $container->map(static function (DependencyInterface $dependency) use ($classDir) {
35
            if ($dependency instanceof NullObjectDependency) {
36
                return $dependency->toNull($classDir);
37
            }
38
39
            return $dependency;
40
        });
41
        $container->weaveAspects(new Compiler($classDir));
42
43
        return $container;
44
    }
45
}
46