Set   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

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