Completed
Push — master ( de8dda...c73508 )
by Woody
02:25
created

PluckMutator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 18 5
1
<?php
2
3
namespace Underscore\Mutator;
4
5
use Underscore\Collection;
6
use Underscore\Mutator;
7
8
class PluckMutator extends Mutator
9
{
10
    /**
11
     * Serves as shorthand to get list of specific key value from every element
12
     *
13
     * If key not found returns null
14
     *
15
     * @param Collection $collection
16
     * @param string|int $key
17
     * @return Collection
18
     */
19
    public function __invoke($collection, $key)
20
    {
21 2
        $iterator = function ($value) use ($key) {
22 2
            if (is_object($value)) {
23 2
                if (is_callable([$value, $key])) {
24 1
                    return call_user_func([$value, $key]);
25
                } else {
26 1
                    return isset($value->{$key}) ? $value->{$key} : null;
27
                }
28
            } else {
29 1
                return isset($value[$key]) ? $value[$key] : null;
30
            }
31 2
        };
32
33 2
        $map = new MapMutator();
34
35 2
        return $map($collection, $iterator);
36
    }
37
}
38