Failed Conditions
Push — new-parser-ast-metadata ( 6127e0...5a4a16 )
by Michael
12s
created

MetadataCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 9
eloc 14
dl 0
loc 71
ccs 20
cts 24
cp 0.8333
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 5 1
A add() 0 6 2
A __construct() 0 4 2
A offsetUnset() 0 5 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 function assert;
10
use function count;
11
use function sprintf;
12
13
final class MetadataCollection implements ArrayAccess, Countable
14
{
15
    /** @var array<string, AnnotationMetadata> */
16
    private $metadata = [];
17
18 11
    public function __construct(AnnotationMetadata ...$metadatas)
19
    {
20 11
        foreach ($metadatas as $metadata) {
21 9
            $this->add($metadata);
22
        }
23 11
    }
24
25 11
    public function add(AnnotationMetadata ...$metadatas) : void
26
    {
27 11
        foreach ($metadatas as $metadata) {
28 11
            assert(! isset($this[$metadata->getName()]));
29
30 11
            $this->metadata[$metadata->getName()] = $metadata;
31
        }
32 11
    }
33
34
    /**
35
     * @param string $name
36
     *
37
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
38
     */
39 11
    public function offsetGet($name) : AnnotationMetadata
40
    {
41 11
        assert(isset($this[$name]), sprintf('Metadata for name %s does not exist', $name));
42
43 11
        return $this->metadata[$name];
44
    }
45
46
    /**
47
     * @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...
48
     * @param AnnotationMetadata $metadata
49
     *
50
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
51
     */
52 3
    public function offsetSet($name, $metadata) : void
53
    {
54 3
        assert($name === null);
55
56 3
        $this->add($metadata);
57 3
    }
58
59
    /**
60
     * @param string $name
61
     *
62
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
63
     */
64 11
    public function offsetExists($name) : bool
65
    {
66 11
        return isset($this->metadata[$name]);
67
    }
68
69
    /**
70
     * @param string $name
71
     *
72
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
73
     */
74
    public function offsetUnset($name) : void
75
    {
76
        assert(isset($this[$name]));
77
78
        unset($this->metadata[$name]);
79
    }
80
81 2
    public function count() : int
82
    {
83 2
        return count($this->metadata);
84
    }
85
}
86