Passed
Pull Request — master (#247)
by Michael
02:00
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
 * In-memory metadata collection.
16
 *
17
 * @internal
18
 */
19
final class TransientMetadataCollection implements MetadataCollection
20
{
21
    /** @var array<string, AnnotationMetadata> */
22
    private $metadata = [];
23
24 295
    public function __construct(AnnotationMetadata ...$metadatas)
25
    {
26 295
        $this->add(...$metadatas);
27 294
    }
28
29
    /**
30
     * @throws MetadataAlreadyExists
31
     */
32 295
    public function add(AnnotationMetadata ...$metadatas) : void
33
    {
34 295
        foreach ($metadatas as $metadata) {
35 294
            if (isset($this->metadata[$metadata->getName()])) {
36 1
                throw MetadataAlreadyExists::new($metadata);
37
            }
38
39 294
            $this->metadata[$metadata->getName()] = $metadata;
40
        }
41 294
    }
42
43 1
    public function include(MetadataCollection $other) : void
44
    {
45 1
        $this->add(...$other);
46 1
    }
47
48
    /**
49
     * @throws MetadataDoesNotExist
50
     */
51 261
    public function get(string $name) : AnnotationMetadata
52
    {
53 261
        if (! isset($this->metadata[$name])) {
54 1
            throw MetadataDoesNotExist::new($name);
55
        }
56
57 260
        return $this->metadata[$name];
58
    }
59
60 264
    public function has(string $name) : bool
61
    {
62 264
        return array_key_exists($name, $this->metadata);
63
    }
64
65
    /**
66
     * @return Traversable<AnnotationMetadata>
67
     */
68 2
    public function getIterator() : Traversable
69
    {
70 2
        yield from array_values($this->metadata);
71 2
    }
72
73 3
    public function count() : int
74
    {
75 3
        return count($this->metadata);
76
    }
77
}
78