Completed
Pull Request — master (#429)
by Alexander
24:00
created

AbstractMethodInvocation::getStaticPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types = 1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2011, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Aop\Framework;
13
14
use Go\Aop\Intercept\MethodInvocation;
15
use Go\Aop\Support\AnnotatedReflectionMethod;
16
use ReflectionMethod;
17
use function array_merge, array_pop;
18
19
/**
20
 * Abstract method invocation implementation
21
 */
22
abstract class AbstractMethodInvocation extends AbstractInvocation implements MethodInvocation
23
{
24
25
    /**
26
     * Instance of object for invoking or class name for static call
27
     *
28
     * @var object|string
29
     */
30
    protected $instance;
31
32
    /**
33
     * Instance of reflection method for class
34
     *
35
     * @var ReflectionMethod
36
     */
37
    protected $reflectionMethod;
38
39
    /**
40
     * Name of the invocation class
41
     *
42
     * @var string
43
     */
44
    protected $className = '';
45
46
    /**
47
     * Constructor for method invocation
48
     *
49
     * @param array $advices List of advices for this invocation
50
     */
51 24
    public function __construct(string $className, string $methodName, array $advices)
52
    {
53 24
        parent::__construct($advices);
54 24
        $this->className        = $className;
55 24
        $this->reflectionMethod = $method = new AnnotatedReflectionMethod($this->className, $methodName);
56
57
        // Give an access to call protected method
58 24
        if ($method->isProtected()) {
59 4
            $method->setAccessible(true);
60
        }
61 24
    }
62
63
    /**
64
     * Invokes current method invocation with all interceptors
65
     *
66
     * @param null|object|string $instance Invocation instance (class name for static methods)
67
     * @param array $arguments List of arguments for method invocation
68
     * @param array $variadicArguments Additional list of variadic arguments
69
     *
70
     * @return mixed Result of invocation
71
     */
72 20
    final public function __invoke($instance = null, array $arguments = [], array $variadicArguments = [])
73
    {
74 20
        if ($this->level > 0) {
75 2
            $this->stackFrames[] = [$this->arguments, $this->instance, $this->current];
76
        }
77
78 20
        if (!empty($variadicArguments)) {
79
            $arguments = array_merge($arguments, $variadicArguments);
80
        }
81
82
        try {
83 20
            ++$this->level;
84
85 20
            $this->current   = 0;
86 20
            $this->instance  = $instance;
87 20
            $this->arguments = $arguments;
88
89 20
            return $this->proceed();
90
        } finally {
91 20
            --$this->level;
92
93 20
            if ($this->level > 0) {
94 20
                [$this->arguments, $this->instance, $this->current] = array_pop($this->stackFrames);
95
            } else {
96
                $this->instance  = null;
97
                $this->arguments = [];
98
            }
99
        }
100
    }
101
102
    /**
103
     * Gets the method being called.
104 3
     *
105
     * @return ReflectionMethod|AnnotatedReflectionMethod the method being called.
106 3
     */
107
    public function getMethod(): ReflectionMethod
108
    {
109
        return $this->reflectionMethod;
110
    }
111
112
    /**
113
     * Returns friendly description of this joinpoint
114
     */
115
    final public function __toString(): string
116
    {
117
        return sprintf(
118
            'execution(%s%s%s())',
119
            $this->getScope(),
120
            $this->reflectionMethod->isStatic() ? '::' : '->',
121
            $this->reflectionMethod->name
122
        );
123
    }
124
}
125