Completed
Push — master ( c4431a...dfa02b )
by Zoltán
02:49
created

ArrayList   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 210
Duplicated Lines 15.71 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 2 Features 3
Metric Value
wmc 35
c 9
b 2
f 3
lcom 1
cbo 4
dl 33
loc 210
ccs 89
cts 89
cp 1
rs 9

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A add() 0 3 1
A addAll() 0 5 2
A addTo() 0 5 1
A get() 0 5 2
A set() 0 12 2
A contains() 0 3 2
A containsAt() 0 5 1
A containsAll() 15 15 3
A containsAny() 11 11 3
A indexOf() 0 3 1
B equals() 0 17 5
A removeAt() 0 7 2
A removeAll() 0 8 2
A subList() 0 5 1
A retainAll() 7 11 3
A checkIndex() 0 7 2

How to fix   Duplicated Code   

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:

1
<?php namespace BuildR\Collection\ArrayList;
2
3
use BuildR\Collection\Collection\AbstractCollection;
4
use BuildR\Collection\Collection\FilterableCollectionTrait;
5
use BuildR\Collection\Exception\ListException;
6
7
/**
8
 * ArrayList implementation
9
 *
10
 * BuildR PHP Framework
11
 *
12
 * @author Zoltán Borsos <[email protected]>
13
 * @package Collection
14
 * @subpackage ArrayList
15
 *
16
 * @copyright    Copyright 2015, Zoltán Borsos.
17
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
18
 * @link         https://github.com/Zolli/BuildR
19
 */
20
class ArrayList extends AbstractCollection implements ListInterface {
21
22
    use FilterableCollectionTrait;
23
24
    /**
25
     * ArrayList constructor.
26
     *
27
     * @param array|NULL $elements
28
     */
29 38
    public function __construct($elements = NULL) {
30 38
        if(is_array($elements)) {
31 9
            $this->addAll($elements);
32 9
        }
33 38
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 29
    public function add($element) {
39 29
        $this->data[] = $element;
40 29
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 28
    public function addAll($elements) {
46 28
        foreach($elements as $element) {
47 28
            $this->add($element);
48 28
        }
49 28
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54 1
    public function addTo($index, $element) {
55 1
        $index = $this->checkIndex($index);
56
57 1
        array_splice($this->data, $index, 0, $element);
58 1
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 15
    public function get($index) {
64 15
        $index = $this->checkIndex($index);
65
66 15
        return (isset($this->data[$index])) ? $this->data[$index] : NULL;
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72 2
    public function set($index, $element) {
73 2
        $index = $this->checkIndex($index);
74 2
        $returns = NULL;
75
76 2
        if(isset($this->data[$index])) {
77 2
            $returns = $this->data[$index];
78 2
        }
79
80 2
        $this->data[$index] = $element;
81
82 2
        return $returns;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 9
    public function contains($element) {
89 9
        return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 3
    public function containsAt($index) {
96 3
        $index = $this->checkIndex($index);
97
98 3
        return isset($this->data[$index]);
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 5 View Code Duplication
    public function containsAll($elements) {
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 5
        $elements = $this->collectionToArray($elements);
106
107 5
        $result = TRUE;
108
109 5
        foreach($elements as $item) {
110 5
            if($this->contains($item) === FALSE) {
111 2
                $result = FALSE;
112
113 2
                break;
114
            }
115 5
        }
116
117 5
        return $result;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123 2 View Code Duplication
    public function containsAny($elements) {
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...
124 2
        $elements = $this->collectionToArray($elements);
125
126 2
        foreach($elements as $item) {
127 2
            if($this->contains($item) === TRUE) {
128 2
                return TRUE;
129
            }
130 2
        }
131
132 2
        return FALSE;
133
    }
134
135
    /**
136
     * {@inheritDoc}
137
     */
138 5
    public function indexOf($element) {
139 5
        return array_search($element, $this->data, TRUE);
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145 1
    public function equals(ListInterface $collection) {
146 1
        if($collection->size() !== $this->size()) {
147 1
            return FALSE;
148
        }
149
150 1
        $elements = $collection->toArray();
151
152 1
        foreach($elements as $key => $value) {
153 1
            if(isset($this->data[$key]) && $this->data[$key] === $value) {
154 1
                continue;
155
            }
156
157 1
            return FALSE;
158 1
        }
159
160 1
        return TRUE;
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166 3
    public function removeAt($index) {
167 3
        $index = $this->checkIndex($index);
168
169 3
        if(isset($this->data[$index])) {
170 3
            unset($this->data[$index]);
171 3
        }
172 3
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177 2
    public function removeAll($elements) {
178 2
        $elements = $this->collectionToArray($elements);
179
180 2
        foreach($elements as $item) {
181 2
            $index = $this->indexOf($item);
182 2
            $this->removeAt($index);
183 2
        }
184 2
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189 1
    public function subList($offset, $length) {
190 1
        $slice = array_slice($this->data, $offset, $length, FALSE);
191
192 1
        return new static($slice);
193
    }
194
195
    /**
196
     * {@inheritDoc}
197
     */
198 2
    public function retainAll($elements) {
199 2
        $elements = $this->collectionToArray($elements);
200
201 2 View Code Duplication
        foreach($this->data as $index => $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
202 2
            if(array_search($item, $elements, TRUE) !== FALSE) {
203 2
                continue;
204
            }
205
206 2
            unset($this->data[$index]);
207 2
        }
208 2
    }
209
210
    /**
211
     * Validates the given index. Check if this a numeric value
212
     * and throws an exception if not. If the index is numeric
213
     * the value is casted to array and returned.
214
     *
215
     * @param mixed $index
216
     *
217
     * @return int
218
     *
219
     * @throws \BuildR\Collection\Exception\ListException
220
     */
221 25
    protected function checkIndex($index) {
222 25
        if(!is_numeric($index)) {
223 5
            throw ListException::nonNumericIndex(gettype($index));
224
        }
225
226 20
        return (int) $index;
227
    }
228
229
}
230