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 ( 0f844e...db3255 )
by Jasper
02:23
created

EquatableMap::diff()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
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 ArrayIterator;
12
use Countable;
13
use IteratorAggregate;
14
use Traversable;
15
16
/**
17
 * @copyright Copyright (c) 2015 Future500 B.V.
18
 * @author    Jasper N. Brouwer <[email protected]>
19
 */
20
final class EquatableMap implements Equatable, Countable, IteratorAggregate
21
{
22
    /**
23
     * @var Equatable[]
24
     */
25
    private $items = [];
26
27
    /**
28
     * @param Equatable[] $items
29
     */
30 120 View Code Duplication
    public function __construct(array $items = [])
31
    {
32 120
        foreach ($items as $key => $value) {
33 96
            if (!$value instanceof Equatable) {
34 3
                throw InvalidArgumentException::invalidTypeInArray('items', Equatable::class, $value);
35
            }
36
37 93
            $this->items[$key] = $value;
38
        }
39 117
    }
40
41 3
    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...
42
    {
43 3
        $items = [];
44
45 3
        foreach ($this->items as $key => $item) {
46 3
            $items[$key] = clone $item;
47
        }
48
49 3
        $this->items = $items;
50 3
    }
51
52 9 View Code Duplication
    public function add(string $key, Equatable $value): EquatableMap
53
    {
54 9
        if ($this->containsKey($key)) {
55 3
            throw InRangeException::keyInRange($key);
56
        }
57
58 6
        $items = $this->items;
59
60 6
        $items[$key] = $value;
61
62 6
        return new static($items);
63
    }
64
65 6 View Code Duplication
    public function remove(Equatable $value): EquatableMap
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...
66
    {
67 6
        $key   = $this->search($value);
68 6
        $items = $this->items;
69
70 6
        unset($items[$key]);
71
72 6
        return new static($items);
73
    }
74
75 9 View Code Duplication
    public function replace(string $key, Equatable $value): EquatableMap
76
    {
77 9
        if (!$this->containsKey($key)) {
78 3
            throw OutOfRangeException::keyOutOfRange($key);
79
        }
80
81 6
        $items = $this->items;
82
83 6
        $items[$key] = $value;
84
85 6
        return new static($items);
86
    }
87
88 39
    public function get(string $key): Equatable
89
    {
90 39
        if (!$this->containsKey($key)) {
91 3
            throw OutOfRangeException::keyOutOfRange($key);
92
        }
93
94 36
        return $this->items[$key];
95
    }
96
97 27 View Code Duplication
    public function search(Equatable $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...
98
    {
99 27
        foreach ($this->items as $key => $item) {
100 24
            if ($item->equals($value)) {
101 24
                return $key;
102
            }
103
        }
104
105 15
        throw OutOfRangeException::valueOutOfRange($value);
106
    }
107
108 6 View Code Duplication
    public function searchAll(Equatable $value): array
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...
109
    {
110 6
        $foundKeys = [];
111
112 6
        foreach ($this->items as $index => $item) {
113 3
            if ($item->equals($value)) {
114 3
                $foundKeys[] = $index;
115
            }
116
        }
117
118 6
        if (!$foundKeys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $foundKeys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
119 3
            throw OutOfRangeException::valueOutOfRange($value);
120
        }
121
122 3
        return $foundKeys;
123
    }
124
125 15
    public function contains(Equatable $value): bool
126
    {
127
        try {
128 15
            $this->search($value);
129 12
            return true;
130 12
        } catch (OutOfRangeException $e) {
131 12
            return false;
132
        }
133
    }
134
135 54
    public function containsKey(string $key): bool
136
    {
137 54
        return isset($this->items[$key]);
138
    }
139
140 18 View Code Duplication
    public function equals($other): bool
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...
141
    {
142 18
        if (!$other instanceof static) {
143 3
            return false;
144
        }
145
146 15
        if ($this->count() !== $other->count()) {
147 3
            return false;
148
        }
149
150 12
        foreach ($this->items as $item) {
151 9
            if ($this->countItem($item) !== $other->countItem($item)) {
152 9
                return false;
153
            }
154
        }
155
156 9
        return true;
157
    }
158
159 54
    public function count(): int
160
    {
161 54
        return count($this->items);
162
    }
163
164 12 View Code Duplication
    public function countItem(Equatable $value): int
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...
165
    {
166 12
        $count = 0;
167
168 12
        foreach ($this->items as $item) {
169 12
            if ($item->equals($value)) {
170 12
                $count++;
171
            }
172
        }
173
174 12
        return $count;
175
    }
176
177 3 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...
178
    {
179 3
        $items = [];
180
181 3
        foreach ($this->items as $key => $item) {
182 3
            if ($other->contains($item)) {
183 3
                $items[$key] = $item;
184
            }
185
        }
186
187 3
        return new self($items);
188
    }
189
190 3 View Code Duplication
    public function intersectKeys(self $other): array
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...
191
    {
192 3
        $keys = [];
193
194 3
        foreach (array_keys($this->items) as $key) {
195 3
            if ($other->containsKey($key)) {
196 3
                $keys[] = $key;
197
            }
198
        }
199
200 3
        return $keys;
201
    }
202
203 3 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...
204
    {
205 3
        $items = [];
206
207 3
        foreach ($this->items as $key => $item) {
208 3
            if (!$other->contains($item)) {
209 3
                $items[$key] = $item;
210
            }
211
        }
212
213 3
        return new self($items);
214
    }
215
216 3 View Code Duplication
    public function diffKeys(self $other): array
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...
217
    {
218 3
        $keys = [];
219
220 3
        foreach (array_keys($this->items) as $key) {
221 3
            if (!$other->containsKey($key)) {
222 3
                $keys[] = $key;
223
            }
224
        }
225
226 3
        return $keys;
227
    }
228
229
    /**
230
     * The filter callable is given an equatable item, and should return
231
     * a boolean indicating whether the item remains or not.
232
     *
233
     * function (Equatable $item, string $key): bool {
234
     *     return true;
235
     * }
236
     */
237 6
    public function filter(callable $filter): self
238
    {
239 6
        return new self(
240 6
            array_filter($this->items, $filter, ARRAY_FILTER_USE_BOTH)
241
        );
242
    }
243
244
    /**
245
     * The mapper callable is given an equatable item, and should return
246
     * a new value to use in it's place.
247
     *
248
     * function (Equatable $item) {
249
     *     return $item;
250
     * }
251
     */
252 3
    public function map(callable $mapper): self
253
    {
254 3
        return new self(
255 3
            array_map($mapper, $this->items)
256
        );
257
    }
258
259
    /**
260
     * The reducer callable is given the carry value and an equatable item,
261
     * and should return the value it should be reduced to.
262
     *
263
     * function ($carry, Equatable $item) {
264
     *     return $carry + 1;
265
     * }
266
     */
267 3
    public function reduce(callable $reducer, $initial = null)
268
    {
269 3
        return array_reduce($this->items, $reducer, $initial);
270
    }
271
272 9
    public function getIterator(): Traversable
273
    {
274 9
        return new ArrayIterator($this->items);
275
    }
276
}
277