Completed
Push — develop ( 15ec3c...8b62a8 )
by Baptiste
02:19
created

Set   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fromString() 0 9 2
A compile() 0 3 1
A __toString() 0 3 1
A __invoke() 0 3 1
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
    Exception\ValueNotSupported,
9
    Lazy\Set as LazySet,
10
    Compilation\Service\Constructor as CompiledConstructor,
11
    Compilation\Service\Constructor\Set as CompiledSet,
12
    Compilation\Service\Argument as CompiledArgument
13
};
14
use Innmind\Immutable\Str;
15
16
final class Set implements Constructor
17
{
18
    private $type;
19
20 9
    private function __construct(string $type)
21
    {
22 9
        $this->type = $type;
23 9
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 15
    public static function fromString(Str $value): Constructor
29
    {
30 15
        if (!$value->matches('~^set<\S+>$~')) {
31 11
            throw new ValueNotSupported((string) $value);
32
        }
33
34 9
        $components = $value->capture('~^set<(?<type>\S+)>$~');
35
36 9
        return new self((string) $components->get('type'));
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 3
    public function __invoke(...$arguments): object
43
    {
44 3
        return LazySet::of($this->type, ...$arguments);
45
    }
46
47 3
    public function compile(CompiledArgument ...$arguments): CompiledConstructor
48
    {
49 3
        return new CompiledSet($this->type, ...$arguments);
50
    }
51
52 2
    public function __toString(): string
53
    {
54 2
        return sprintf('set<%s>', $this->type);
55
    }
56
}
57