Completed
Push — master ( 4d9441...c4431a )
by Zoltán
04:15 queued 01:16
created

ArrayList::checkIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php namespace BuildR\Collection\ArrayList;
2
3
use BuildR\Collection\Collection\AbstractCollection;
4
use BuildR\Collection\Exception\ListException;
5
use BuildR\Collection\Utils\ArrayFilter;
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
    /**
23
     * ArrayList constructor.
24
     *
25
     * @param array|NULL $elements
26
     */
27 38
    public function __construct($elements = NULL) {
28 38
        if(is_array($elements)) {
29 9
            $this->addAll($elements);
30 9
        }
31 38
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36 29
    public function add($element) {
37 29
        $this->data[] = $element;
38 29
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 28
    public function addAll($elements) {
44 28
        foreach($elements as $element) {
45 28
            $this->add($element);
46 28
        }
47 28
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 1
    public function addTo($index, $element) {
53 1
        $index = $this->checkIndex($index);
54
55 1
        array_splice($this->data, $index, 0, $element);
56 1
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 15
    public function get($index) {
62 15
        $index = $this->checkIndex($index);
63
64 15
        return (isset($this->data[$index])) ? $this->data[$index] : NULL;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 2
    public function filter(callable $filter) {
71 2
        $result = ArrayFilter::execute($this->data, $filter);
72
73 2
        return new static($result);
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 2
    public function set($index, $element) {
80 2
        $index = $this->checkIndex($index);
81 2
        $returns = NULL;
82
83 2
        if(isset($this->data[$index])) {
84 2
            $returns = $this->data[$index];
85 2
        }
86
87 2
        $this->data[$index] = $element;
88
89 2
        return $returns;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 9
    public function contains($element) {
96 9
        return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 3
    public function containsAt($index) {
103 3
        $index = $this->checkIndex($index);
104
105 3
        return isset($this->data[$index]);
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     */
111 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...
112 5
        $elements = $this->collectionToArray($elements);
113
114 5
        $result = TRUE;
115
116 5
        foreach($elements as $item) {
117 5
            if($this->contains($item) === FALSE) {
118 2
                $result = FALSE;
119
120 2
                break;
121
            }
122 5
        }
123
124 5
        return $result;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130 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...
131 2
        $elements = $this->collectionToArray($elements);
132
133 2
        foreach($elements as $item) {
134 2
            if($this->contains($item) === TRUE) {
135 2
                return TRUE;
136
            }
137 2
        }
138
139 2
        return FALSE;
140
    }
141
142
    /**
143
     * {@inheritDoc}
144
     */
145 5
    public function indexOf($element) {
146 5
        return array_search($element, $this->data, TRUE);
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152 1
    public function equals(ListInterface $collection) {
153 1
        if($collection->size() !== $this->size()) {
154 1
            return FALSE;
155
        }
156
157 1
        $elements = $collection->toArray();
158
159 1
        foreach($elements as $key => $value) {
160 1
            if(isset($this->data[$key]) && $this->data[$key] === $value) {
161 1
                continue;
162
            }
163
164 1
            return FALSE;
165 1
        }
166
167 1
        return TRUE;
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173 3
    public function removeAt($index) {
174 3
        $index = $this->checkIndex($index);
175
176 3
        if(isset($this->data[$index])) {
177 3
            unset($this->data[$index]);
178 3
        }
179 3
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184 2
    public function removeAll($elements) {
185 2
        $elements = $this->collectionToArray($elements);
186
187 2
        foreach($elements as $item) {
188 2
            $index = $this->indexOf($item);
189 2
            $this->removeAt($index);
190 2
        }
191 2
    }
192
193
    /**
194
     * {@inheritDoc}
195
     */
196 1
    public function subList($offset, $length) {
197 1
        $slice = array_slice($this->data, $offset, $length, FALSE);
198
199 1
        return new static($slice);
200
    }
201
202
    /**
203
     * {@inheritDoc}
204
     */
205 2
    public function retainAll($elements) {
206 2
        $elements = $this->collectionToArray($elements);
207
208 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...
209 2
            if(array_search($item, $elements, TRUE) !== FALSE) {
210 2
                continue;
211
            }
212
213 2
            unset($this->data[$index]);
214 2
        }
215 2
    }
216
217
    /**
218
     * Validates the given index. Check if this a numeric value
219
     * and throws an exception if not. If the index is numeric
220
     * the value is casted to array and returned.
221
     *
222
     * @param mixed $index
223
     *
224
     * @return int
225
     *
226
     * @throws \BuildR\Collection\Exception\ListException
227
     */
228 25
    protected function checkIndex($index) {
229 25
        if(!is_numeric($index)) {
230 5
            throw ListException::nonNumericIndex(gettype($index));
231
        }
232
233 20
        return (int) $index;
234
    }
235
236
}
237