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

EquatableVector::intersect()   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 EquatableVector implements Equatable, Countable, IteratorAggregate
21
{
22
    /**
23
     * @var Equatable[]
24
     */
25
    private $items = [];
26
27
    /**
28
     * @param Equatable[] $items
29
     */
30 126 View Code Duplication
    public function __construct(array $items = [])
31
    {
32 126
        foreach ($items as $item) {
33 102
            if (!$item instanceof Equatable) {
34 3
                throw InvalidArgumentException::invalidTypeInArray('items', Equatable::class, $item);
35
            }
36
37 99
            $this->items[] = $item;
38
        }
39 123
    }
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 $item) {
46 3
            $items[] = clone $item;
47
        }
48
49 3
        $this->items = $items;
50 3
    }
51
52 6
    public function add(Equatable $value): EquatableVector
53
    {
54 6
        $items = $this->items;
55
56 6
        $items[] = $value;
57
58 6
        return new static($items);
59
    }
60
61 12 View Code Duplication
    public function remove(Equatable $value): EquatableVector
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...
62
    {
63 12
        $index = $this->search($value);
64 12
        $items = $this->items;
65
66 12
        unset($items[$index]);
67
68 12
        return new static($items);
69
    }
70
71 9
    public function replace(int $index, Equatable $value): EquatableVector
72
    {
73 9
        if (!$this->containsIndex($index)) {
74 3
            throw OutOfRangeException::indexOutOfRange($index);
75
        }
76
77 6
        $items = $this->items;
78
79 6
        $items[$index] = $value;
80
81 6
        return new static($items);
82
    }
83
84 42
    public function get(int $index): Equatable
85
    {
86 42
        if (!$this->containsIndex($index)) {
87 3
            throw OutOfRangeException::indexOutOfRange($index);
88
        }
89
90 39
        return $this->items[$index];
91
    }
92
93 39 View Code Duplication
    public function search(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...
94
    {
95 39
        foreach ($this->items as $index => $item) {
96 36
            if ($item->equals($value)) {
97 36
                return $index;
98
            }
99
        }
100
101 18
        throw OutOfRangeException::valueOutOfRange($value);
102
    }
103
104 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...
105
    {
106 6
        $foundIndexes = [];
107
108 6
        foreach ($this->items as $index => $item) {
109 3
            if ($item->equals($value)) {
110 3
                $foundIndexes[] = $index;
111
            }
112
        }
113
114 6
        if (!$foundIndexes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $foundIndexes 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...
115 3
            throw OutOfRangeException::valueOutOfRange($value);
116
        }
117
118 3
        return $foundIndexes;
119
    }
120
121 18
    public function contains(Equatable $value): bool
122
    {
123
        try {
124 18
            $this->search($value);
125 12
            return true;
126 15
        } catch (OutOfRangeException $e) {
127 15
            return false;
128
        }
129
    }
130
131 48
    public function containsIndex(int $index): bool
132
    {
133 48
        return isset($this->items[$index]);
134
    }
135
136 21 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...
137
    {
138 21
        if (!$other instanceof static) {
139 3
            return false;
140
        }
141
142 18
        if ($this->count() !== $other->count()) {
143 3
            return false;
144
        }
145
146 15
        foreach ($this->items as $item) {
147 12
            if ($this->countItem($item) !== $other->countItem($item)) {
148 12
                return false;
149
            }
150
        }
151
152 9
        return true;
153
    }
154
155 57
    public function count(): int
156
    {
157 57
        return count($this->items);
158
    }
159
160 15 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...
161
    {
162 15
        $count = 0;
163
164 15
        foreach ($this->items as $item) {
165 15
            if ($item->equals($value)) {
166 15
                $count++;
167
            }
168
        }
169
170 15
        return $count;
171
    }
172
173 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...
174
    {
175 3
        $items = [];
176
177 3
        foreach ($this->items as $item) {
178 3
            if ($other->contains($item)) {
179 3
                $items[] = $item;
180
            }
181
        }
182
183 3
        return new self($items);
184
    }
185
186 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...
187
    {
188 3
        $items = [];
189
190 3
        foreach ($this->items as $item) {
191 3
            if (!$other->contains($item)) {
192 3
                $items[] = $item;
193
            }
194
        }
195
196 3
        return new self($items);
197
    }
198
199
    /**
200
     * The filter callable is given an equatable item, and should return
201
     * a boolean indicating whether the item remains or not.
202
     *
203
     * function (Equatable $item): bool {
204
     *     return true;
205
     * }
206
     */
207 3
    public function filter(callable $filter): self
208
    {
209 3
        return new self(
210 3
            array_filter($this->items, $filter)
211
        );
212
    }
213
214
    /**
215
     * The mapper callable is given an equatable item, and should return
216
     * a new value to use in it's place.
217
     *
218
     * function (Equatable $item) {
219
     *     return $item;
220
     * }
221
     */
222 3
    public function map(callable $mapper): self
223
    {
224 3
        return new self(
225 3
            array_map($mapper, $this->items)
226
        );
227
    }
228
229
    /**
230
     * The reducer callable is given the carry value and an equatable item,
231
     * and should return the value it should be reduced to.
232
     *
233
     * function ($carry, Equatable $item) {
234
     *     return $carry + 1;
235
     * }
236
     */
237 3
    public function reduce(callable $reducer, $initial = null)
238
    {
239 3
        return array_reduce($this->items, $reducer, $initial);
240
    }
241
242 12
    public function getIterator(): Traversable
243
    {
244 12
        return new ArrayIterator($this->items);
245
    }
246
}
247