Failed Conditions
Push — type ( 257e93...5997a7 )
by Michael
02:15
created

IntersectionType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 21
dl 0
loc 56
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A describe() 0 13 2
A __construct() 0 5 1
A validate() 0 11 3
A acceptsNull() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Type;
6
7
use function array_map;
8
use function assert;
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
    public function __construct(Type ...$subTypes)
21
    {
22
        assert(count($subTypes) >= 2);
23
24
        $this->subTypes = $subTypes;
25
    }
26
27
    public function describe() : string
28
    {
29
        return implode(
30
            '&',
31
            array_map(
32
                static function (Type $subType) : string {
33
                    if ($subType instanceof CompositeType) {
34
                        return sprintf('(%s)', $subType->describe());
35
                    }
36
37
                    return $subType->describe();
38
                },
39
                $this->subTypes
40
            )
41
        );
42
    }
43
44
    /**
45
     * @param mixed $value
46
     */
47
    public function validate($value) : bool
48
    {
49
        foreach ($this->subTypes as $subType) {
50
            if ($subType->validate($value)) {
51
                continue;
52
            }
53
54
            return false;
55
        }
56
57
        return true;
58
    }
59
60
    public function acceptsNull() : bool
61
    {
62
        foreach ($this->subTypes as $subType) {
63
            if ($subType->acceptsNull()) {
64
                continue;
65
            }
66
67
            return false;
68
        }
69
70
        return true;
71
    }
72
}
73