Failed Conditions
Pull Request — master (#247)
by Michael
13:16
created

MetadataCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 14
dl 0
loc 77
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 5 1
A add() 0 6 2
A __construct() 0 3 1
A offsetUnset() 0 5 1
A getIterator() 0 3 1
A offsetExists() 0 3 1
A offsetGet() 0 5 1
A count() 0 3 1
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
    public function __construct(AnnotationMetadata ...$metadatas)
26
    {
27
        $this->add(...$metadatas);
28
    }
29
30
    public function add(AnnotationMetadata ...$metadatas) : void
31
    {
32
        foreach ($metadatas as $metadata) {
33
            assert(! isset($this[$metadata->getName()]), sprintf('Metadata with name %s already exists.', $metadata->getName()));
34
35
            $this->metadata[$metadata->getName()] = $metadata;
36
        }
37
    }
38
39
    /**
40
     * @param string $name
41
     *
42
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
43
     */
44
    public function offsetGet($name) : AnnotationMetadata
45
    {
46
        assert(isset($this[$name]), sprintf('Metadata for name %s does not exist', $name));
47
48
        return $this->metadata[$name];
49
    }
50
51
    /**
52
     * @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...
53
     * @param AnnotationMetadata $metadata
54
     *
55
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
56
     */
57
    public function offsetSet($name, $metadata) : void
58
    {
59
        assert($name === null, 'Setting named metadata is not supported.');
60
61
        $this->add($metadata);
62
    }
63
64
    /**
65
     * @param string $name
66
     *
67
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
68
     */
69
    public function offsetExists($name) : bool
70
    {
71
        return array_key_exists($name, $this->metadata);
72
    }
73
74
    /**
75
     * @param string $name
76
     *
77
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
78
     */
79
    public function offsetUnset($name) : void
80
    {
81
        assert(isset($this[$name]));
82
83
        unset($this->metadata[$name]);
84
    }
85
86
    public function count() : int
87
    {
88
        return count($this->metadata);
89
    }
90
91
    /**
92
     * @return Traversable<AnnotationMetadata>
93
     */
94
    public function getIterator() : Traversable
95
    {
96
        yield from array_values($this->metadata);
97
    }
98
}
99