Map   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 48
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 3 1
A fromString() 0 11 2
A accepts() 0 8 3
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
    MapInterface,
12
    Str
13
};
14
15
final class Map implements Type
16
{
17
    private const PATTERN = '~^map<(?<key>.+), ?(?<value>.+)>$~';
18
    private $key;
19
    private $value;
20
21 4
    public function __construct(string $key, string $value)
22
    {
23 4
        $this->key = $key;
24 4
        $this->value = $value;
25 4
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function accepts($value): bool
31
    {
32 2
        if (!$value instanceof MapInterface) {
33 1
            return false;
34
        }
35
36 2
        return (string) $value->keyType() === $this->key &&
37 2
            (string) $value->valueType() === $this->value;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 15
    public static function fromString(Str $value): Type
44
    {
45 15
        if (!$value->matches(self::PATTERN)) {
46 13
            throw new ValueNotSupported((string) $value);
47
        }
48
49 2
        $components = $value->capture(self::PATTERN);
50
51 2
        return new self(
52 2
            (string) $components->get('key'),
53 2
            (string) $components->get('value')
54
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function __toString(): string
61
    {
62 1
        return sprintf('map<%s, %s>', $this->key, $this->value);
63
    }
64
}
65