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 ( 1b6119...8c0dd9 )
by Jasper
02:10
created

ImmutableEquatableVector::guardItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * @license https://github.com/f500/equatable/blob/master/LICENSE MIT
5
 */
6
7
namespace F500\Equatable;
8
9
use ArrayIterator;
10
11
/**
12
 * @copyright Copyright (c) 2015 Future500 B.V.
13
 * @author    Jasper N. Brouwer <[email protected]>
14
 */
15
class ImmutableEquatableVector implements EquatableVector
16
{
17
    /**
18
     * @var Equatable[]
19
     */
20
    private $items = [];
21
22
    /**
23
     * @param Equatable[] $items
24
     */
25 111 View Code Duplication
    public function __construct(array $items = [])
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...
26
    {
27 111
        foreach ($items as $item) {
28 81
            if (!$item instanceof Equatable) {
29 3
                throw InvalidArgumentException::invalidTypeInArray('items', Equatable::class, $item);
30
            }
31
32 78
            $this->items[] = $item;
33 72
        }
34 108
    }
35
36 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...
37
    {
38 3
        $items = [];
39
40 3
        foreach ($this->items as $item) {
41 3
            $items[] = clone $item;
42 2
        }
43
44 3
        $this->items = $items;
45 3
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 6
    public function add(Equatable $value)
51
    {
52 6
        $items = $this->items;
53
54 6
        $items[] = $value;
55
56 6
        return new static($items);
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 12 View Code Duplication
    public function remove(Equatable $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...
63
    {
64 12
        $key   = $this->search($value);
65 12
        $items = $this->items;
66
67 12
        unset($items[$key]);
68
69 12
        return new static($items);
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 12
    public function replace($key, Equatable $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...
76
    {
77 12
        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
    /**
89
     * @inheritdoc
90
     */
91 33
    public function get($key)
92
    {
93 33
        if (!$this->containsKey($key)) {
94 3
            throw OutOfRangeException::keyOutOfRange($key);
95
        }
96
97 27
        return $this->items[$key];
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 24 View Code Duplication
    public function search(Equatable $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...
104
    {
105 24
        foreach ($this->items as $index => $item) {
106 21
            if ($item->equals($value)) {
107 21
                return $index;
108
            }
109 12
        }
110
111 6
        throw OutOfRangeException::valueOutOfRange($value);
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117 6
    public function searchAll(Equatable $value)
118
    {
119 6
        $foundKeys = [];
120
121 6
        foreach ($this->items as $index => $item) {
122 3
            if ($item->equals($value)) {
123 3
                $foundKeys[] = $index;
124 2
            }
125 4
        }
126
127 6
        if (!$foundKeys) {
128 3
            throw OutOfRangeException::valueOutOfRange($value);
129
        }
130
131 3
        return $foundKeys;
132
    }
133
134
    /**
135
     * @inheritdoc
136
     */
137 3
    public function contains(Equatable $value)
138
    {
139
        try {
140 3
            $this->search($value);
141 3
            return true;
142 3
        } catch (OutOfRangeException $e) {
143 3
            return false;
144
        }
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150 45
    public function containsKey($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
    {
152 45
        if (!is_int($key)) {
153 9
            throw InvalidArgumentException::invalidType('key', 'integer', $key);
154
        }
155
156 36
        return isset($this->items[$key]);
157
    }
158
159
    /**
160
     * @inheritdoc
161
     */
162 21 View Code Duplication
    public function equals($other)
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...
163
    {
164 21
        if (!$other instanceof static) {
165 3
            return false;
166
        }
167
168 18
        if ($this->count() !== $other->count()) {
169 3
            return false;
170
        }
171
172 15
        foreach ($this->items as $item) {
173 12
            if ($this->countItem($item) !== $other->countItem($item)) {
174 8
                return false;
175
            }
176 8
        }
177
178 9
        return true;
179
    }
180
181
    /**
182
     * @inheritdoc
183
     */
184 12
    public function getIterator()
185
    {
186 12
        return new ArrayIterator($this->items);
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192 45
    public function count()
193
    {
194 45
        return count($this->items);
195
    }
196
197
    /**
198
     * @inheritdoc
199
     */
200 15 View Code Duplication
    public function countItem(Equatable $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...
201
    {
202 15
        $count = 0;
203
204 15
        foreach ($this->items as $item) {
205 15
            if ($item->equals($value)) {
206 15
                $count++;
207 10
            }
208 10
        }
209
210 15
        return $count;
211
    }
212
}
213