ArrayList::containsAny()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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