Failed Conditions
Push — annotation-metadata ( 414255 )
by Michael
02:42
created

MetadataCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 68%

Importance

Changes 0
Metric Value
wmc 9
eloc 14
dl 0
loc 77
ccs 17
cts 25
cp 0.68
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
final class MetadataCollection implements ArrayAccess, Countable, IteratorAggregate
18
{
19
    /** @var array<string, AnnotationMetadata> */
20
    private $metadata = [];
21
22 278
    public function __construct(AnnotationMetadata ...$metadatas)
23
    {
24 278
        $this->add(...$metadatas);
25 278
    }
26
27 278
    public function add(AnnotationMetadata ...$metadatas) : void
28
    {
29 278
        foreach ($metadatas as $metadata) {
30 278
            assert(! isset($this[$metadata->getName()]), sprintf('Metadata with name %s already exists.', $metadata->getName()));
31
32 278
            $this->metadata[$metadata->getName()] = $metadata;
33
        }
34 278
    }
35
36
    /**
37
     * @param string $name
38
     *
39
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
40
     */
41 242
    public function offsetGet($name) : AnnotationMetadata
42
    {
43 242
        assert(isset($this[$name]), sprintf('Metadata for name %s does not exist', $name));
44
45 242
        return $this->metadata[$name];
46
    }
47
48
    /**
49
     * @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...
50
     * @param AnnotationMetadata $metadata
51
     *
52
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
53
     */
54 231
    public function offsetSet($name, $metadata) : void
55
    {
56 231
        assert($name === null, 'Setting named metadata is not supported.');
57
58 231
        $this->add($metadata);
59 231
    }
60
61
    /**
62
     * @param string $name
63
     *
64
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
65
     */
66 278
    public function offsetExists($name) : bool
67
    {
68 278
        return array_key_exists($name, $this->metadata);
69
    }
70
71
    /**
72
     * @param string $name
73
     *
74
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
75
     */
76
    public function offsetUnset($name) : void
77
    {
78
        assert(isset($this[$name]));
79
80
        unset($this->metadata[$name]);
81
    }
82
83
    public function count() : int
84
    {
85
        return count($this->metadata);
86
    }
87
88
    /**
89
     * @return Traversable<AnnotationMetadata>
90
     */
91
    public function getIterator() : Traversable
92
    {
93
        yield from array_values($this->metadata);
94
    }
95
}
96