Set   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 15 3
A __construct() 0 3 1
D process() 0 27 9
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Config\Property;
5
6
use Innmind\Config\{
7
    Property,
8
    Properties,
9
    Exception\SchemaNotParseable,
10
    Exception\InvalidArgumentException,
11
};
12
use Innmind\Immutable;
13
14
final class Set implements Property
15
{
16
    private const PATTERN = '~^set<(?<type>.+)>\+?$~';
17
18
    private $type;
19
    private $requiresValue = false;
20
21 15
    private function __construct(string $type)
22
    {
23 15
        $this->type = $type;
24 15
    }
25
26 18
    public static function build(Immutable\Str $schema, Properties $properties): Property
27
    {
28 18
        if (!$schema->matches(self::PATTERN)) {
29 4
            throw new SchemaNotParseable((string) $schema);
30
        }
31
32 15
        $self = new self(
33 15
            (string) $schema->capture(self::PATTERN)->get('type')
34
        );
35
36 15
        if ((string) $schema->substring(-1) === '+') {
37 6
            $self->requiresValue = true;
38
        }
39
40 15
        return $self;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 10
    public function process($value)
47
    {
48 10
        if ($value instanceof Immutable\SetInterface && (string) $value->type() === $this->type) {
49 1
            if ($this->requiresValue && $value->size() === 0) {
50 1
                throw new InvalidArgumentException;
51
            }
52
53
            return $value;
54
        }
55
56 9
        if ($value instanceof Immutable\SetInterface) {
57 1
            throw new InvalidArgumentException;
58
        }
59
60 8
        $value = $value ?? [];
61
62 8
        if (!is_array($value)) {
63 2
            throw new InvalidArgumentException;
64
        }
65
66 6
        $set = Immutable\Set::of($this->type, ...$value);
67
68 6
        if ($this->requiresValue && $set->size() === 0) {
69 2
            throw new InvalidArgumentException;
70
        }
71
72 4
        return $set;
73
    }
74
}
75