Completed
Pull Request — master (#3)
by
unknown
02:44
created

EnumeratesValues::each()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Arr\Support\Traits;
4
5
use CachingIterator;
6
use Exception;
7
use IlluminateAgnostic\Arr\Contracts\Support\Arrayable;
8
use IlluminateAgnostic\Arr\Contracts\Support\Jsonable;
9
use IlluminateAgnostic\Arr\Support\Arr;
10
use IlluminateAgnostic\Arr\Support\Collection;
11
use IlluminateAgnostic\Arr\Support\Enumerable;
12
use IlluminateAgnostic\Arr\Support\HigherOrderCollectionProxy;
13
use JsonSerializable;
14
use Symfony\Component\VarDumper\VarDumper;
15
use Traversable;
16
17
/**
18
 * @property-read HigherOrderCollectionProxy $average
19
 * @property-read HigherOrderCollectionProxy $avg
20
 * @property-read HigherOrderCollectionProxy $contains
21
 * @property-read HigherOrderCollectionProxy $each
22
 * @property-read HigherOrderCollectionProxy $every
23
 * @property-read HigherOrderCollectionProxy $filter
24
 * @property-read HigherOrderCollectionProxy $first
25
 * @property-read HigherOrderCollectionProxy $flatMap
26
 * @property-read HigherOrderCollectionProxy $groupBy
27
 * @property-read HigherOrderCollectionProxy $keyBy
28
 * @property-read HigherOrderCollectionProxy $map
29
 * @property-read HigherOrderCollectionProxy $max
30
 * @property-read HigherOrderCollectionProxy $min
31
 * @property-read HigherOrderCollectionProxy $partition
32
 * @property-read HigherOrderCollectionProxy $reject
33
 * @property-read HigherOrderCollectionProxy $sortBy
34
 * @property-read HigherOrderCollectionProxy $sortByDesc
35
 * @property-read HigherOrderCollectionProxy $sum
36
 * @property-read HigherOrderCollectionProxy $unique
37
 */
38
trait EnumeratesValues
39
{
40
    /**
41
     * The methods that can be proxied.
42
     *
43
     * @var array
44
     */
45
    protected static $proxies = [
46
        'average', 'avg', 'contains', 'each', 'every', 'filter', 'first',
47
        'flatMap', 'groupBy', 'keyBy', 'map', 'max', 'min', 'partition',
48
        'reject', 'some', 'sortBy', 'sortByDesc', 'sum', 'unique',
49
    ];
50
51
    /**
52
     * Create a new collection instance if the value isn't one already.
53
     *
54
     * @param  mixed  $items
55
     * @return static
56
     */
57
    public static function make($items = [])
58 30
    {
59
        return new static($items);
0 ignored issues
show
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with $items.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60 30
    }
61
62
    /**
63
     * Wrap the given value in a collection if applicable.
64
     *
65
     * @param  mixed  $value
66
     * @return static
67
     */
68
    public static function wrap($value)
69 14
    {
70
        return $value instanceof Enumerable
71 14
            ? new static($value)
0 ignored issues
show
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with $value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
72 4
            : new static(Arr::wrap($value));
0 ignored issues
show
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with \IlluminateAgnostic\Arr\Support\Arr::wrap($value).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73 14
    }
74
75
    /**
76
     * Get the underlying items from the given collection if applicable.
77
     *
78
     * @param  array|static  $value
79
     * @return array
80
     */
81
    public static function unwrap($value)
82 6
    {
83
        return $value instanceof Enumerable ? $value->all() : $value;
84 6
    }
85
86
    /**
87
     * Alias for the "avg" method.
88
     *
89
     * @param  callable|string|null  $callback
90
     * @return mixed
91
     */
92
    public function average($callback = null)
93 6
    {
94
        return $this->avg($callback);
0 ignored issues
show
Bug introduced by
It seems like avg() 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...
95 6
    }
96
97
    /**
98
     * Alias for the "contains" method.
99
     *
100
     * @param  mixed  $key
101
     * @param  mixed  $operator
102
     * @param  mixed  $value
103
     * @return bool
104
     */
105
    public function some($key, $operator = null, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $key 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 $operator 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 $value 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...
106 2
    {
107
        return $this->contains(...func_get_args());
0 ignored issues
show
Bug introduced by
The method contains() does not exist on IlluminateAgnostic\Arr\S...Traits\EnumeratesValues. Did you maybe mean containsStrict()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
108 2
    }
109
110
    /**
111
     * Determine if an item exists, using strict comparison.
112
     *
113
     * @param  mixed  $key
114
     * @param  mixed  $value
115
     * @return bool
116
     */
117
    public function containsStrict($key, $value = null)
118 2
    {
119
        if (func_num_args() === 2) {
120 2
            return $this->contains(function ($item) use ($key, $value) {
0 ignored issues
show
Bug introduced by
The method contains() does not exist on IlluminateAgnostic\Arr\S...Traits\EnumeratesValues. Did you maybe mean containsStrict()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
121
                return data_get($item, $key) === $value;
122 2
            });
123 2
        }
124
125
        if ($this->useAsCallable($key)) {
126 2
            return ! is_null($this->first($key));
0 ignored issues
show
Bug introduced by
It seems like first() 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...
127 2
        }
128
129
        foreach ($this as $item) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<IlluminateAgnostic\...raits\EnumeratesValues> is not traversable.
Loading history...
130 2
            if ($item === $key) {
131 2
                return true;
132 2
            }
133
        }
134
135
        return false;
136 2
    }
137
138
    /**
139
     * Dump the items and end the script.
140
     *
141
     * @param  mixed  ...$args
142
     * @return void
143
     */
144
    public function dd(...$args)
145
    {
146
        call_user_func_array([$this, 'dump'], $args);
147
148
        die(1);
149
    }
150
151
    /**
152
     * Dump the items.
153
     *
154
     * @return $this
155
     */
156
    public function dump()
157
    {
158
        (new static(func_get_args()))
0 ignored issues
show
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with func_get_args().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
It seems like push() 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...
159
            ->push($this)
160
            ->each(function ($item) {
161
                VarDumper::dump($item);
162
            });
163
164
        return $this;
165
    }
166
167
    /**
168
     * Execute a callback over each item.
169
     *
170
     * @param  callable  $callback
171
     * @return $this
172
     */
173
    public function each(callable $callback)
174 15
    {
175
        foreach ($this as $key => $item) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<IlluminateAgnostic\...raits\EnumeratesValues> is not traversable.
Loading history...
176 15
            if ($callback($item, $key) === false) {
177 15
                break;
178 4
            }
179
        }
180
181
        return $this;
182 15
    }
183
184
    /**
185
     * Execute a callback over each nested chunk of items.
186
     *
187
     * @param  callable  $callback
188
     * @return static
189
     */
190
    public function eachSpread(callable $callback)
191 2
    {
192
        return $this->each(function ($chunk, $key) use ($callback) {
193
            $chunk[] = $key;
194 2
195
            return $callback(...$chunk);
196 2
        });
197 2
    }
198
199
    /**
200
     * Determine if all items pass the given test.
201
     *
202
     * @param  string|callable  $key
203
     * @param  mixed  $operator
204
     * @param  mixed  $value
205
     * @return bool
206
     */
207
    public function every($key, $operator = null, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $operator 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 $value 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...
208 2
    {
209
        if (func_num_args() === 1) {
210 2
            $callback = $this->valueRetriever($key);
211 2
212
            foreach ($this as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<IlluminateAgnostic\...raits\EnumeratesValues> is not traversable.
Loading history...
213 2
                if (! $callback($v, $k)) {
214 2
                    return false;
215 2
                }
216
            }
217
218
            return true;
219 2
        }
220
221
        return $this->every($this->operatorForWhere(...func_get_args()));
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
222 2
    }
223
224
    /**
225
     * Get the first item by the given key value pair.
226
     *
227
     * @param  string  $key
228
     * @param  mixed  $operator
229
     * @param  mixed  $value
230
     * @return mixed
231
     */
232
    public function firstWhere($key, $operator = null, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $key 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 $operator 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 $value 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...
233 2
    {
234
        return $this->first($this->operatorForWhere(...func_get_args()));
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like first() 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...
235 2
    }
236
237
    /**
238
     * Determine if the collection is not empty.
239
     *
240
     * @return bool
241
     */
242
    public function isNotEmpty()
243 18
    {
244
        return ! $this->isEmpty();
0 ignored issues
show
Bug introduced by
It seems like isEmpty() 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...
245 18
    }
246
247
    /**
248
     * Run a map over each nested chunk of items.
249
     *
250
     * @param  callable  $callback
251
     * @return static
252
     */
253
    public function mapSpread(callable $callback)
254 2
    {
255
        return $this->map(function ($chunk, $key) use ($callback) {
0 ignored issues
show
Bug introduced by
It seems like map() 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...
256
            $chunk[] = $key;
257 2
258
            return $callback(...$chunk);
259 2
        });
260 2
    }
261
262
    /**
263
     * Run a grouping map over the items.
264
     *
265
     * The callback should return an associative array with a single key/value pair.
266
     *
267
     * @param  callable  $callback
268
     * @return static
269
     */
270
    public function mapToGroups(callable $callback)
271 4
    {
272
        $groups = $this->mapToDictionary($callback);
0 ignored issues
show
Bug introduced by
It seems like mapToDictionary() 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...
273 4
274
        return $groups->map([$this, 'make']);
275 4
    }
276
277
    /**
278
     * Map a collection and flatten the result by a single level.
279
     *
280
     * @param  callable  $callback
281
     * @return static
282
     */
283
    public function flatMap(callable $callback)
284 2
    {
285
        return $this->map($callback)->collapse();
0 ignored issues
show
Bug introduced by
It seems like map() 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...
286 2
    }
287
288
    /**
289
     * Map the values into a new class.
290
     *
291
     * @param  string  $class
292
     * @return static
293
     */
294
    public function mapInto($class)
295 2
    {
296
        return $this->map(function ($value, $key) use ($class) {
0 ignored issues
show
Bug introduced by
It seems like map() 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...
297
            return new $class($value, $key);
298 2
        });
299 2
    }
300
301
    /**
302
     * Get the min value of a given key.
303
     *
304
     * @param  callable|string|null  $callback
305
     * @return mixed
306
     */
307 View Code Duplication
    public function min($callback = null)
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...
308 2
    {
309
        $callback = $this->valueRetriever($callback);
310 2
311
        return $this->map(function ($value) use ($callback) {
0 ignored issues
show
Bug introduced by
It seems like map() 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...
312
            return $callback($value);
313 2
        })->filter(function ($value) {
314
            return ! is_null($value);
315 2
        })->reduce(function ($result, $value) {
316
            return is_null($result) || $value < $result ? $value : $result;
317 2
        });
318 2
    }
319
320
    /**
321
     * Get the max value of a given key.
322
     *
323
     * @param  callable|string|null  $callback
324
     * @return mixed
325
     */
326 View Code Duplication
    public function max($callback = null)
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...
327 2
    {
328
        $callback = $this->valueRetriever($callback);
329 2
330
        return $this->filter(function ($value) {
0 ignored issues
show
Bug introduced by
It seems like filter() 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...
331
            return ! is_null($value);
332 2
        })->reduce(function ($result, $item) use ($callback) {
333
            $value = $callback($item);
334 2
335
            return is_null($result) || $value > $result ? $value : $result;
336 2
        });
337 2
    }
338
339
    /**
340
     * "Paginate" the collection by slicing it into a smaller collection.
341
     *
342
     * @param  int  $page
343
     * @param  int  $perPage
344
     * @return static
345
     */
346
    public function forPage($page, $perPage)
347 2
    {
348
        $offset = max(0, ($page - 1) * $perPage);
349 2
350
        return $this->slice($offset, $perPage);
0 ignored issues
show
Bug introduced by
It seems like slice() 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...
351 2
    }
352
353
    /**
354
     * Partition the collection into two arrays using the given callback or key.
355
     *
356
     * @param  callable|string  $key
357
     * @param  mixed  $operator
358
     * @param  mixed  $value
359
     * @return static
360
     */
361
    public function partition($key, $operator = null, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $operator 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 $value 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...
362 14
    {
363
        $passed = [];
364 14
        $failed = [];
365 14
366
        $callback = func_num_args() === 1
367 14
                ? $this->valueRetriever($key)
368 12
                : $this->operatorForWhere(...func_get_args());
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
369 14
370
        foreach ($this as $key => $item) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<IlluminateAgnostic\...raits\EnumeratesValues> is not traversable.
Loading history...
371 14
            if ($callback($item, $key)) {
372 12
                $passed[$key] = $item;
373 12
            } else {
374
                $failed[$key] = $item;
375 12
            }
376
        }
377
378
        return new static([new static($passed), new static($failed)]);
0 ignored issues
show
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with $passed.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with $failed.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with array(new static($passed), new static($failed)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
379 14
    }
380
381
    /**
382
     * Get the sum of the given values.
383
     *
384
     * @param  callable|string|null  $callback
385
     * @return mixed
386
     */
387
    public function sum($callback = null)
388 16
    {
389
        if (is_null($callback)) {
390 16
            $callback = function ($value) {
391
                return $value;
392 12
            };
393 12
        } else {
394
            $callback = $this->valueRetriever($callback);
395 4
        }
396
397
        return $this->reduce(function ($result, $item) use ($callback) {
0 ignored issues
show
Bug introduced by
It seems like reduce() 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...
398
            return $result + $callback($item);
399 14
        }, 0);
400 16
    }
401
402
    /**
403
     * Apply the callback if the value is truthy.
404
     *
405
     * @param  bool  $value
406
     * @param  callable  $callback
407
     * @param  callable  $default
408
     * @return static|mixed
409
     */
410
    public function when($value, callable $callback, callable $default = null)
411 24
    {
412
        if ($value) {
413 24
            return $callback($this, $value);
414 16
        } elseif ($default) {
415 20
            return $default($this, $value);
416 8
        }
417
418
        return $this;
419 12
    }
420
421
    /**
422
     * Apply the callback if the collection is empty.
423
     *
424
     * @param  callable  $callback
425
     * @param  callable  $default
426
     * @return static|mixed
427
     */
428
    public function whenEmpty(callable $callback, callable $default = null)
429 8
    {
430
        return $this->when($this->isEmpty(), $callback, $default);
0 ignored issues
show
Bug introduced by
It seems like isEmpty() 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...
431 8
    }
432
433
    /**
434
     * Apply the callback if the collection is not empty.
435
     *
436
     * @param  callable  $callback
437
     * @param  callable  $default
438
     * @return static|mixed
439
     */
440
    public function whenNotEmpty(callable $callback, callable $default = null)
441 8
    {
442
        return $this->when($this->isNotEmpty(), $callback, $default);
443 8
    }
444
445
    /**
446
     * Apply the callback if the value is falsy.
447
     *
448
     * @param  bool  $value
449
     * @param  callable  $callback
450
     * @param  callable  $default
451
     * @return static|mixed
452
     */
453
    public function unless($value, callable $callback, callable $default = null)
454 4
    {
455
        return $this->when(! $value, $callback, $default);
456 4
    }
457
458
    /**
459
     * Apply the callback unless the collection is empty.
460
     *
461
     * @param  callable  $callback
462
     * @param  callable  $default
463
     * @return static|mixed
464
     */
465
    public function unlessEmpty(callable $callback, callable $default = null)
466 4
    {
467
        return $this->whenNotEmpty($callback, $default);
468 4
    }
469
470
    /**
471
     * Apply the callback unless the collection is not empty.
472
     *
473
     * @param  callable  $callback
474
     * @param  callable  $default
475
     * @return static|mixed
476
     */
477
    public function unlessNotEmpty(callable $callback, callable $default = null)
478 4
    {
479
        return $this->whenEmpty($callback, $default);
480 4
    }
481
482
    /**
483
     * Filter items by the given key value pair.
484
     *
485
     * @param  string  $key
486
     * @param  mixed  $operator
487
     * @param  mixed  $value
488
     * @return static
489
     */
490
    public function where($key, $operator = null, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $key 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 $operator 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 $value 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...
491 14
    {
492
        return $this->filter($this->operatorForWhere(...func_get_args()));
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
It seems like filter() 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...
493 14
    }
494
495
    /**
496
     * Filter items by the given key value pair using strict comparison.
497
     *
498
     * @param  string  $key
499
     * @param  mixed  $value
500
     * @return static
501
     */
502 4
    public function whereStrict($key, $value)
503
    {
504 4
        return $this->where($key, '===', $value);
505
    }
506
507
    /**
508
     * Filter items by the given key value pair.
509
     *
510
     * @param  string  $key
511
     * @param  mixed  $values
512
     * @param  bool  $strict
513 4
     * @return static
514
     */
515 4 View Code Duplication
    public function whereIn($key, $values, $strict = false)
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...
516
    {
517
        $values = $this->getArrayableItems($values);
518
519
        return $this->filter(function ($item) use ($key, $values, $strict) {
0 ignored issues
show
Bug introduced by
It seems like filter() 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...
520
            return in_array(data_get($item, $key), $values, $strict);
521
        });
522
    }
523
524
    /**
525 6
     * Filter items by the given key value pair using strict comparison.
526
     *
527 6
     * @param  string  $key
528
     * @param  mixed  $values
529
     * @return static
530
     */
531
    public function whereInStrict($key, $values)
532
    {
533
        return $this->whereIn($key, $values, true);
534
    }
535
536
    /**
537
     * Filter items such that the value of the given key is between the given values.
538 4
     *
539
     * @param  string  $key
540 4
     * @param  array  $values
541
     * @return static
542
     */
543 4
    public function whereBetween($key, $values)
544 4
    {
545
        return $this->where($key, '>=', reset($values))->where($key, '<=', end($values));
546
    }
547
548
    /**
549
     * Filter items such that the value of the given key is not between the given values.
550
     *
551
     * @param  string  $key
552
     * @param  array  $values
553
     * @return static
554 2
     */
555
    public function whereNotBetween($key, $values)
556 2
    {
557
        return $this->filter(function ($item) use ($key, $values) {
0 ignored issues
show
Bug introduced by
It seems like filter() 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...
558
            return data_get($item, $key) < reset($values) || data_get($item, $key) > end($values);
559
        });
560
    }
561
562
    /**
563
     * Filter items by the given key value pair.
564
     *
565
     * @param  string  $key
566 2
     * @param  mixed  $values
567
     * @param  bool  $strict
568 2
     * @return static
569
     */
570 View Code Duplication
    public function whereNotIn($key, $values, $strict = false)
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...
571
    {
572
        $values = $this->getArrayableItems($values);
573
574
        return $this->reject(function ($item) use ($key, $values, $strict) {
575
            return in_array(data_get($item, $key), $values, $strict);
576
        });
577
    }
578 2
579
    /**
580
     * Filter items by the given key value pair using strict comparison.
581 2
     *
582 2
     * @param  string  $key
583
     * @param  mixed  $values
584
     * @return static
585
     */
586
    public function whereNotInStrict($key, $values)
587
    {
588
        return $this->whereNotIn($key, $values, true);
589
    }
590
591
    /**
592
     * Filter the items, removing any items that don't match the given type.
593 4
     *
594
     * @param  string  $type
595 4
     * @return static
596
     */
597
    public function whereInstanceOf($type)
598 4
    {
599 4
        return $this->filter(function ($value) use ($type) {
0 ignored issues
show
Bug introduced by
It seems like filter() 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...
600
            return $value instanceof $type;
601
        });
602
    }
603
604
    /**
605
     * Pass the collection to the given callback and return the result.
606
     *
607
     * @param  callable $callback
608
     * @return mixed
609 2
     */
610
    public function pipe(callable $callback)
611 2
    {
612
        return $callback($this);
613
    }
614
615
    /**
616
     * Pass the collection to the given callback and then return it.
617
     *
618
     * @param  callable  $callback
619
     * @return $this
620 2
     */
621
    public function tap(callable $callback)
622
    {
623 2
        $callback(clone $this);
624 2
625
        return $this;
626
    }
627
628
    /**
629
     * Create a collection of all elements that do not pass a given truth test.
630
     *
631
     * @param  callable|mixed  $callback
632
     * @return static
633 2
     */
634
    public function reject($callback = true)
635 2
    {
636
        $useAsCallable = $this->useAsCallable($callback);
637
638
        return $this->filter(function ($value, $key) use ($callback, $useAsCallable) {
0 ignored issues
show
Bug introduced by
It seems like filter() 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...
639
            return $useAsCallable
640
                ? ! $callback($value, $key)
641
                : $value != $callback;
642
        });
643
    }
644 2
645
    /**
646 2
     * Return only unique items from the collection array.
647
     *
648 2
     * @param  string|callable|null  $key
649
     * @param  bool  $strict
650
     * @return static
651
     */
652
    public function unique($key = null, $strict = false)
653
    {
654
        $callback = $this->valueRetriever($key);
655
656
        $exists = [];
657 26
658
        return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) {
659 26
            if (in_array($id = $callback($item, $key), $exists, $strict)) {
660
                return true;
661
            }
662 26
663 24
            $exists[] = $id;
664 26
        });
665 26
    }
666
667
    /**
668
     * Return only unique items from the collection array using strict comparison.
669
     *
670
     * @param  string|callable|null  $key
671
     * @return static
672
     */
673
    public function uniqueStrict($key = null)
674
    {
675 18
        return $this->unique($key, true);
676
    }
677 18
678
    /**
679 18
     * Collect the values into a collection.
680
     *
681
     * @return \IlluminateAgnostic\Arr\Support\Collection
682 18
     */
683 18
    public function collect()
684
    {
685
        return new Collection($this->all());
0 ignored issues
show
Bug introduced by
It seems like all() 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...
686 18
    }
687 18
688
    /**
689
     * Get the collection of items as a plain array.
690
     *
691
     * @return array
692
     */
693
    public function toArray()
694
    {
695
        return $this->map(function ($value) {
0 ignored issues
show
Bug introduced by
It seems like map() 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...
696 2
            return $value instanceof Arrayable ? $value->toArray() : $value;
697
        })->all();
698 2
    }
699
700
    /**
701
     * Convert the object into something JSON serializable.
702
     *
703
     * @return array
704
     */
705
    public function jsonSerialize()
706 86
    {
707
        return array_map(function ($value) {
708 86
            if ($value instanceof JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
709
                return $value->jsonSerialize();
710
            } elseif ($value instanceof Jsonable) {
711
                return json_decode($value->toJson(), true);
712
            } elseif ($value instanceof Arrayable) {
713
                return $value->toArray();
714
            }
715
716 121
            return $value;
717
        }, $this->all());
0 ignored issues
show
Bug introduced by
It seems like all() 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...
718
    }
719 115
720 121
    /**
721
     * Get the collection of items as JSON.
722
     *
723
     * @param  int  $options
724
     * @return string
725
     */
726
    public function toJson($options = 0)
727
    {
728 8
        return json_encode($this->jsonSerialize(), $options);
729
    }
730
731 8
    /**
732 4
     * Get a CachingIterator instance.
733 8
     *
734 2
     * @param  int  $flags
735 8
     * @return \CachingIterator
736 4
     */
737
    public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
738
    {
739 6
        return new CachingIterator($this->getIterator(), $flags);
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...
740 8
    }
741
742
    /**
743
     * Count the number of items in the collection using a given truth test.
744
     *
745
     * @param  callable|null  $callback
746
     * @return static
747
     */
748
    public function countBy($callback = null)
749 8
    {
750
        if (is_null($callback)) {
751 8
            $callback = function ($value) {
752
                return $value;
753
            };
754
        }
755
756
        return new static($this->groupBy($callback)->map(function ($value) {
0 ignored issues
show
Bug introduced by
It seems like groupBy() 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...
Unused Code introduced by
The call to EnumeratesValues::__construct() has too many arguments starting with $this->groupBy($callback...urn $value->count(); }).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
757
            return $value->count();
758
        }));
759
    }
760 2
761
    /**
762 2
     * Convert the collection to its string representation.
763
     *
764
     * @return string
765
     */
766
    public function __toString()
767
    {
768
        return $this->toJson();
769
    }
770
771 4
    /**
772
     * Add a method to the list of proxied methods.
773 4
     *
774
     * @param  string  $method
775 2
     * @return void
776 2
     */
777
    public static function proxy($method)
778
    {
779
        static::$proxies[] = $method;
780 4
    }
781 4
782
    /**
783
     * Dynamically access collection proxies.
784
     *
785
     * @param  string  $key
786
     * @return mixed
787
     *
788
     * @throws \Exception
789 6
     */
790
    public function __get($key)
791 6
    {
792
        if (! in_array($key, static::$proxies)) {
793
            throw new Exception("Property [{$key}] does not exist on this collection instance.");
794
        }
795
796
        return new HigherOrderCollectionProxy($this, $key);
797
    }
798
799
    /**
800 2
     * Results array of items from Collection or Arrayable.
801
     *
802 2
     * @param  mixed  $items
803 2
     * @return array
804
     */
805
    protected function getArrayableItems($items)
806
    {
807
        if (is_array($items)) {
808
            return $items;
809
        } elseif ($items instanceof Enumerable) {
810
            return $items->all();
811
        } elseif ($items instanceof Arrayable) {
812
            return $items->toArray();
813 33
        } elseif ($items instanceof Jsonable) {
814
            return json_decode($items->toJson(), true);
815 33
        } elseif ($items instanceof JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
816 2
            return (array) $items->jsonSerialize();
817
        } elseif ($items instanceof Traversable) {
818
            return iterator_to_array($items);
819 31
        }
820
821
        return (array) $items;
822
    }
823
824
    /**
825
     * Get an operator checker callback.
826
     *
827
     * @param  string  $key
828 497
     * @param  string  $operator
829
     * @param  mixed  $value
830 497
     * @return \Closure
831 487
     */
832 91
    protected function operatorForWhere($key, $operator = null, $value = null)
833 57
    {
834 36
        if (func_num_args() === 1) {
835 2
            $value = true;
836 36
837 2
            $operator = '=';
838 36
        }
839 2
840 34
        if (func_num_args() === 2) {
841 4
            $value = $operator;
842
843
            $operator = '=';
844 30
        }
845
846
        return function ($item) use ($key, $operator, $value) {
847
            $retrieved = data_get($item, $key);
848
849
            $strings = array_filter([$retrieved, $value], function ($value) {
850
                return is_string($value) || (is_object($value) && method_exists($value, '__toString'));
851
            });
852
853
            if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) {
854
                return in_array($operator, ['!=', '<>', '!==']);
855 26
            }
856
857 26
            switch ($operator) {
858 2
                default:
859
                case '=':
860 2
                case '==':  return $retrieved == $value;
861
                case '!=':
862
                case '<>':  return $retrieved != $value;
863 26
                case '<':   return $retrieved < $value;
864 12
                case '>':   return $retrieved > $value;
865
                case '<=':  return $retrieved <= $value;
866 12
                case '>=':  return $retrieved >= $value;
867
                case '===': return $retrieved === $value;
868
                case '!==': return $retrieved !== $value;
869
            }
870 26
        };
871
    }
872
873 26
    /**
874 26
     * Determine if the given value is callable, but not a string.
875
     *
876 26
     * @param  mixed  $value
877 2
     * @return bool
878
     */
879
    protected function useAsCallable($value)
880 26
    {
881
        return ! is_string($value) && is_callable($value);
882 26
    }
883 26
884 20
    /**
885 20
     * Get a value retrieving callback.
886 20
     *
887 20
     * @param  callable|string|null  $value
888 20
     * @return callable
889 20
     */
890 14
    protected function valueRetriever($value)
891 6
    {
892
        if ($this->useAsCallable($value)) {
893 26
            return $value;
894
        }
895
896
        return function ($item) use ($value) {
897
            return data_get($item, $value);
0 ignored issues
show
Documentation introduced by
$value is of type callable|null, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
898
        };
899
    }
900
}
901