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

MetadataCollection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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