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

IntersectionType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 8
eloc 18
dl 0
loc 53
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

4 Methods

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