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

AccessProtectedTrait::createProtectedCaller()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
crap 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
     *
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