Passed
Push — master ( ba4ca1...142814 )
by Evgeniy
58s queued 11s
created

Compiler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 40
dl 0
loc 91
ccs 42
cts 42
cp 1
rs 10
c 2
b 1
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compile() 0 7 1
A autowire() 0 11 3
A compileAlias() 0 8 2
A registerInterface() 0 4 1
A registerClass() 0 4 1
A getDependenciesString() 0 7 2
A compileClasses() 0 13 2
A getClass() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI;
6
7
use Cekta\DI\Loader\Alias;
8
use Cekta\DI\Loader\Factory;
9
use Cekta\DI\Loader\FactoryVariadic;
10
11
class Compiler
12
{
13
    /**
14
     * @var Reflection
15
     */
16
    private $reflection;
17
    private $classes = [];
18
    private $alias = [];
19
    private $variadic = [];
20
21 15
    public function __construct(Reflection $reflection)
22
    {
23 15
        $this->reflection = $reflection;
24 15
    }
25
26 9
    public function autowire(string $name): self
27
    {
28 9
        if (!$this->reflection->isInstantiable($name)) {
29 3
            return $this;
30
        }
31 6
        if ($this->reflection->isVariadic($name)) {
32 3
            $this->variadic[] = true;
33
        }
34 6
        return $this->registerClass(
35 6
            $name,
36 6
            ...$this->reflection->getDependencies($name)
37
        );
38
    }
39
40 9
    public function registerClass(string $name, string ...$dependencies): self
41
    {
42 9
        $this->classes[$name] = $dependencies;
43 9
        return $this;
44
    }
45
46 3
    public function registerInterface(string $name, string $implementation): self
47
    {
48 3
        $this->alias[$name] = $implementation;
49 3
        return $this;
50
    }
51
52 15
    public function compile(): string
53
    {
54 5
        return "<?php
55
56
declare(strict_types=1);
57
58 15
return [{$this->compileAlias()}{$this->compileClasses()}
59
];";
60
    }
61
62 15
    private function compileAlias(): string
63
    {
64 15
        $compiledContainers = '';
65 15
        $class = Alias::class;
66 15
        foreach ($this->alias as $name => $implementation) {
67 3
            $compiledContainers .= "\n    '$name' => new $class('$implementation'),";
68
        }
69 15
        return $compiledContainers;
70
    }
71
72 15
    private function compileClasses(): string
73
    {
74 15
        $compiledContainers = '';
75 15
        foreach ($this->classes as $name => $dependencies) {
76 9
            $class = $this->getClass($name);
77
            $compiledContainers .= <<<TAG
78
79 9
    '$name' => new $class(
80 9
        '$name',{$this->getDependenciesString(...$dependencies)}
81
    ),
82
TAG;
83
        }
84 15
        return $compiledContainers;
85
    }
86
87 9
    private function getClass($name): string
88
    {
89 9
        if (in_array($name, $this->variadic)) {
90 3
            return FactoryVariadic::class;
91
        }
92 6
        return Factory::class;
93
    }
94
95 9
    private function getDependenciesString(string ...$dependencies): string
96
    {
97 9
        $result = '';
98 9
        foreach ($dependencies as $dependency) {
99 9
            $result .= PHP_EOL . "        '$dependency',";
100
        }
101 9
        return $result;
102
    }
103
}
104