GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( db3255...800171 )
by Jasper
14:47
created

Map   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 268
Duplicated Lines 48.13 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 49
lcom 1
cbo 2
dl 129
loc 268
rs 8.5454
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __clone() 14 14 3
A get() 0 8 2
A containsKey() 0 4 1
A search() 10 10 3
B equals() 0 22 6
A values() 0 6 1
A keys() 0 6 1
A add() 0 12 2
A replace() 9 9 1
A replaceAll() 0 15 3
A replaceKey() 12 12 2
A remove() 9 9 1
A removeAll() 15 15 3
A removeKey() 12 12 2
A intersect() 12 12 3
A intersectKeys() 12 12 3
A diff() 12 12 3
A diffKeys() 12 12 3
A filter() 0 6 1
A map() 0 6 1
A guardAgainstInvalidKey() 0 6 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Map often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Map, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @license https://github.com/f500/equatable/blob/master/LICENSE MIT
5
 */
6
7
declare(strict_types=1);
8
9
namespace F500\Equatable;
10
11
use F500\Equatable\Exceptions\InRangeException;
12
use F500\Equatable\Exceptions\InvalidArgumentException;
13
use F500\Equatable\Exceptions\OutOfRangeException;
14
15
/**
16
 * @copyright Copyright (c) 2015 Future500 B.V.
17
 * @author    Jasper N. Brouwer <[email protected]>
18
 */
19
final class Map extends Collection
20
{
21
    public function __construct(array $values = [])
22
    {
23
        foreach ($values as $key => $value) {
24
            $this->guardAgainstInvalidKey($key);
25
            $this->guardAgainstInvalidValue($value);
26
            $this->items[$key] = $value;
27
        }
28
    }
29
30 View Code Duplication
    public function __clone()
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...
31
    {
32
        $items = [];
33
34
        foreach ($this->items as $key => $item) {
35
            if (is_object($item)) {
36
                $items[$key] = clone $item;
37
            } else {
38
                $items[$key] = $item;
39
            }
40
        }
41
42
        $this->items = $items;
43
    }
44
45
    public function get(string $key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
46
    {
47
        if (!$this->containsKey($key)) {
48
            throw OutOfRangeException::keyOutOfRange($key);
49
        }
50
51
        return $this->items[$key];
52
    }
53
54
    public function containsKey(string $key): bool
55
    {
56
        return isset($this->items[$key]);
57
    }
58
59 View Code Duplication
    public function search($value): string
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...
60
    {
61
        foreach ($this->items as $key => $item) {
62
            if ($this->theseAreEqual($item, $value)) {
63
                return $key;
64
            }
65
        }
66
67
        throw OutOfRangeException::valueOutOfRange($value);
68
    }
69
70
    public function equals($other): bool
71
    {
72
        if (!$other instanceof static) {
73
            return false;
74
        }
75
76
        if ($this->count() !== $other->count()) {
77
            return false;
78
        }
79
80
        foreach ($this->items as $key => $item) {
81
            if (!$other->containsKey($key)) {
82
                return false;
83
            }
84
85
            if (!$this->theseAreEqual($item, $other->get($key))) {
86
                return false;
87
            }
88
        }
89
90
        return true;
91
    }
92
93
    public function values(): Vector
94
    {
95
        $items = array_values($this->items);
96
97
        return new Vector($items);
98
    }
99
100
    public function keys(): Vector
101
    {
102
        $items = array_keys($this->items);
103
104
        return new Vector($items);
105
    }
106
107
    public function add(string $key, $value): self
108
    {
109
        if ($this->containsKey($key)) {
110
            throw InRangeException::keyInRange($key);
111
        }
112
113
        $items = $this->items;
114
115
        $items[$key] = $value;
116
117
        return new self($items);
118
    }
119
120 View Code Duplication
    public function replace($searchValue, $replacementValue): self
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...
121
    {
122
        $items = $this->items;
123
        $key   = $this->search($searchValue);
124
125
        $items[$key] = $replacementValue;
126
127
        return new self($items);
128
    }
129
130
    public function replaceAll($searchValue, $replacementValue): self
131
    {
132
        $items = $this->items;
133
        $keys  = $this->searchAll($searchValue);
134
135
        if ($keys->isEmpty()) {
136
            return $this;
137
        }
138
139
        foreach ($keys as $key) {
140
            $items[$key] = $replacementValue;
141
        }
142
143
        return new self($items);
144
    }
145
146 View Code Duplication
    public function replaceKey(string $key, $replacementValue): self
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...
147
    {
148
        if (!$this->containsKey($key)) {
149
            throw OutOfRangeException::keyOutOfRange($key);
150
        }
151
152
        $items = $this->items;
153
154
        $items[$key] = $replacementValue;
155
156
        return new self($items);
157
    }
158
159 View Code Duplication
    public function remove($value): self
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...
160
    {
161
        $items = $this->items;
162
        $key   = $this->search($value);
163
164
        unset($items[$key]);
165
166
        return new self($items);
167
    }
168
169 View Code Duplication
    public function removeAll($value): self
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...
170
    {
171
        $items = $this->items;
172
        $keys  = $this->searchAll($value);
173
174
        if ($keys->isEmpty()) {
175
            return $this;
176
        }
177
178
        foreach ($keys as $key) {
179
            unset($items[$key]);
180
        }
181
182
        return new self($items);
183
    }
184
185 View Code Duplication
    public function removeKey(string $key): self
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...
186
    {
187
        if (!$this->containsKey($key)) {
188
            throw OutOfRangeException::keyOutOfRange($key);
189
        }
190
191
        $items = $this->items;
192
193
        unset($items[$key]);
194
195
        return new self($items);
196
    }
197
198 View Code Duplication
    public function intersect(self $other): self
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...
199
    {
200
        $items = [];
201
202
        foreach ($this->items as $key => $item) {
203
            if ($other->contains($item)) {
204
                $items[$key] = $item;
205
            }
206
        }
207
208
        return new self($items);
209
    }
210
211 View Code Duplication
    public function intersectKeys(self $other): self
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...
212
    {
213
        $items = [];
214
215
        foreach ($this->items as $key => $item) {
216
            if ($other->containsKey($key)) {
217
                $items[$key] = $item;
218
            }
219
        }
220
221
        return new self($items);
222
    }
223
224 View Code Duplication
    public function diff(self $other): self
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...
225
    {
226
        $items = [];
227
228
        foreach ($this->items as $key => $item) {
229
            if (!$other->contains($item)) {
230
                $items[$key] = $item;
231
            }
232
        }
233
234
        return new self($items);
235
    }
236
237 View Code Duplication
    public function diffKeys(self $other): self
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...
238
    {
239
        $items = [];
240
241
        foreach ($this->items as $key => $item) {
242
            if (!$other->containsKey($key)) {
243
                $items[$key] = $item;
244
            }
245
        }
246
247
        return new self($items);
248
    }
249
250
    /**
251
     * The filter callable is given an item and it's key, and should
252
     * return a boolean indicating whether the item remains or not.
253
     *
254
     * function ($item, $key): bool {
255
     *     return true;
256
     * }
257
     */
258
    public function filter(callable $filter): self
259
    {
260
        $items = array_filter($this->items, $filter, ARRAY_FILTER_USE_BOTH);
261
262
        return new self($items);
263
    }
264
265
    /**
266
     * The mapper callable is given an item, and should return
267
     * a new value to use in it's place.
268
     *
269
     * function ($item) {
270
     *     return $item;
271
     * }
272
     */
273
    public function map(callable $mapper): self
274
    {
275
        $items = array_map($mapper, $this->items);
276
277
        return new self($items);
278
    }
279
280
    private function guardAgainstInvalidKey($key): void
281
    {
282
        if (!is_string($key)) {
283
            throw InvalidArgumentException::invalidKeyTypeInArray('values', 'string', $key);
284
        }
285
    }
286
}
287