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

HigherOrderCollectionProxy   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 6 2
A __call() 0 6 1
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