Set::fromString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Argument\Type;
5
6
use Innmind\Compose\{
7
    Definition\Argument\Type,
8
    Exception\ValueNotSupported
9
};
10
use Innmind\Immutable\{
11
    SetInterface,
12
    Str
13
};
14
15
final class Set implements Type
16
{
17
    private const PATTERN = '~^set<(?<type>.+)>$~';
18
    private $type;
19
20 4
    public function __construct(string $type)
21
    {
22 4
        $this->type = $type;
23 4
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function accepts($value): bool
29
    {
30 2
        if (!$value instanceof SetInterface) {
31 1
            return false;
32
        }
33
34 2
        return (string) $value->type() === $this->type;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 13
    public static function fromString(Str $value): Type
41
    {
42 13
        if (!$value->matches(self::PATTERN)) {
43 11
            throw new ValueNotSupported((string) $value);
44
        }
45
46 2
        $components = $value->capture(self::PATTERN);
47
48 2
        return new self((string) $components->get('type'));
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function __toString(): string
55
    {
56 1
        return sprintf('set<%s>', $this->type);
57
    }
58
}
59