Completed
Push — master ( ef526e...dbde0c )
by Zoltán
26:25 queued 16:13
created

ArrayList::removeAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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 22
    public function __construct($elements = NULL) {
26 22
        if(is_array($elements)) {
27 3
            $this->addAll($elements);
28 3
        }
29 22
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34 21
    public function add($element) {
35 21
        $this->data[] = $element;
36 21
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 20
    public function addAll($elements) {
42 20
        foreach($elements as $element) {
43 20
            $this->add($element);
44 20
        }
45 20
    }
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 13
    public function get($index) {
58 13
        return (isset($this->data[$index])) ? $this->data[$index] : NULL;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 2
    public function filter(callable $filter) {
65 2
        if(defined('HHVM_VERSION')) {
66
            $returnedList = new static();
67
68
            foreach($this->data as $index => $value) {
69
                if(call_user_func_array($filter, [$value, $index]) === TRUE) {
70
                    $returnedList->add($value);
71
                }
72
            }
73
74
            return $returnedList;
75
        }
76
77 2
        $result = array_filter($this->data, $filter, ARRAY_FILTER_USE_BOTH);
78
79 2
        return new static($result);
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 2
    public function set($index, $element) {
86 2
        $returns = NULL;
87 2
        if(isset($this->data[$index])) {
88 2
            $returns = $this->data[$index];
89 2
        }
90
91 2
        $this->data[$index] = $element;
92
93 2
        return $returns;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 9
    public function contains($element) {
100 9
        return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 3
    public function containsAt($index) {
107 3
        return isset($this->data[$index]);
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 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...
114 5
        $elements = $this->collectionToArray($elements);
115
116 5
        $result = TRUE;
117
118 5
        foreach($elements as $item) {
119 5
            if($this->contains($item) === FALSE) {
120 2
                $result = FALSE;
121
122 2
                break;
123
            }
124 5
        }
125
126 5
        return $result;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132 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...
133 2
        $elements = $this->collectionToArray($elements);
134
135 2
        foreach($elements as $item) {
136 2
            if($this->contains($item) === TRUE) {
137 2
                return TRUE;
138
            }
139 2
        }
140
141 2
        return FALSE;
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147 5
    public function indexOf($element) {
148 5
        return array_search($element, $this->data, TRUE);
149
    }
150
151
    /**
152
     * {@inheritDoc}
153
     */
154 1
    public function equals(ListInterface $collection) {
155 1
        if($collection->size() !== $this->size()) {
156 1
            return FALSE;
157
        }
158
159 1
        $elements = $collection->toArray();
160
161 1
        foreach($elements as $key => $value) {
162 1
            if(isset($this->data[$key]) && $this->data[$key] === $value) {
163 1
                continue;
164
            }
165
166 1
            return FALSE;
167 1
        }
168
169 1
        return TRUE;
170
    }
171
172
    /**
173
     * {@inheritDoc}
174
     */
175 3
    public function removeAt($index) {
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