Construct::compile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service\Constructor;
5
6
use Innmind\Compose\{
7
    Definition\Service\Constructor,
8
    Lazy,
9
    Compilation\Service\Constructor as CompiledConstructor,
10
    Compilation\Service\Constructor\Construct as CompiledConstruct,
11
    Compilation\Service\Argument as CompiledArgument
12
};
13
use Innmind\Immutable\{
14
    Str,
15
    Sequence
16
};
17
18
final class Construct implements Constructor
19
{
20
    private $value;
21
22 224
    private function __construct(string $value)
23
    {
24 224
        $this->value = $value;
25 224
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 224
    public static function fromString(Str $value): Constructor
31
    {
32 224
        return new self((string) $value);
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 115
    public function __invoke(...$arguments): object
39
    {
40 115
        $class = (string) $this->value;
41
42 115
        $arguments = Sequence::of(...$arguments)->map(static function($argument) {
43 19
            if ($argument instanceof Lazy) {
44 15
                return $argument->load();
45
            }
46
47 16
            return $argument;
48 115
        });
49
50 114
        return new $class(...$arguments);
51
    }
52
53 7
    public function compile(CompiledArgument ...$arguments): CompiledConstructor
54
    {
55 7
        return new CompiledConstruct($this->value, ...$arguments);
56
    }
57
58 24
    public function __toString(): string
59
    {
60 24
        return $this->value;
61
    }
62
}
63