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