Failed Conditions
Pull Request — master (#247)
by Michael
05:15
created

MetadataCollection::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Metadata;
6
7
use ArrayAccess;
8
use Countable;
9
use IteratorAggregate;
10
use Traversable;
11
use function array_key_exists;
12
use function array_values;
13
use function assert;
14
use function count;
15
use function sprintf;
16
17
/**
18
 * @internal
19
 */
20
final class MetadataCollection implements ArrayAccess, Countable, IteratorAggregate
21
{
22
    /** @var array<string, AnnotationMetadata> */
23
    private $metadata = [];
24
25 292
    public function __construct(AnnotationMetadata ...$metadatas)
26
    {
27 292
        $this->add(...$metadatas);
28 292
    }
29
30 292
    public function add(AnnotationMetadata ...$metadatas) : void
31
    {
32 292
        foreach ($metadatas as $metadata) {
33 292
            assert(! isset($this[$metadata->getName()]), sprintf('Metadata with name %s already exists.', $metadata->getName()));
34
35 292
            $this->metadata[$metadata->getName()] = $metadata;
36
        }
37 292
    }
38
39
    public function include(self $other) : void
40
    {
41
        $this->add(...$other);
42
    }
43
44
    /**
45
     * @param string $name
46
     *
47
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
48
     */
49 260
    public function offsetGet($name) : AnnotationMetadata
50
    {
51 260
        assert(isset($this[$name]), sprintf('Metadata for name %s does not exist', $name));
52
53 260
        return $this->metadata[$name];
54
    }
55
56
    /**
57
     * @param null               $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
58
     * @param AnnotationMetadata $metadata
59
     *
60
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
61
     */
62 252
    public function offsetSet($name, $metadata) : void
63
    {
64 252
        assert($name === null, 'Setting named metadata is not supported.');
65
66 252
        $this->add($metadata);
67 252
    }
68
69
    /**
70
     * @param string $name
71
     *
72
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
73
     */
74 292
    public function offsetExists($name) : bool
75
    {
76 292
        return array_key_exists($name, $this->metadata);
77
    }
78
79
    /**
80
     * @param string $name
81
     *
82
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
83
     */
84 1
    public function offsetUnset($name) : void
85
    {
86 1
        assert(isset($this[$name]));
87
88 1
        unset($this->metadata[$name]);
89 1
    }
90
91 2
    public function count() : int
92
    {
93 2
        return count($this->metadata);
94
    }
95
96
    /**
97
     * @return Traversable<AnnotationMetadata>
98
     */
99 1
    public function getIterator() : Traversable
100
    {
101 1
        yield from array_values($this->metadata);
102 1
    }
103
}
104