Failed Conditions
Push — type ( 257e93...5997a7 )
by Michael
02:15
created

UnionType::acceptsNull()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Type;
6
7
use function array_map;
8
use function assert;
9
use function count;
10
use function implode;
11
use function sprintf;
12
13
/**
14
 * @internal
15
 */
16
final class UnionType implements CompositeType
17
{
18
    /** @var Type[] */
19
    private $subTypes;
20
21
    public function __construct(Type ...$subTypes)
22
    {
23
        assert(count($subTypes) >= 2);
24
25
        $this->subTypes = $subTypes;
26
    }
27
28
    public function describe() : string
29
    {
30
        return implode(
31
            '|',
32
            array_map(
33
                static function (Type $subType) : string {
34
                    if ($subType instanceof CompositeType) {
35
                        return sprintf('(%s)', $subType->describe());
36
                    }
37
38
                    return $subType->describe();
39
                },
40
                $this->subTypes
41
            )
42
        );
43
    }
44
45
    /**
46
     * @param mixed $value
47
     */
48
    public function validate($value) : bool
49
    {
50
        foreach ($this->subTypes as $subType) {
51
            if (! $subType->validate($value)) {
52
                continue;
53
            }
54
55
            return true;
56
        }
57
58
        return false;
59
    }
60
61
    public function acceptsNull() : bool
62
    {
63
        foreach ($this->subTypes as $subType) {
64
            if (! $subType->acceptsNull()) {
65
                continue;
66
            }
67
68
            return true;
69
        }
70
71
        return false;
72
    }
73
}
74