Construct   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 43
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 3 1
A __construct() 0 3 1
A __toString() 0 3 1
A compile() 0 3 1
A __invoke() 0 13 2
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