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.

ArrayCollection   B
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 42
lcom 1
cbo 0
dl 0
loc 327
rs 8.295
c 0
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 4 1
A first() 0 4 1
A last() 0 4 1
A key() 0 4 1
A next() 0 4 1
A current() 0 4 1
A remove() 0 11 3
A removeElement() 0 12 2
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 10 2
A offsetUnset() 0 4 1
A containsKey() 0 4 2
A contains() 0 4 1
A exists() 0 10 3
A forAll() 0 4 1
A indexOf() 0 4 1
A get() 0 4 2
A getKeys() 0 4 1
A getValues() 0 4 1
A count() 0 4 1
A set() 0 4 1
A add() 0 6 1
A isEmpty() 0 4 1
A getIterator() 0 4 1
A map() 0 4 1
A filter() 0 4 1
A partition() 0 14 3
A __toString() 0 4 1
A clear() 0 4 1
A slice() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like ArrayCollection 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 ArrayCollection, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace DF\PHPCoverFish\Common;
4
5
use ArrayIterator;
6
use Closure;
7
8
/**
9
 * ArrayCollection helper, based on the work on \Doctrine\Common\ArrayCollection of Benjamin Eberlei <[email protected]>,
10
 * Jonathan Wage <[email protected]> and Roman Borschel <[email protected]>. minor changes and improvements by Patrick Paechnatz
11
 *
12
 * core:
13
 *
14
 * @since  2.0
15
 * @author Guilherme Blanco <[email protected]>
16
 * @author Jonathan Wage <[email protected]>
17
 * @author Roman Borschel <[email protected]>
18
 *
19
 * changes/improvements:
20
 *
21
 * @author Patrick Paechnatz <[email protected]>
22
 */
23
class ArrayCollection implements Collection
24
{
25
    /**
26
     * An array containing the entries of this collection.
27
     *
28
     * @var array
29
     */
30
    private $elements;
31
32
    /**
33
     * Initializes a new ArrayCollection.
34
     *
35
     * @param array $elements
36
     *
37
     * @codeCoverageIgnore
38
     */
39
    public function __construct(array $elements = array())
40
    {
41
        $this->elements = $elements;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function toArray()
48
    {
49
        return $this->elements;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function first()
56
    {
57
        return reset($this->elements);
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function last()
64
    {
65
        return end($this->elements);
66
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function key()
72
    {
73
        return key($this->elements);
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function next()
80
    {
81
        return next($this->elements);
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function current()
88
    {
89
        return current($this->elements);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function remove($key)
96
    {
97
        if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) {
98
            return null;
99
        }
100
101
        $removed = $this->elements[$key];
102
        unset($this->elements[$key]);
103
104
        return $removed;
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    public function removeElement($element)
111
    {
112
        $key = array_search($element, $this->elements, true);
113
114
        if ($key === false) {
115
            return false;
116
        }
117
118
        unset($this->elements[$key]);
119
120
        return true;
121
    }
122
123
    /**
124
     * Required by interface ArrayAccess.
125
     *
126
     * {@inheritDoc}
127
     */
128
    public function offsetExists($offset)
129
    {
130
        return $this->containsKey($offset);
131
    }
132
133
    /**
134
     * Required by interface ArrayAccess.
135
     *
136
     * {@inheritDoc}
137
     */
138
    public function offsetGet($offset)
139
    {
140
        return $this->get($offset);
141
    }
142
143
    /**
144
     * Required by interface ArrayAccess.
145
     *
146
     * {@inheritDoc}
147
     */
148
    public function offsetSet($offset, $value)
149
    {
150
        if (!isset($offset)) {
151
            return $this->add($value);
152
        }
153
154
        $this->set($offset, $value);
155
156
        return null;
157
    }
158
159
    /**
160
     * Required by interface ArrayAccess.
161
     *
162
     * {@inheritDoc}
163
     */
164
    public function offsetUnset($offset)
165
    {
166
        return $this->remove($offset);
167
    }
168
169
    /**
170
     * {@inheritDoc}
171
     */
172
    public function containsKey($key)
173
    {
174
        return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180
    public function contains($element)
181
    {
182
        return in_array($element, $this->elements, true);
183
    }
184
185
    /**
186
     * Tests for the existence of an element that satisfies the given predicate.
187
     *
188
     * @param Closure $p The predicate.
189
     * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise.
190
     */
191
    public function exists(Closure $p)
192
    {
193
        foreach ($this->elements as $key => $element) {
194
            if ($p($key, $element)) {
195
                return true;
196
            }
197
        }
198
199
        return false;
200
    }
201
202
    /**
203
     * Applies the given predicate p to all elements of this collection,
204
     * returning true, if the predicate yields true for all elements.
205
     *
206
     * @param Closure $p The predicate.
207
     * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
208
     */
209
    public function forAll(Closure $p)
210
    {
211
        return $this->exists($p);
212
    }
213
214
    /**
215
     * {@inheritDoc}
216
     */
217
    public function indexOf($element)
218
    {
219
        return array_search($element, $this->elements, true);
220
    }
221
222
    /**
223
     * {@inheritDoc}
224
     */
225
    public function get($key)
226
    {
227
        return isset($this->elements[$key]) ? $this->elements[$key] : null;
228
    }
229
230
    /**
231
     * {@inheritDoc}
232
     */
233
    public function getKeys()
234
    {
235
        return array_keys($this->elements);
236
    }
237
238
    /**
239
     * {@inheritDoc}
240
     */
241
    public function getValues()
242
    {
243
        return array_values($this->elements);
244
    }
245
246
    /**
247
     * {@inheritDoc}
248
     */
249
    public function count()
250
    {
251
        return count($this->elements);
252
    }
253
254
    /**
255
     * {@inheritDoc}
256
     */
257
    public function set($key, $value)
258
    {
259
        $this->elements[$key] = $value;
260
    }
261
262
    /**
263
     * {@inheritDoc}
264
     */
265
    public function add($value)
266
    {
267
        $this->elements[] = $value;
268
269
        return true;
270
    }
271
272
    /**
273
     * {@inheritDoc}
274
     */
275
    public function isEmpty()
276
    {
277
        return empty($this->elements);
278
    }
279
280
    /**
281
     * Required by interface IteratorAggregate.
282
     *
283
     * {@inheritDoc}
284
     */
285
    public function getIterator()
286
    {
287
        return new ArrayIterator($this->elements);
288
    }
289
290
    /**
291
     * {@inheritDoc}
292
     */
293
    public function map(Closure $func)
294
    {
295
        return new static(array_map($func, $this->elements));
296
    }
297
298
    /**
299
     * {@inheritDoc}
300
     */
301
    public function filter(Closure $p)
302
    {
303
        return new static(array_filter($this->elements, $p));
304
    }
305
306
    /**
307
     * {@inheritDoc}
308
     */
309
    public function partition(Closure $p)
310
    {
311
        $matches = $noMatches = array();
312
313
        foreach ($this->elements as $key => $element) {
314
            if ($p($key, $element)) {
315
                $matches[$key] = $element;
316
            } else {
317
                $noMatches[$key] = $element;
318
            }
319
        }
320
321
        return array(new static($matches), new static($noMatches));
322
    }
323
324
    /**
325
     * Returns a string representation of this object.
326
     *
327
     * @return string
328
     */
329
    public function __toString()
330
    {
331
        return sprintf('%s@%s', __CLASS__, spl_object_hash($this));
332
    }
333
334
    /**
335
     * {@inheritDoc}
336
     */
337
    public function clear()
338
    {
339
        $this->elements = array();
340
    }
341
342
    /**
343
     * {@inheritDoc}
344
     */
345
    public function slice($offset, $length = null)
346
    {
347
        return array_slice($this->elements, $offset, $length, true);
348
    }
349
}