PluckMutator::__invoke()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 11
nc 1
nop 2
crap 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