AccessProtectedTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createProtectedCaller() 0 10 1
A callProtected() 0 8 2
A getProtected() 0 9 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Support;
4
5
use Closure;
6
7
trait AccessProtectedTrait
8
{
9
    /**
10
     * Create closure to call inaccessible method.
11
     *
12
     * @param $instance
13
     * @return \Closure
14
     */
15 384
    protected function createProtectedCaller($instance)
16
    {
17
        $closure = function ($method, $args) {
18 12
            $callable = [$this, $method];
19
20 12
            return call_user_func_array($callable, $args);
21 384
        };
22
23 384
        return $closure->bindTo($instance, $instance);
24
    }
25
26
    /**
27
     * Gets inaccessible property.
28
     *
29
     * @param $instance
30
     * @param $property
31
     * @return \Closure
32
     */
33 84
    protected function getProtected($instance, $property)
34
    {
35
        $closure = function ($property) {
36 84
            return $this->$property;
37 84
        };
38 84
        $callback = $closure->bindTo($instance, $instance);
39
40 84
        return $callback($property);
41
    }
42
43
    /**
44
     * Calls inaccessible method.
45
     *
46
     * @param object|\Closure $instance
47
     * @param $method
48
     * @param $args
49
     * @return mixed
50
     */
51 12
    protected function callProtected($instance, $method, $args = [])
52
    {
53 12
        if (! ($instance instanceof Closure)) {
54 12
            $instance = $this->createProtectedCaller($instance);
55
        }
56
57 12
        return call_user_func($instance, $method, $args);
58
    }
59
}
60