Completed
Push — master ( 5305a0...ad0632 )
by Zoltán
14:04 queued 10:48
created

HashMap::checkKeyType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php namespace BuildR\Collection\Map;
2
3
use BuildR\Collection\ArrayList\ArrayList;
4
use BuildR\Collection\Collection\AbstractCollection;
5
use BuildR\Collection\Exception\MapException;
6
use BuildR\Collection\Set\Set;
7
8
class HashMap extends AbstractCollection implements MapInterface {
9
10
    /**
11
     * {@inheritDoc}
12
     */
13 52
    public function containsKey($key) {
14 52
        $this->checkKeyType($key);
15
16 52
        return isset($this->data[$key]);
17
    }
18
19
    /**
20
     * {@inheritDoc}
21
     */
22 4
    public function containsValue($value) {
23 4
        return (array_search($value, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
24
    }
25
26
    /**
27
     * {@inheritDoc}
28
     */
29 24
    public function contains($key, $value) {
30 24
        if(isset($this->data[$key]) && $this->data[$key] === $value) {
31 24
            return TRUE;
32
        }
33
34 12
        return FALSE;
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 12
    public function equals(MapInterface $map) {
41 12
        if($this->size() !== $map->size()) {
42 4
            return FALSE;
43
        }
44
45 12
        foreach($this->data as $key => $value) {
46 12
            if(!$map->contains($key, $value)) {
47 4
                return FALSE;
48
            }
49 12
        }
50
51 12
        return TRUE;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 8
    public function each(callable $callback) {
58 8
        foreach($this->data as $key => $value) {
59 8
            call_user_func_array($callback, [$key, $value]);
60 8
        }
61 8
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 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...
67 28
        $this->checkKeyType($key);
68
69 28
        if(!$this->containsKey($key)) {
70 4
            return $defaultValue;
71
        }
72
73 28
        return $this->data[$key];
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 4
    public function keySet() {
80 4
        return new Set(array_keys($this->data));
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86 4
    public function valueList() {
87 4
        return new ArrayList(array_values($this->data));
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 4
    public function merge(MapInterface $map) {
94 4
        $self = $this;
95
96 4
        $map->each(function($key, $value) use($self) {
97 4
            if(!$self->containsKey($key)) {
98 4
                $self->put($key, $value);
99 4
            }
100 4
        });
101 4
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 52 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...
107 52
        $this->checkKeyType($key);
108 52
        $return = NULL;
109
110 52
        if($this->containsKey($key)) {
111 4
            $return = $this->get($key);
112 4
        }
113
114 52
        $this->data[$key] = $value;
115
116 52
        return $return;
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122 44
    public function putAll($map) {
123 44
        if($map instanceof MapInterface) {
124 4
            $map = $map->toArray();
125 4
        }
126
127 44
        foreach($map as $key => $value) {
128 44
            $this->put($key, $value);
129 44
        }
130 44
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 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...
136 4
        $this->checkKeyType($key);
137
138 4
        if($this->containsKey($key)) {
139 4
            return $this->get($key);
140
        }
141
142 4
        $this->put($key, $value);
143
144 4
        return NULL;
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 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...
151 4
        $this->checkKeyType($key);
152
153 4
        if($this->containsKey($key)) {
154 4
            $previousElement = $this->get($key);
155 4
            unset($this->data[$key]);
156
157 4
            return $previousElement;
158
        }
159
160 4
        return NULL;
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166 4
    public function removeIf($key, $value) {
167 4
        $this->checkKeyType($key);
168
169 4
        if($this->contains($key, $value)) {
170 4
            $previousElement = $this->get($key);
171 4
            unset($this->data[$key]);
172
173 4
            return $previousElement;
174
        }
175
176 4
        return NULL;
177
    }
178
179
    /**
180
     * {@inheritDoc}
181
     */
182 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...
183 4
        $this->checkKeyType($key);
184
185 4
        if($this->containsKey($key)) {
186 4
            $previousValue = $this->get($key);
187 4
            $this->data[$key] = $value;
188
189 4
            return $previousValue;
190
        }
191
192 4
        return NULL;
193
    }
194
195
    /**
196
     * {@inheritDoc}
197
     */
198 4
    public function replaceIf($key, $oldValue, $newValue) {
199 4
        $this->checkKeyType($key);
200
201 4
        if($this->contains($key, $oldValue)) {
202 4
            $previousValue = $this->get($key);
203 4
            $this->data[$key] = $newValue;
204
205 4
            return $previousValue;
206
        }
207
208 4
        return NULL;
209
    }
210
211
    /**
212
     * Validate the given key. This map only allows scalar types
213
     * as items key. If the validation is fails, throws a MapException.
214
     *
215
     * @param mixed $key
216
     *
217
     * @return bool
218
     *
219
     * @throws \BuildR\Collection\Exception\MapException
220
     */
221 56
    protected function checkKeyType($key) {
222 56
        if(is_scalar($key)) {
223 52
            return TRUE;
224
        }
225
226 4
        throw MapException::notValidKey($key);
227
    }
228
229
}
230