Javascript::includes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Recca0120\Lodash\JArray;
4
5
use Recca0120\Lodash\JString;
6
7
trait Javascript
8
{
9
    /**
10
     * The toString() method returns a string representing the specified array and its elements.
11
     *
12
     * @return string
13
     */
14 1
    public function __toString()
15
    {
16 1
        return $this->toString();
17
    }
18
19
    /**
20
     * The concat() method is used to merge two or more arrays.
21
     * This method does not change the existing arrays, but instead returns a new array.
22
     *
23
     * @return static
24
     */
25 4
    public function concat()
26
    {
27 4
        $array = $this->getArrayCopy();
28 4
        foreach (func_get_args() as $value) {
29 4
            $array = array_merge($array, (array) $value);
30 4
        }
31
32 4
        return new static($array);
33
    }
34
35
    /**
36
     * The copyWithin() method shallow copies part of an array to another location in the same array and returns it,
37
     * without modifying its size.
38
     *
39
     * @param int $target
40
     * @param int $start
41
     * @param int $end
42
     * @return static
43
     */
44
    public function copyWithin($target, $start, $end)
0 ignored issues
show
Unused Code introduced by
The parameter $target is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $start is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $end is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
    }
47
48
    /**
49
     * The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
50
     *
51
     * @return ArrayIterator
52
     */
53 3
    public function entries()
54
    {
55 3
        return $this->getIterator();
0 ignored issues
show
Bug introduced by
It seems like getIterator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
    }
57
58
    /**
59
     * The every() method tests whether all elements in the array pass the test implemented by the provided function.
60
     *
61
     * @param callable $callback
62
     * @return bool
63
     */
64 1 View Code Duplication
    public function every(callable $callback)
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...
65
    {
66 1
        $array = $this->getArrayCopy();
67 1
        foreach ($array as $key => $value) {
68 1
            if ($callback($value, $key, $array) === false) {
69 1
                return false;
70
            }
71 1
        }
72
73 1
        return true;
74
    }
75
76
    /**
77
     * The fill() method fills all the elements of an array from a start index to an end index with a static value.
78
     *
79
     * @param  $value
80
     * @param  $start
81
     * @param  $end
82
     * @return static
83
     */
84 1
    public function fill($value, $start = null, $end = null)
85
    {
86 1
        $array = $this->getArrayCopy();
87 1
        $length = $this->length();
88
89 1
        if ($end <= 0) {
90 1
            $end = $length - $end;
91 1
        }
92
93 1
        if (is_null($start) === true) {
94 1
            $i = $end ?: $length;
95
96 1
            while ($i--) {
97 1
                $array[$i] = $value;
98 1
            }
99 1
        }
100
101 1
        if ($start <= 0) {
102 1
            $start = $length - $start;
103 1
        }
104
105 1
        $end = $end ?: $length;
106 1
        $i = $start;
107
108 1
        while ($i !== $end) {
109 1
            $array[$i] = $value;
110 1
            $i++;
111 1
        }
112
113 1
        return new static($array);
114
    }
115
116
    /**
117
     * The filter() method creates a new array with all elements that pass the test implemented by the provided function.
118
     *
119
     * @param callable $callback
120
     * @return static
121
     */
122 1
    public function filter(callable $callback)
123
    {
124 1
        return new static(array_filter($this->getArrayCopy(), $callback));
125
    }
126
127
    /**
128
     * The find() method returns a value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
129
     *
130
     * @param callable $callback
131
     * @return mix
132
     */
133 1 View Code Duplication
    public function find(callable $callback)
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...
134
    {
135 1
        $array = $this->getArrayCopy();
136 1
        foreach ($array as $key => $value) {
137 1
            if ($callback($value, $key, $array) === true) {
138 1
                return $value;
139
            }
140 1
        }
141
    }
142
143
    /**
144
     * The findIndex() method returns an index of the first element in the array that satisfies the provided testing function.
145
     * Otherwise -1 is returned.
146
     *
147
     * @param callable $callback
148
     * @return int|string
149
     */
150 1 View Code Duplication
    public function findIndex(callable $callback)
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...
151
    {
152 1
        $array = $this->getArrayCopy();
153 1
        foreach ($array as $key => $value) {
154 1
            if ($callback($value, $key, $array) === true) {
155 1
                return $key;
156
            }
157 1
        }
158
159 1
        return -1;
160
    }
161
162
    /**
163
     * The forEach() method executes a provided function once for each array element.
164
     * php < 7 not allow forEach.
165
     *
166
     * @param callable $callback
167
     */
168 1
    public function each(callable $callback)
169
    {
170 1
        $array = $this->getArrayCopy();
171 1
        foreach ($array as $key => $value) {
172 1
            $callback($value, $key, $array);
173 1
        }
174 1
    }
175
176
    /**
177
     * The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
178
     *
179
     * @param mixed $searchElement
180
     * @param int $fromIndex
181
     * @return bool
182
     */
183 1
    public function includes($searchElement, $fromIndex = 0)
184
    {
185 1
        return in_array($searchElement, $this->slice($fromIndex)->getArrayCopy(), true);
186
    }
187
188
    /**
189
     * The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
190
     *
191
     * @param mixed $searchElement
192
     * @param int $fromIndex
193
     * @return int|string
194
     */
195 1 View Code Duplication
    public function indexOf($searchElement, $fromIndex = 0)
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...
196
    {
197 1
        $result = array_search($searchElement, $this->getArrayCopy(), true);
198
199 1
        return $result === false ? -1 : $result + $fromIndex;
200
201
        // if ($fromIndex < 0) {
202
        //     $fromIndex = $this->length() + $fromIndex;
203
        // }
204
        // $result = array_search($searchElement, $this->slice($fromIndex)->getArrayCopy(), true);
205
206
        // return $result === false ? -1 : $result + $fromIndex;
207
    }
208
209
    /**
210
     * The join() method joins all elements of an array into a string.
211
     *
212
     * @param string $separator
213
     * @return \Recca0120\Lodash\JString
214
     */
215 4
    public function join($separator = ',')
216
    {
217 4
        return new JString(implode($separator, $this->getArrayCopy()));
218
    }
219
220
    /**
221
     * The keys() method returns a new Array Iterator that contains the keys for each index in the array.
222
     *
223
     * @return \ArrayIterator
224
     */
225 1
    public function keys()
226
    {
227 1
        return (new static(array_keys($this->getArrayCopy())))->entries();
228
    }
229
230
    /**
231
     * The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
232
     *
233
     * @param mixed $searchElement
234
     * @param int $fromIndex
235
     * @return int|string
236
     */
237 1 View Code Duplication
    public function lastIndexOf($searchElement, $fromIndex = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $fromIndex is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
238
    {
239 1
        $result = array_search($searchElement, array_reverse($this->getArrayCopy(), true));
240
241 1
        return $result === false ? -1 : $result;
242
        // $result = array_search($searchElement, array_reverse($this->slice($fromIndex)->getArrayCopy(), true));
243
244
        // return ($result === false) ? -1 : $result + $fromIndex;
245
    }
246
247
    /**
248
     * The map() method creates a new array with the results of calling a provided function on every element in this array.
249
     *
250
     * @param callable $callback
251
     * @return static
252
     */
253 2
    public function map(callable $callback)
254
    {
255 2
        return new static(array_map($callback, $this->getArrayCopy()));
256
    }
257
258
    /**
259
     * The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
260
     *
261
     * @return static
262
     */
263 1
    public function pop()
264
    {
265 1
        $array = $this->getArrayCopy();
266 1
        $pop = array_pop($array);
267 1
        $this->exchangeArray($array);
0 ignored issues
show
Bug introduced by
It seems like exchangeArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
268
269 1
        return $pop;
270
    }
271
272
    /**
273
     * The push() method adds one or more elements to the end of an array and returns the new length of the array.
274
     *
275
     * @return int
276
     */
277 1
    public function push()
278
    {
279 1
        $array = $this->getArrayCopy();
280 1
        foreach (func_get_args() as $parameter) {
281 1
            array_push($array, $parameter);
282 1
        }
283 1
        $this->exchangeArray($array);
0 ignored issues
show
Bug introduced by
It seems like exchangeArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
284
285 1
        return $this->count();
286
    }
287
288
    /**
289
     * The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
290
     *
291
     * @param callable $callback
292
     * @param mixed $initialValue
293
     * @return mixed
294
     */
295 2
    public function reduce(callable $callback, $initialValue = null)
296
    {
297 2
        return array_reduce($this->getArrayCopy(), $callback, $initialValue);
298
    }
299
300
    /**
301
     * The reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) has to reduce it to a single value.
302
     *
303
     * @param callable $callback
304
     * @param mixed $initialValue
305
     * @return mixed
306
     */
307 1
    public function reduceRight(callable $callback, $initialValue = null)
308
    {
309 1
        return $this->reverse(true)->reduce($callback, $initialValue);
310
    }
311
312
    /**
313
     * The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
314
     *
315
     * @param bool $preservekeys
316
     * @return static
317
     */
318 2
    public function reverse($preservekeys = false)
319
    {
320 2
        $array = $this->getArrayCopy();
321 2
        $array = array_reverse($array, $preservekeys);
322 2
        $this->exchangeArray($array);
0 ignored issues
show
Bug introduced by
It seems like exchangeArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
323
324 2
        return new static($array);
325
    }
326
327
    /**
328
     * The shift() method removes the first element from an array and returns that element. This method changes the length of the array.
329
     *
330
     * @return mix
331
     */
332 1
    public function shift()
333
    {
334 1
        $array = $this->getArrayCopy();
335 1
        $result = array_shift($array);
336 1
        $this->exchangeArray($array);
0 ignored issues
show
Bug introduced by
It seems like exchangeArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
337
338 1
        return $result;
339
    }
340
341
    /**
342
     * The slice() method returns a shallow copy of a portion of an array
343
     * into a new array object selected from begin to end (end not included). The original array will not be modified.
344
     *
345
     * @param int $begin
346
     * @param int $end
347
     * @return static
348
     */
349 2
    public function slice($begin, $end = null)
350
    {
351 2
        $array = $this->getArrayCopy();
352 2
        $length = $this->length();
353 2
        $begin = $begin < 0 ? $length + $begin : $begin;
354
355 2
        if (is_null($end) === true) {
356 1
            return new static(array_slice($array, $begin, $length - $begin));
357
        }
358
359 1
        $end = $end < 0 ? $length + $end : $end;
360 1
        $end -= $begin;
361
362 1
        return new static(array_slice($array, $begin, $end));
363
    }
364
365
    /**
366
     * The some() method tests whether some element in the array passes the test implemented by the provided function.
367
     *
368
     * @param callable $callback
369
     * @return bool
370
     */
371 1 View Code Duplication
    public function some(callable $callback)
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...
372
    {
373 1
        $array = $this->getArrayCopy();
374 1
        foreach ($array as $key => $value) {
375 1
            if ($callback($value, $key, $array) === true) {
376 1
                return true;
377
            }
378 1
        }
379
380 1
        return false;
381
    }
382
383
    /**
384
     * The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.
385
     *
386
     * @param  callable|null $compareFunction
387
     * @return $this
388
     */
389 1
    public function sort(callable $compareFunction = null)
390
    {
391 1
        if (is_null($compareFunction) === true) {
392 1
            $compareFunction = function ($a, $b) {
393 1
                return $a > $b;
394 1
            };
395 1
        }
396 1
        $array = $this->getArrayCopy();
397 1
        usort($array, $compareFunction);
398 1
        $this->exchangeArray($array);
0 ignored issues
show
Bug introduced by
It seems like exchangeArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
399
400 1
        return $this;
401
    }
402
403
    /**
404
     * The splice() method changes the content of an array by removing existing elements and/or adding new elements.
405
     *
406
     * @return $this
407
     */
408 1 View Code Duplication
    public function splice()
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...
409
    {
410 1
        $array = $this->getArrayCopy();
411 1
        call_user_func_array('array_splice', array_merge([&$array], func_get_args()));
412 1
        $this->exchangeArray($array);
0 ignored issues
show
Bug introduced by
It seems like exchangeArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
413
414 1
        return $this;
415
    }
416
417
    public function toLocaleString()
418
    {
419
    }
420
421
    /**
422
     * The toString() method returns a string representing the specified array and its elements.
423
     *
424
     * @return string
425
     */
426 1
    public function toString()
427
    {
428 1
        return (string) $this->join(',');
429
    }
430
431
    /**
432
     * The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
433
     *
434
     * @return $this
435
     */
436 1 View Code Duplication
    public function unshift()
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...
437
    {
438 1
        $array = $this->getArrayCopy();
439 1
        call_user_func_array('array_unshift', array_merge([&$array], func_get_args()));
440 1
        $this->exchangeArray($array);
441
442 1
        return $this;
443
    }
444
445
    /**
446
     * The values() method returns a new Array Iterator object that contains the values for each index in the array.
447
     *
448
     * @return \ArrayIterator
449
     */
450 1
    public function values()
451
    {
452 1
        return (new static(array_values($this->getArrayCopy())))->entries();
453
    }
454
455
    /**
456
     * length.
457
     *
458
     * @return int
459
     */
460 3
    public function length()
461
    {
462 3
        return $this->count();
0 ignored issues
show
Bug introduced by
It seems like count() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
463
    }
464
}
465