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

UnionType::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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