Failed Conditions
Pull Request — master (#256)
by Michael
02:33
created

IntersectionType::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 array_map;
9
use function count;
10
use function implode;
11
use function sprintf;
12
13
/**
14
 * @internal
15
 */
16
final class IntersectionType implements CompositeType
17
{
18
    /** @var Type[] */
19
    private $subTypes;
20
21 27
    public function __construct(Type ...$subTypes)
22
    {
23 27
        if (count($subTypes) < 2) {
24 1
            throw CompositeTypeRequiresAtLeastTwoSubTypes::fromInsufficientAmount(count($subTypes));
25
        }
26
27 27
        $this->subTypes = $subTypes;
28 27
    }
29
30
    /**
31
     * @return Type[]
32
     */
33 1
    public function getSubTypes() : array
34
    {
35 1
        return $this->subTypes;
36
    }
37
38 3
    public function describe() : string
39
    {
40 3
        return implode(
41 3
            '&',
42 3
            array_map(
43
                static function (Type $subType) : string {
44 3
                    if ($subType instanceof CompositeType) {
45 1
                        return sprintf('(%s)', $subType->describe());
46
                    }
47
48 3
                    return $subType->describe();
49 3
                },
50 3
                $this->subTypes
51
            )
52
        );
53
    }
54
55
    /**
56
     * @param mixed $value
57
     */
58 20
    public function validate($value) : bool
59
    {
60 20
        foreach ($this->subTypes as $subType) {
61 20
            if ($subType->validate($value)) {
62 5
                continue;
63
            }
64
65 17
            return false;
66
        }
67
68 3
        return true;
69
    }
70
}
71