Completed
Push — master ( e86954...7cbcb1 )
by Ítalo
02:48
created

Dictionary::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information.
4
namespace Collections;
5
6
use Collections\Exception\KeyException;
7
use Collections\Iterator\MapIterator;
8
use Collections\Traits\GuardTrait;
9
use Collections\Traits\StrictKeyedIterableTrait;
10
use InvalidArgumentException;
11
use Symfony\Component\PropertyAccess\Exception\OutOfBoundsException;
12
use Traversable;
13
14
/**
15
 * Represents a collection of keys and values.
16
 */
17
class Dictionary extends AbstractCollectionArray implements MapInterface, \ArrayAccess
18
{
19
    use StrictKeyedIterableTrait,
20
        GuardTrait;
21
22
    public function at($k)
23
    {
24
        return $this[$k];
25
    }
26
27 21
    public function set($key, $value)
28
    {
29 21
        $this->container[$key] = $value;
30
31 21
        return $this;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 8
    public function get($index)
38
    {
39 8
        if ($this->containsKey($index) === false) {
40 1
            throw new OutOfBoundsException('No element at position ' . $index);
41
        }
42
43 7
        return $this->container[$index];
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 11
    public function add($key, $value)
50
    {
51 11
        if ($this->containsKey($key)) {
52 1
            throw new KeyException('The key ' . $key . ' already exists!');
53
        }
54 11
        $this->set($key, $value);
55
56 11
        return $this;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 4
    public function addAll($items)
63
    {
64 4
        if (!is_array($items) && !$items instanceof Traversable) {
65 1
            throw new \InvalidArgumentException('The items must be an array or Traversable');
66
        }
67
68 3
        foreach ($items as $key => $value) {
69 3
            if (is_array($value)) {
70 1
                $value = Dictionary::fromArray($value);
71
            }
72 3
            $this->add($key, $value);
73
        }
74 3
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 16
    public function containsKey($key)
80
    {
81 16
        return array_key_exists($key, $this->container);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function contains($element)
88
    {
89
        return in_array($element, $this->container, true);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 2
    public function remove($element)
96
    {
97 2
        $this->validateKeyBounds($element);
98 1
        unset($this->container[$element]);
99
100 1
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function removeKey($key)
107
    {
108 1
        return $this->remove($key);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 5
    public function offsetExists($offset)
115
    {
116 5
        return isset($this->container[$offset]) || array_key_exists($offset, $this->container);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 5
    public function offsetGet($offset)
123
    {
124 5
        return $this->get($offset);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 10
    public function offsetSet($offset, $value)
131
    {
132 10
        if (is_null($offset)) {
133
            $this->add($offset, $value);
134
        } else {
135 10
            $this->set($offset, $value);
136
        }
137 10
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    public function offsetUnset($offset)
143
    {
144 1
        $this->removeKey($offset);
145 1
    }
146
147
    /**
148
     * Gets the collection's iterator
149
     * @return MapIterator
150
     */
151 9
    public function getIterator()
152
    {
153 9
        return new MapIterator($this->container);
154
    }
155
156
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
157