HashMap::remove()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php namespace BuildR\Collection\Map;
2
3
use BuildR\Collection\ArrayList\ArrayList;
4
use BuildR\Collection\Collection\AbstractTypedCollection;
5
use BuildR\Collection\Collection\FilterableCollectionTrait;
6
use BuildR\Collection\Exception\MapException;
7
use BuildR\Collection\Set\HashSet;
8
9
/**
10
 * MapInterface implementation
11
 *
12
 * BuildR PHP Framework
13
 *
14
 * @author Zoltán Borsos <[email protected]>
15
 * @package Collection
16
 * @subpackage Map
17
 *
18
 * @copyright    Copyright 2015, Zoltán Borsos.
19
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
20
 * @link         https://github.com/Zolli/BuildR
21
 */
22
class HashMap extends AbstractTypedCollection implements MapInterface {
23
24
    use FilterableCollectionTrait;
25
26
    /**
27
     * HashMap constructor.
28
     *
29
     * @param array|null $content
30
     */
31 58
    public function __construct($content = NULL) {
32 58
        if($content !== NULL && is_array($content)) {
33 6
            $this->putAll($content);
34 6
        }
35 58
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 54
    public function containsKey($key) {
41 54
        $this->checkKeyType($key);
42
43 54
        return isset($this->data[$key]);
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 4
    public function containsValue($value) {
50 4
        return (array_search($value, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 24
    public function contains($key, $value) {
57 24
        if(isset($this->data[$key]) && $this->data[$key] === $value) {
58 24
            return TRUE;
59
        }
60
61 12
        return FALSE;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 12
    public function equals(MapInterface $map) {
68 12
        if($this->size() !== $map->size()) {
69 4
            return FALSE;
70
        }
71
72 12
        foreach($this->data as $key => $value) {
73 12
            if(!$map->contains($key, $value)) {
74 4
                return FALSE;
75
            }
76 12
        }
77
78 12
        return TRUE;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 8
    public function each(callable $callback) {
85 8
        foreach($this->data as $key => $value) {
86 8
            call_user_func_array($callback, [$key, $value]);
87 8
        }
88 8
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 28 View Code Duplication
    public function get($key, $defaultValue = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94 28
        $this->checkKeyType($key);
95
96 28
        if(!$this->containsKey($key)) {
97 4
            return $defaultValue;
98
        }
99
100 28
        return $this->data[$key];
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 4
    public function keySet() {
107 4
        return new HashSet(array_keys($this->data));
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 4
    public function valueList() {
114 4
        return new ArrayList(array_values($this->data));
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 4
    public function merge(MapInterface $map) {
121 4
        $self = $this;
122
123 4
        $map->each(function($key, $value) use($self) {
124 4
            if(!$self->containsKey($key)) {
125 4
                $self->put($key, $value);
126 4
            }
127 4
        });
128 4
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 54 View Code Duplication
    public function put($key, $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134 54
        $this->doTypeCheck($value);
135 54
        $this->checkKeyType($key);
136 54
        $return = NULL;
137
138 54
        if($this->containsKey($key)) {
139 4
            $return = $this->get($key);
140 4
        }
141
142 54
        $this->data[$key] = $value;
143
144 54
        return $return;
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 46
    public function putAll($map) {
151 46
        if($map instanceof MapInterface) {
152 4
            $map = $map->toArray();
153 4
        }
154
155 46
        foreach($map as $key => $value) {
156 46
            $this->put($key, $value);
157 46
        }
158 46
    }
159
160
    /**
161
     * {@inheritDoc}
162
     */
163 4 View Code Duplication
    public function putIfAbsent($key, $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164 4
        $this->checkKeyType($key);
165
166 4
        if($this->containsKey($key)) {
167 4
            return $this->get($key);
168
        }
169
170 4
        $this->put($key, $value);
171
172 4
        return NULL;
173
    }
174
175
    /**
176
     * {@inheritDoc}
177
     */
178 4 View Code Duplication
    public function remove($key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179 4
        $this->checkKeyType($key);
180
181 4
        if($this->containsKey($key)) {
182 4
            $previousElement = $this->get($key);
183 4
            unset($this->data[$key]);
184
185 4
            return $previousElement;
186
        }
187
188 4
        return NULL;
189
    }
190
191
    /**
192
     * {@inheritDoc}
193
     */
194 4
    public function removeIf($key, $value) {
195 4
        $this->checkKeyType($key);
196
197 4
        if($this->contains($key, $value)) {
198 4
            $previousElement = $this->get($key);
199 4
            unset($this->data[$key]);
200
201 4
            return $previousElement;
202
        }
203
204 4
        return NULL;
205
    }
206
207
    /**
208
     * {@inheritDoc}
209
     */
210 4 View Code Duplication
    public function replace($key, $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211 4
        $this->doTypeCheck($value);
212 4
        $this->checkKeyType($key);
213
214 4
        if($this->containsKey($key)) {
215 4
            $previousValue = $this->get($key);
216 4
            $this->data[$key] = $value;
217
218 4
            return $previousValue;
219
        }
220
221 4
        return NULL;
222
    }
223
224
    /**
225
     * {@inheritDoc}
226
     */
227 4
    public function replaceIf($key, $oldValue, $newValue) {
228 4
        $this->checkKeyType($key);
229
230 4
        if($this->contains($key, $oldValue)) {
231 4
            $previousValue = $this->get($key);
232 4
            $this->data[$key] = $newValue;
233
234 4
            return $previousValue;
235
        }
236
237 4
        return NULL;
238
    }
239
240
    /**
241
     * Validate the given key. This map only allows scalar types
242
     * as items key. If the validation is fails, throws a MapException.
243
     *
244
     * @param mixed $key
245
     *
246
     * @return bool
247
     *
248
     * @throws \BuildR\Collection\Exception\MapException
249
     */
250 58
    protected function checkKeyType($key) {
251 58
        if(is_scalar($key)) {
252 54
            return TRUE;
253
        }
254
255 4
        throw MapException::notValidKey($key);
256
    }
257
258
}
259