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

StaticClosureMethodInvocation::getThis()   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 2015, 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 Closure;
15
16
/**
17
 * Static closure method invocation is responsible to call static methods via closure
18
 */
19
final class StaticClosureMethodInvocation extends AbstractMethodInvocation
20
{
21
    /**
22
     * Closure to use
23
     *
24
     * @var Closure
25
     */
26
    protected $closureToCall;
27
28
    /**
29
     * Previous scope of invocation
30
     *
31
     * @var null|string
32
     */
33
    protected $previousScope;
34
35
    /**
36
     * For static calls we store given argument as 'scope' property
37
     */
38 13
    protected static $propertyName = 'scope';
39
40 13
    /**
41 1
     * Proceeds all registered advices for the static method and returns an invocation result
42
     */
43 1
    public function proceed()
44
    {
45
        if (isset($this->advices[$this->current])) {
46
            $currentInterceptor = $this->advices[$this->current++];
47 13
48 13
            return $currentInterceptor->invoke($this);
49 13
        }
50
51 13
        // Rebind the closure if scope (class name) was changed since last time
52 13
        if ($this->previousScope !== $this->scope) {
53
            if ($this->closureToCall === null) {
54
                $this->closureToCall = static::getStaticInvoker($this->reflectionMethod->class, $this->reflectionMethod->name);
55 13
            }
56
            $this->closureToCall = $this->closureToCall->bindTo(null, $this->scope);
57
            $this->previousScope = $this->scope;
58
        }
59
60
        return ($this->closureToCall)($this->arguments);
61
62 13
    }
63
64
    /**
65 13
     * Returns static method invoker for the concrete method in the class
66 13
     */
67
    protected static function getStaticInvoker(string $className, string $methodName): Closure
68
    {
69
        return function (array $args) use ($className, $methodName) {
70
            return forward_static_call_array([$className, $methodName], $args);
71
        };
72
    }
73
74
    /**
75
     * Checks if the current joinpoint is dynamic or static
76
     *
77
     * Dynamic joinpoint contains a reference to an object that can be received via getThis() method call
78
     *
79
     * @see ClassJoinpoint::getThis()
80
     */
81
    final public function isDynamic(): bool
82
    {
83
        return false;
84
    }
85
86
    /**
87
     * Returns the object for which current joinpoint is invoked
88
     *
89
     * @return object Instance of object or null for static call/unavailable context
90
     */
91
    final public function getThis(): ?object
92
    {
93
        return null;
94
    }
95
96
    /**
97
     * Returns the static scope name (class name) of this joinpoint.
98
     */
99
    final public function getScope(): string
100
    {
101
        // $this->scope contains the current class scope that was received via static::class
102
        return $this->scope;
103
    }
104
}
105