Passed
Pull Request — master (#2)
by Sebastian
02:16
created

ArrayList::hasValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Copyright (C) 2016 Sebastian Böttger <[email protected]>
5
 * You may use, distribute and modify this code under the
6
 * terms of the MIT license.
7
 *
8
 * You should have received a copy of the MIT license with
9
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
10
 */
11
12
namespace Seboettg\Collection;
13
14
/**
15
 * ArrayList is a useful wrapper class for an array, similar to Java's ArrayList
16
 * @package Seboettg\Collection
17
 *
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
class ArrayList implements Collection
21
{
22
23
    /**
24
     * internal array
25
     *
26
     * @var array
27
     */
28
    protected $array;
29
30
    /**
31
     * ArrayList constructor.
32
     * @param array $data
33
     */
34
    public function __construct(array $data = [])
35
    {
36
        $this->array = $data;
37
    }
38
39
    /**
40
     * flush array list
41
     *
42
     * @return $this
43
     */
44
    public function clear()
45
    {
46
        $this->array = [];
47
        return $this;
48
    }
49
50
    /**
51
     * returns element with key $key
52
     * @param $key
53
     * @return mixed|null
54
     */
55
    public function get($key)
56
    {
57
        return isset($this->array[$key]) ? $this->array[$key] : null;
58
    }
59
60
    /**
61
     * Inserts or replaces the element at the specified position in this list with the specified element.
62
     *
63
     * @param $key
64
     * @param $element
65
     * @return $this
66
     */
67
    public function set($key, $element)
68
    {
69
        $this->array[$key] = $element;
70
        return $this;
71
    }
72
73
    /**
74
     * overrides contents of ArrayList with the contents of $array
75
     * @param array $array
76
     * @return $this
77
     */
78
    public function setArray(array $array)
79
    {
80
        return $this->replace($array);
81
    }
82
83
    /**
84
     * Appends the specified element to the end of this list.
85
     *
86
     * @param $element
87
     * @return $this
88
     */
89
    public function append($element)
90
    {
91
        $this->array[] = $element;
92
        return $this;
93
    }
94
95
    /**
96
     * Inserts the specified element at the specified position in this list. If an other element already exist at the
97
     * specified position the affected positions will transformed into a numerated array. As well the existing element
98
     * as the specified element will be appended to this array.
99
     *
100
     * @param $key
101
     * @param $element
102
     * @return $this
103
     */
104
    public function add($key, $element)
105
    {
106
107
        if (!array_key_exists($key, $this->array)) {
108
            $this->array[$key] = $element;
109
        } elseif (is_array($this->array[$key])) {
110
            $this->array[$key][] = $element;
111
        } else {
112
            $this->array[$key] = [$this->array[$key], $element];
113
        }
114
115
        return $this;
116
    }
117
118
    /**
119
     * Removes the element at the specified position in this list.
120
     *
121
     * @param $key
122
     * @return $this
123
     */
124
    public function remove($key)
125
    {
126
        unset($this->array[$key]);
127
        return $this;
128
    }
129
130
    /**
131
     * Returns true if an element exists on the specified position.
132
     *
133
     * @param mixed $key
134
     * @return bool
135
     */
136
    public function hasKey($key)
137
    {
138
        return array_key_exists($key, $this->array);
139
    }
140
141
    /**
142
     * Returns true if the specified value exists in this list. Uses PHP's array_search function
143
     * @link http://php.net/manual/en/function.array-search.php
144
     *
145
     * @param string $value
146
     *
147
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
148
     */
149
    public function hasValue($value)
150
    {
151
        $result = array_search($value, $this->array, true);
152
        return ($result !== false);
153
    }
154
155
    /**
156
     * replaces this list by the specified array
157
     * @param array $data
158
     *
159
     * @return ArrayList
160
     */
161
    public function replace(array $data)
162
    {
163
        $this->array = $data;
164
        return $this;
165
    }
166
167
    /**
168
     * {@inheritDoc}
169
     */
170
    public function getIterator()
171
    {
172
        return new \ArrayIterator($this->array);
173
    }
174
175
    /**
176
     * Offset to retrieve
177
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
178
     * @param mixed $offset The offset to retrieve.
179
     *
180
     * @return mixed Can return all value types.
181
     */
182
    public function offsetGet($offset)
183
    {
184
        return isset($this->array[$offset]) ? $this->array[$offset] : null;
185
    }
186
187
    /**
188
     * Offset to set
189
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
190
     * @param mixed $offset The offset to assign the value to.
191
     * @param mixed $value The value to set.
192
     */
193
    public function offsetSet($offset, $value)
194
    {
195
        $this->array[$offset] = $value;
196
    }
197
198
    /**
199
     * Whether a offset exists
200
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
201
     *
202
     * @param  mixed $offset
203
     * @return bool
204
     */
205
    public function offsetExists($offset)
206
    {
207
        return isset($this->array[$offset]);
208
    }
209
210
    /**
211
     * Offset to unset
212
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
213
     * @param mixed $offset The offset to unset.
214
     */
215
    public function offsetUnset($offset)
216
    {
217
        unset($this->array[$offset]);
218
    }
219
220
    /**
221
     * {@inheritDoc}
222
     */
223
    public function toArray()
224
    {
225
        return $this->array;
226
    }
227
228
    /**
229
     * {@inheritDoc}
230
     */
231
    public function count()
232
    {
233
        return count($this->array);
234
    }
235
236
    /**
237
     * Shuffles this list (randomizes the order of the elements in). It uses the PHP function shuffle
238
     * @see http://php.net/manual/en/function.shuffle.php
239
     * @return $this
240
     */
241
    public function shuffle() {
242
        shuffle($this->array);
243
        return $this;
244
    }
245
246
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
247
     * @param $order
248
     *
249
    public function sort($order = Comparator::ORDER_ASC)
250
    {
251
        Collections::sort($this, new class($order) extends Comparator {
252
253
254
            public function compare(Comparable $a, Comparable $b)
255
            {
256
                if ($this->sortingOrder === Comparator::ORDER_ASC) {
257
                    return $a->compareTo($b);
258
                }
259
                return $b->compareTo($a);
260
            }
261
        });
262
    }
263
    */
264
}
265