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

UnionType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 11
dl 0
loc 41
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubTypes() 0 3 1
A validate() 0 11 3
A describe() 0 3 1
A __construct() 0 7 2
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