Completed
Push — master ( 28615a...652b7c )
by Alexander
25s queued 11s
created

AbstractMethodInvocation::__toString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3.1852

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 1
cts 3
cp 0.3333
rs 9.9666
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 3.1852
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;
18
use function array_pop;
19
use function count;
20
use function is_string;
21
22
/**
23
 * Abstract method invocation implementation
24
 */
25
abstract class AbstractMethodInvocation extends AbstractInvocation implements MethodInvocation
26
{
27
28
    /**
29
     * Instance of object for invoking
30
     *
31
     * @var object|null
32
     */
33
    protected $instance;
34
35
    /**
36
     * Instance of reflection method for class
37
     *
38
     * @var ReflectionMethod
39
     */
40
    protected $reflectionMethod;
41
42
    /**
43
     * Class name scope for static invocation
44
     *
45
     * @var string
46
     */
47
    protected $scope;
48
49
    /**
50
     * This static variable holds the name of field to use to avoid extra "if" section in the __invoke method
51 24
     *
52
     * Overridden in children classes and initialized via LSB
53 24
     *
54 24
     * @var string
55 24
     */
56
    protected static $propertyName;
57
58 24
    /**
59 4
     * Constructor for method invocation
60
     *
61 24
     * @param array $advices List of advices for this invocation
62
     */
63
    public function __construct(string $className, string $methodName, array $advices)
64
    {
65
        parent::__construct($advices);
66
        $this->reflectionMethod = $method = new AnnotatedReflectionMethod($className, $methodName);
67
68
        // Give an access to call protected method
69
        if ($method->isProtected()) {
70
            $method->setAccessible(true);
71
        }
72 20
    }
73
74 20
    /**
75 2
     * Invokes current method invocation with all interceptors
76
     *
77
     * @param null|object|string $instance Invocation instance (class name for static methods)
78 20
     * @param array $arguments List of arguments for method invocation
79
     * @param array $variadicArguments Additional list of variadic arguments
80
     *
81
     * @return mixed Result of invocation
82
     */
83 20
    final public function __invoke($instance = null, array $arguments = [], array $variadicArguments = [])
84
    {
85 20
        if ($this->level > 0) {
86 20
            $this->stackFrames[] = [$this->arguments, $this->scope, $this->instance, $this->current];
87 20
        }
88
89 20
        if (count($variadicArguments) > 0) {
90
            $arguments = array_merge($arguments, $variadicArguments);
91 20
        }
92
93 20
        try {
94 20
            ++$this->level;
95
96
            $this->current   = 0;
97
            $this->arguments = $arguments;
98
99
            $this->{static::$propertyName} = $instance;
100
101
            return $this->proceed();
102
        } finally {
103
            --$this->level;
104 3
105
            if ($this->level > 0) {
106 3
                [$this->arguments, $this->scope, $this->instance, $this->current] = array_pop($this->stackFrames);
107
            } else {
108
                $this->instance  = null;
109
                $this->arguments = [];
110
            }
111
        }
112
    }
113
114
    /**
115
     * Gets the method being called.
116
     *
117
     * @return ReflectionMethod|AnnotatedReflectionMethod the method being called.
118
     */
119
    public function getMethod(): ReflectionMethod
120
    {
121
        return $this->reflectionMethod;
122
    }
123
124
    /**
125 1
     * Returns friendly description of this joinpoint
126
     */
127 1
    final public function __toString(): string
128
    {
129
        return sprintf(
130
            'execution(%s%s%s())',
131
            $this->getScope(),
132
            $this->reflectionMethod->isStatic() ? '::' : '->',
133
            $this->reflectionMethod->name
134
        );
135
    }
136
}
137