Completed
Push — master ( e2b114...270eeb )
by Freek
05:03
created

src/Macros/Extract.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
namespace Spatie\CollectionMacros\Macros;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Extract keys from a collection, like `only`, except:
9
 * - If a value doesn't exist, it returns null instead of omitting it
10
 * - It returns a collection without keys, so `list()` can be used.
11
 *
12
 * @mixin \Illuminate\Support\Collection
13
 *
14
 * @return \Illuminate\Support\Collection
15
 */
16
class Extract
17
{
18
    public function __invoke()
19
    {
20
        return function ($keys): Collection {
21
            $keys = is_array($keys) ? $keys : func_get_args();
22
23
            return array_reduce($keys, function ($extracted, $key) {
24
                return $extracted->push(
25
                    data_get($this->items, $key)
0 ignored issues
show
The property items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
                );
27
            }, new static());
28
        };
29
    }
30
}
31