Completed
Pull Request — master (#253)
by Alexander
22:35
created

ReflectionFunctionInvocation::__invoke()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 25
ccs 0
cts 14
cp 0
rs 8.5806
cc 4
eloc 13
nc 8
nop 2
crap 20
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Aop\Framework;
12
13
use Go\Aop\Intercept\FunctionInvocation;
14
use Go\Aop\Intercept\Interceptor;
15
use ReflectionFunction;
16
17
/**
18
 * Function invocation implementation
19
 */
20
class ReflectionFunctionInvocation extends AbstractInvocation implements FunctionInvocation
21
{
22
23
    /**
24
     * Instance of reflection function
25
     *
26
     * @var null|ReflectionFunction
27
     */
28
    protected $reflectionFunction = null;
29
30
    /**
31
     * Constructor for function invocation
32
     *
33
     * @param string $functionName Function to invoke
34
     * @param $advices array List of advices for this invocation
35
     */
36
    public function __construct($functionName, array $advices)
37
    {
38
        parent::__construct($advices);
39
        $this->reflectionFunction = new ReflectionFunction($functionName);
40
    }
41
42
    /**
43
     * Invokes original function and return result from it
44
     *
45
     * @return mixed
46
     */
47
    public function proceed()
48
    {
49
        if (isset($this->advices[$this->current])) {
50
            /** @var $currentInterceptor Interceptor */
51
            $currentInterceptor = $this->advices[$this->current++];
52
53
            return $currentInterceptor->invoke($this);
54
        }
55
56
        return $this->reflectionFunction->invokeArgs($this->arguments);
57
    }
58
59
    /**
60
     * Gets the function being called.
61
     *
62
     * @return ReflectionFunction the method being called.
63
     */
64
    public function getFunction()
65
    {
66
        return $this->reflectionFunction;
67
    }
68
69
    /**
70
     * Returns the object that holds the current joinpoint's static
71
     * part.
72
     *
73
     * @return object|null the object (can be null if the accessible object is
74
     * static).
75
     */
76
    public function getThis()
77
    {
78
        return null;
79
    }
80
81
    /**
82
     * Returns the static part of this joinpoint.
83
     *
84
     * @return object
85
     */
86
    public function getStaticPart()
87
    {
88
        return $this->reflectionFunction;
89
    }
90
91
    /**
92
     * Invokes current function invocation with all interceptors
93
     *
94
     * @param array $arguments List of arguments for function invocation
95
     * @param array $variadicArguments Additional list of variadic arguments
96
     *
97
     * @return mixed Result of invocation
98
     */
99
    final public function __invoke(array $arguments = [], array $variadicArguments = [])
100
    {
101
        if ($this->level) {
102
            array_push($this->stackFrames, [$this->arguments, $this->current]);
103
        }
104
105
        if (!empty($variadicArguments)) {
106
            $arguments = array_merge($arguments, $variadicArguments);
107
        }
108
109
        ++$this->level;
110
111
        $this->current   = 0;
112
        $this->arguments = $arguments;
113
114
        $result = $this->proceed();
115
116
        --$this->level;
117
118
        if ($this->level) {
119
            list($this->arguments, $this->current) = array_pop($this->stackFrames);
120
        }
121
122
        return $result;
123
    }
124
125
    /**
126
     * Returns a friendly description of current joinpoint
127
     *
128
     * @return string
129
     */
130
    final public function __toString()
131
    {
132
        return sprintf(
133
            "execution(%s())",
134
            $this->reflectionFunction->getName()
135
        );
136
    }
137
}
138