Completed
Push — master ( 71ec50...18d3f1 )
by Albert
02:10
created

AccessProtectedTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createProtectedCaller() 0 8 1
A getProtected() 0 8 1
A callProtected() 0 8 2
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
     *
14
     * @return Closure
15
     */
16 22
    protected function createProtectedCaller($instance)
17
    {
18
        return Closure::bind(function ($method, $args) {
19 5
            $callable = array($this, $method);
20
21 5
            return call_user_func_array($callable, $args);
22 22
        }, $instance, $instance);
23
    }
24
25
    /**
26
     * Gets inaccessible property.
27
     *
28
     * @param $instance
29
     * @param $property
30
     *
31
     * @return Closure
32
     */
33
    protected function getProtected($instance, $property)
34
    {
35 7
        $closure = Closure::bind(function ($property) {
36 7
            return $this->$property;
37 7
        }, $instance, $instance);
38
39 7
        return $closure($property);
40
    }
41
42
    /**
43
     * Calls inaccessible method.
44
     *
45
     * @param object|Closure $instance
46
     * @param $method
47
     * @param $args
48
     *
49
     * @return mixed
50
     */
51 1
    protected function callProtected($instance, $method, $args = [])
52
    {
53 1
        if (! ($instance instanceof Closure)) {
54 1
            $instance = $this->createProtectedCaller($instance);
55 1
        }
56
57 1
        return call_user_func($instance, $method, $args);
58
    }
59
}
60