Completed
Push — master ( 09a151...fdd724 )
by Zoltán
02:38
created

ArrayList::equals()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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