Completed
Push — master ( 785bde...8b4d11 )
by Freek
01:29
created

Extract   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 15
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 2
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
 * @return \Illuminate\Support\Collection
13
 */
14
class Extract
15
{
16
    public function __invoke()
17
    {
18
        return function ($keys): Collection {
19
            $keys = is_array($keys) ? $keys : func_get_args();
20
21
            return array_reduce($keys, function ($extracted, $key) {
22
                return $extracted->push(
23
                    data_get($this->items, $key)
0 ignored issues
show
Bug introduced by
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...
24
                );
25
            }, new static());
26
        };
27
    }
28
}
29