UnionType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 19
dl 0
loc 52
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A describe() 0 9 1
A validate() 0 11 3
A acceptsNull() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Metadata\Type;
6
7
use function array_map;
8
use function assert;
9
use function count;
10
use function implode;
11
12
final class UnionType implements Type
13
{
14
    /** @var Type[] */
15
    private $subTypes;
16
17 28
    public function __construct(Type ...$subTypes)
18
    {
19 28
        assert(count($subTypes) !== 0);
20
21 28
        $this->subTypes = $subTypes;
22 28
    }
23
24 12
    public function describe() : string
25
    {
26 12
        return implode(
27 12
            '|',
28 12
            array_map(
29
                static function (Type $subType) : string {
30 12
                    return $subType->describe();
31 12
                },
32 12
                $this->subTypes
33
            )
34
        );
35
    }
36
37
    /**
38
     * @param mixed $value
39
     */
40 9
    public function validate($value) : bool
41
    {
42 9
        foreach ($this->subTypes as $subType) {
43 9
            if (! $subType->validate($value)) {
44 7
                continue;
45
            }
46
47 4
            return true;
48
        }
49
50 5
        return false;
51
    }
52
53 2
    public function acceptsNull() : bool
54
    {
55 2
        foreach ($this->subTypes as $subType) {
56 2
            if (! $subType->acceptsNull()) {
57 2
                continue;
58
            }
59
60 1
            return true;
61
        }
62
63 1
        return false;
64
    }
65
}
66