Completed
Push — master ( 6d0dd7...30241b )
by Sebastian
02:33
created

src/macros/firstOrFail.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Illuminate\Support\Collection;
4
use Spatie\CollectionMacros\Exceptions\CollectionItemNotFound;
5
6
/*
7
 * Get the first item. Throws CollectionItemNotFound if the item was not found.
8
 *
9
 * @return mixed
10
 */
11
Collection::macro('firstOrFail', function () {
12
    if (! is_null($item = $this->first())) {
0 ignored issues
show
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
13
        return collect($item);
14
    }
15
16
    throw new CollectionItemNotFound('No items found in collection.');
17
});
18