Completed
Push — master ( a51ab2...247c5c )
by Antonio Carlos
03:22
created

HigherOrderCollectionProxy::__get()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Str\Support;
4
5
/**
6
 * @mixin \IlluminateAgnostic\Str\Support\Collection
7
 */
8
class HigherOrderCollectionProxy
9
{
10
    /**
11
     * The collection being operated on.
12
     *
13
     * @var \IlluminateAgnostic\Str\Support\Collection
14
     */
15
    protected $collection;
16
17
    /**
18
     * The method being proxied.
19
     *
20
     * @var string
21
     */
22
    protected $method;
23
24
    /**
25
     * Create a new proxy instance.
26
     *
27
     * @param  \IlluminateAgnostic\Str\Support\Collection  $collection
28
     * @param  string  $method
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31 5
    public function __construct(Collection $collection, $method)
32
    {
33 5
        $this->method = $method;
34 5
        $this->collection = $collection;
35 5
    }
36
37
    /**
38
     * Proxy accessing an attribute onto the collection items.
39
     *
40
     * @param  string  $key
41
     * @return mixed
42
     */
43
    public function __get($key)
44
    {
45 4
        return $this->collection->{$this->method}(function ($value) use ($key) {
46 4
            return is_array($value) ? $value[$key] : $value->{$key};
47 4
        });
48
    }
49
50
    /**
51
     * Proxy a method call onto the collection items.
52
     *
53
     * @param  string  $method
54
     * @param  array  $parameters
55
     * @return mixed
56
     */
57
    public function __call($method, $parameters)
58
    {
59 1
        return $this->collection->{$this->method}(function ($value) use ($method, $parameters) {
60 1
            return $value->{$method}(...$parameters);
61 1
        });
62
    }
63
}
64