Passed
Push — type-parser ( bda507 )
by Michael
03:05
created

IntersectionType::getSubTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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