Completed
Pull Request — 1.x (#286)
by Alexander
02:43
created

ReflectionFunctionInvocation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 113
ccs 0
cts 47
cp 0
rs 10
c 1
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A proceed() 0 11 2
A getFunction() 0 4 1
A getThis() 0 4 1
A getStaticPart() 0 4 1
A __invoke() 0 21 3
A __toString() 0 7 1
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 Arguments for the invocation
95
     *
96
     * @return mixed
97
     */
98
    final public function __invoke(array $arguments = [])
99
    {
100
        if ($this->level) {
101
            array_push($this->stackFrames, array($this->arguments, $this->current));
102
        }
103
104
        ++$this->level;
105
106
        $this->current   = 0;
107
        $this->arguments = $arguments;
108
109
        $result = $this->proceed();
110
111
        --$this->level;
112
113
        if ($this->level) {
114
            list($this->arguments, $this->current) = array_pop($this->stackFrames);
115
        }
116
117
        return $result;
118
    }
119
120
    /**
121
     * Returns a friendly description of current joinpoint
122
     *
123
     * @return string
124
     */
125
    final public function __toString()
126
    {
127
        return sprintf(
128
            "execution(%s())",
129
            $this->reflectionFunction->getName()
130
        );
131
    }
132
}
133