Completed
Push — master ( 4c18be...d36967 )
by Mike
10:04
created

Compound::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Reflection\Types;
15
16
use ArrayIterator;
17
use InvalidArgumentException;
18
use IteratorAggregate;
19
use phpDocumentor\Reflection\Type;
20
use function implode;
21
22
/**
23
 * Value Object representing a Compound Type.
24
 *
25
 * A Compound Type is not so much a special keyword or object reference but is a series of Types that are separated
26
 * using an OR operator (`|`). This combination of types signifies that whatever is associated with this compound type
27
 * may contain a value with any of the given types.
28
 */
29
final class Compound implements Type, IteratorAggregate
30
{
31
    /** @var Type[] */
32
    private $types = [];
33
34
    /**
35
     * Initializes a compound type (i.e. `string|int`) and tests if the provided types all implement the Type interface.
36
     *
37
     * @param Type[] $types
38
     */
39
    public function __construct(array $types)
40
    {
41 2
        foreach ($types as $type) {
42
            $this->add($type);
43 2
        }
44
    }
45 2
46 2
    /**
47
     * Returns the type at the given index.
48
     */
49
    public function get(int $index) : ?Type
50 1
    {
51 1
        if (!$this->has($index)) {
52
            return null;
53
        }
54
55
        return $this->types[$index];
56 2
    }
57
58 2
    /**
59 1
     * Tests if this compound type has a type with the given index.
60
     */
61
    public function has(int $index) : bool
62 1
    {
63
        return isset($this->types[$index]);
64
    }
65
66
    /**
67
     * Tests if this compound type contains the given type.
68 2
     */
69
    public function contains(Type $type): bool
70 2
    {
71
        foreach ($this->types as $typePart) {
72
            // if the type is duplicate; do not add it
73
            if ((string) $typePart === (string) $type) {
74
                return true;
75
            }
76 1
        }
77
78 1
        return false;
79
    }
80
81
    /**
82
     * Returns a rendered output of the Type as it would be used in a DocBlock.
83
     */
84 1
    public function __toString() : string
85
    {
86 1
        return implode('|', $this->types);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getIterator()
93
    {
94
        return new ArrayIterator($this->types);
95
    }
96
97
    private function add(Type $type): void
98
    {
99
        // if the type is duplicate; do not add it
100
        if ($this->contains($type)) {
101
            return;
102
        }
103
104
        $this->types[] = $type;
105
    }
106
}
107