Completed
Push — master ( 038f1d...f91c10 )
by Zoltán
12:36 queued 21s
created

ArrayList::containsAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 ArrayListInterface {
19
20
    /**
21
     * ArrayList constructor.
22
     *
23
     * @param array|NULL $elements
24
     */
25 24
    public function __construct($elements = NULL) {
26 24
        if(is_array($elements)) {
27 5
            $this->addAll($elements);
28 5
        }
29 24
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34 23
    public function add($element) {
35 23
        $this->data[] = $element;
36 23
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 22
    public function addAll($elements) {
42 22
        foreach($elements as $element) {
43 22
            $this->add($element);
44 22
        }
45 22
    }
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
        $result = array_filter($this->data, $filter, ARRAY_FILTER_USE_BOTH);
66
67 2
        return new static($result);
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 2
    public function set($index, $element) {
74 2
        $returns = NULL;
75 2
        if(isset($this->data[$index])) {
76 2
            $returns = $this->data[$index];
77 2
        }
78
79 2
        $this->data[$index] = $element;
80
81 2
        return $returns;
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 9
    public function contains($element) {
88 9
        return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 3
    public function containsAt($index) {
95 3
        return isset($this->data[$index]);
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 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...
102 5
        $elements = $this->collectionToArray($elements);
103
104 5
        $result = TRUE;
105
106 5
        foreach($elements as $item) {
107 5
            if($this->contains($item) === FALSE) {
108 2
                $result = FALSE;
109
110 2
                break;
111
            }
112 5
        }
113
114 5
        return $result;
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 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...
121 2
        $elements = $this->collectionToArray($elements);
122
123 2
        foreach($elements as $item) {
124 2
            if($this->contains($item) === TRUE) {
125 2
                return TRUE;
126
            }
127 2
        }
128
129 2
        return FALSE;
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 5
    public function indexOf($element) {
136 5
        return array_search($element, $this->data, TRUE);
0 ignored issues
show
Bug Compatibility introduced by
The expression array_search($element, $this->data, TRUE); of type false|integer|string adds the type string to the return on line 136 which is incompatible with the return type declared by the interface BuildR\Collection\ArrayL...yListInterface::indexOf of type integer|boolean.
Loading history...
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 1
    public function equals(ArrayListInterface $collection) {
143 1
        if($collection->size() !== $this->size()) {
144 1
            return FALSE;
145
        }
146
147 1
        $elements = $collection->toArray();
148
149 1
        foreach($elements as $key => $value) {
150 1
            if(isset($this->data[$key]) && $this->data[$key] === $value) {
151 1
                continue;
152
            }
153
154 1
            return FALSE;
155 1
        }
156
157 1
        return TRUE;
158
    }
159
160
    /**
161
     * {@inheritDoc}
162
     */
163 3
    public function removeAt($index) {
164 3
        if(isset($this->data[$index])) {
165 3
            unset($this->data[$index]);
166 3
        }
167 3
    }
168
169
    /**
170
     * {@inheritDoc}
171
     */
172 2
    public function removeAll($elements) {
173 2
        $elements = $this->collectionToArray($elements);
174
175 2
        foreach($elements as $item) {
176 2
            $index = $this->indexOf($item);
177 2
            $this->removeAt($index);
178 2
        }
179 2
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184 1
    public function subList($offset, $length) {
185 1
        $slice = array_slice($this->data, $offset, $length, FALSE);
186
187 1
        return new static($slice);
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193 2
    public function retainAll($elements) {
194 2
        $elements = $this->collectionToArray($elements);
195
196 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...
197 2
            if(array_search($item, $elements, TRUE) !== FALSE) {
198 2
                continue;
199
            }
200
201 2
            unset($this->data[$index]);
202 2
        }
203 2
    }
204
205
}
206