Completed
Push — master ( dbde0c...7e9c38 )
by Zoltán
30:20 queued 20:45
created

ArrayList::addTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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
            return $this->executeHhvmArrayFilter($filter);
67
        }
68
69 2
        $result = array_filter($this->data, $filter, ARRAY_FILTER_USE_BOTH);
70
71 2
        return new static($result);
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 2
    public function set($index, $element) {
78 2
        $returns = NULL;
79 2
        if(isset($this->data[$index])) {
80 2
            $returns = $this->data[$index];
81 2
        }
82
83 2
        $this->data[$index] = $element;
84
85 2
        return $returns;
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 9
    public function contains($element) {
92 9
        return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 3
    public function containsAt($index) {
99 3
        return isset($this->data[$index]);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 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...
106 5
        $elements = $this->collectionToArray($elements);
107
108 5
        $result = TRUE;
109
110 5
        foreach($elements as $item) {
111 5
            if($this->contains($item) === FALSE) {
112 2
                $result = FALSE;
113
114 2
                break;
115
            }
116 5
        }
117
118 5
        return $result;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124 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...
125 2
        $elements = $this->collectionToArray($elements);
126
127 2
        foreach($elements as $item) {
128 2
            if($this->contains($item) === TRUE) {
129 2
                return TRUE;
130
            }
131 2
        }
132
133 2
        return FALSE;
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139 5
    public function indexOf($element) {
140 5
        return array_search($element, $this->data, TRUE);
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146 1
    public function equals(ListInterface $collection) {
147 1
        if($collection->size() !== $this->size()) {
148 1
            return FALSE;
149
        }
150
151 1
        $elements = $collection->toArray();
152
153 1
        foreach($elements as $key => $value) {
154 1
            if(isset($this->data[$key]) && $this->data[$key] === $value) {
155 1
                continue;
156
            }
157
158 1
            return FALSE;
159 1
        }
160
161 1
        return TRUE;
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     */
167 3
    public function removeAt($index) {
168 3
        if(isset($this->data[$index])) {
169 3
            unset($this->data[$index]);
170 3
        }
171 3
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176 2
    public function removeAll($elements) {
177 2
        $elements = $this->collectionToArray($elements);
178
179 2
        foreach($elements as $item) {
180 2
            $index = $this->indexOf($item);
181 2
            $this->removeAt($index);
182 2
        }
183 2
    }
184
185
    /**
186
     * {@inheritDoc}
187
     */
188 1
    public function subList($offset, $length) {
189 1
        $slice = array_slice($this->data, $offset, $length, FALSE);
190
191 1
        return new static($slice);
192
    }
193
194
    /**
195
     * {@inheritDoc}
196
     */
197 2
    public function retainAll($elements) {
198 2
        $elements = $this->collectionToArray($elements);
199
200 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...
201 2
            if(array_search($item, $elements, TRUE) !== FALSE) {
202 2
                continue;
203
            }
204
205 2
            unset($this->data[$index]);
206 2
        }
207 2
    }
208
209
    /**
210
     * The HHVM is not compatible with the $flag attribute that introduced in PHP 5.6
211
     * This is a fallback method that provides same functionality on HHVM that
212
     * ARRAY_FILTER_BOTH argument on PHP 5.6
213
     *
214
     * @param callable $filter
215
     *
216
     * @return static
217
     */
218
    protected function executeHhvmArrayFilter(callable $filter) {
219
        $returnedList = new static();
220
221
        foreach($this->data as $index => $value) {
222
            if(call_user_func_array($filter, [$value, $index]) === TRUE) {
223
                $returnedList->add($value);
224
            }
225
        }
226
227
        return $returnedList;
228
    }
229
230
}
231