Getter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 24
ccs 5
cts 5
cp 1
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 3 1
1
<?php
2
3
namespace Bdf\Collection\Util\Functor\Transformer;
4
5
/**
6
 * Call a getter on the element
7
 *
8
 * (i) This transformer do not add any prefix on the getter.
9
 *     If the method name is getName(), you must instantiate with `new Getter('getName')`
10
 *
11
 * <code>
12
 * $persons->stream()
13
 *     ->map(new Getter('name')) // Call method name() on each element, and return the result
14
 *     ->toArray()
15
 * ;
16
 * </code>
17
 */
18
final class Getter implements TransformerInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
26
    /**
27
     * Getter constructor.
28
     *
29
     * @param string $name The method name
30
     */
31 7
    public function __construct(string $name)
32
    {
33 7
        $this->name = $name;
34 7
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 7
    public function __invoke($element, $key = null)
40
    {
41 7
        return $element->{$this->name}();
42
    }
43
}
44