Failed Conditions
Push — master ( ecccd5...ae55c7 )
by Marco
33s
created

UnionType::getSubTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Type;
6
7
use Doctrine\Annotations\Type\Exception\CompositeTypeRequiresAtLeastTwoSubTypes;
8
use function count;
9
10
/**
11
 * @internal
12
 */
13
final class UnionType implements CompositeType
14
{
15
    /** @var Type[] */
16
    private $subTypes;
17
18 31
    public function __construct(Type ...$subTypes)
19
    {
20 31
        if (count($subTypes) < 2) {
21 1
            throw CompositeTypeRequiresAtLeastTwoSubTypes::fromInsufficientAmount(count($subTypes));
22
        }
23
24 31
        $this->subTypes = $subTypes;
25 31
    }
26
27
    /**
28
     * @return Type[]
29
     */
30 1
    public function getSubTypes() : array
31
    {
32 1
        return $this->subTypes;
33
    }
34
35 10
    public function describe() : string
36
    {
37 10
        return CompositeTypeDescriber::describe('|', $this->subTypes);
38
    }
39
40
    /**
41
     * @param mixed $value
42
     */
43 19
    public function validate($value) : bool
44
    {
45 19
        foreach ($this->subTypes as $subType) {
46 19
            if (! $subType->validate($value)) {
47 18
                continue;
48
            }
49
50 10
            return true;
51
        }
52
53 11
        return false;
54
    }
55
}
56