Failed Conditions
Pull Request — new-parser-ast-metadata (#2)
by
unknown
02:16
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 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