Collection   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 65
ccs 19
cts 20
cp 0.95
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetGet() 0 7 2
A count() 0 3 1
A offsetSet() 0 4 1
A offsetUnset() 0 3 1
A getIterator() 0 3 1
A add() 0 3 1
A __construct() 0 4 2
A offsetExists() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Knp\DictionaryBundle\Dictionary;
6
7
use ArrayAccess;
8
use IteratorAggregate;
9
use Knp\DictionaryBundle\Dictionary;
10
use Knp\DictionaryBundle\Exception\DictionaryNotFoundException;
11
12
/**
13
 * @implements ArrayAccess<string, Dictionary<mixed>>
14
 * @implements IteratorAggregate<string, Dictionary<mixed>>
15
 */
16
final class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
17
{
18
    /**
19
     * @var array<string, Dictionary<mixed>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, Dictionary<mixed>> at position 4 could not be parsed: Expected '>' at position 4, but found 'Dictionary'.
Loading history...
20
     */
21
    private array $dictionaries = [];
22
23
    /**
24
     * @param Dictionary<mixed> ...$dictionaries
25
     */
26
    public function __construct(Dictionary ...$dictionaries)
27
    {
28
        foreach ($dictionaries as $dictionary) {
29 32
            $this->add($dictionary);
30
        }
31 32
    }
32 21
33
    /**
34 32
     * @param Dictionary<mixed> $dictionary
35
     */
36
    public function add(Dictionary $dictionary): void
37
    {
38
        $this->dictionaries[$dictionary->getName()] = $dictionary;
39 23
    }
40
41 23
    public function offsetExists($offset): bool
42 23
    {
43
        return \array_key_exists($offset, $this->dictionaries);
44 11
    }
45
46 11
    /**
47
     * @return Dictionary<mixed>
48
     *                           {@inheritdoc}
49
     *
50
     * @throws DictionaryNotFoundException
51
     */
52
    public function offsetGet($offset): Dictionary
53
    {
54
        if (!$this->offsetExists($offset)) {
55
            throw new DictionaryNotFoundException($offset, array_keys($this->dictionaries));
56 10
        }
57
58 10
        return $this->dictionaries[$offset];
59
    }
60
61
    public function offsetSet($offset, $value): void
62 10
    {
63
        throw new \RuntimeException(
64
            'To add a Dictionary to the Collection, use '.self::class.'::add(Dictionary $dictionary).'
65 1
        );
66
    }
67 1
68 1
    public function offsetUnset($offset): void
69
    {
70
        throw new \RuntimeException('It is not possible to remove a dictionary from the collection.');
71
    }
72 1
73
    public function getIterator(): \Traversable
74 1
    {
75
        return new \ArrayIterator($this->dictionaries);
76
    }
77 2
78
    public function count(): int
79 2
    {
80
        return \count($this->dictionaries);
81
    }
82
}
83