Completed
Push — master ( 575ef6...5305a0 )
by Zoltán
12:40
created

ArrayList   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 215
Duplicated Lines 15.35 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 2 Features 1
Metric Value
wmc 38
c 7
b 2
f 1
lcom 1
cbo 2
dl 33
loc 215
ccs 91
cts 91
cp 1
rs 8.3999

18 Methods

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

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