Passed
Pull Request — master (#247)
by Michael
02:14
created

TransientMetadataCollection::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\Metadata;
6
7
use Doctrine\Annotations\Metadata\Exception\MetadataAlreadyExists;
8
use Doctrine\Annotations\Metadata\Exception\MetadataDoesNotExist;
9
use Traversable;
10
use function array_key_exists;
11
use function array_values;
12
use function count;
13
14
/**
15
 * @internal
16
 */
17
final class TransientMetadataCollection implements MetadataCollection
18
{
19
    /** @var array<string, AnnotationMetadata> */
20
    private $metadata = [];
21
22 295
    public function __construct(AnnotationMetadata ...$metadatas)
23
    {
24 295
        $this->add(...$metadatas);
25 294
    }
26
27
    /**
28
     * @throws MetadataAlreadyExists
29
     */
30 295
    public function add(AnnotationMetadata ...$metadatas) : void
31
    {
32 295
        foreach ($metadatas as $metadata) {
33 294
            if (isset($this->metadata[$metadata->getName()])) {
34 1
                throw MetadataAlreadyExists::new($metadata);
35
            }
36
37 294
            $this->metadata[$metadata->getName()] = $metadata;
38
        }
39 294
    }
40
41 1
    public function include(MetadataCollection $other) : void
42
    {
43 1
        $this->add(...$other);
44 1
    }
45
46
    /**
47
     * @throws MetadataDoesNotExist
48
     */
49 261
    public function get(string $name) : AnnotationMetadata
50
    {
51 261
        if (! isset($this->metadata[$name])) {
52 1
            throw MetadataDoesNotExist::new($name);
53
        }
54
55 260
        return $this->metadata[$name];
56
    }
57
58 264
    public function has(string $name) : bool
59
    {
60 264
        return array_key_exists($name, $this->metadata);
61
    }
62
63
    /**
64
     * @return Traversable<AnnotationMetadata>
65
     */
66 2
    public function getIterator() : Traversable
67
    {
68 2
        yield from array_values($this->metadata);
69 2
    }
70
71 3
    public function count() : int
72
    {
73 3
        return count($this->metadata);
74
    }
75
}
76