Completed
Pull Request — master (#357)
by
unknown
01:58
created

DynamicClosureMethodInvocation::proceed()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
ccs 8
cts 10
cp 0.8
rs 9.2
cc 4
eloc 10
nc 5
nop 0
crap 4.128
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
/**
15
 * Dynamic closure method invocation is responsible to call dynamic methods via closure
16
 */
17
final class DynamicClosureMethodInvocation extends AbstractMethodInvocation
18
{
19
    /**
20
     * Closure to use
21
     *
22
     * @var \Closure
23
     */
24
    protected $closureToCall;
25
26
    /**
27
     * Previous instance of invocation
28
     *
29
     * @var null|object|string
30
     */
31
    protected $previousInstance;
32
33
    /**
34
     * Invokes original method and return result from it
35
     *
36
     * @return mixed
37
     */
38 7
    public function proceed()
39
    {
40 7
        if (isset($this->advices[$this->current])) {
41 1
            return ($this->advices[$this->current++])->invoke($this);
42
        }
43
44
        // Fill the closure only once if it's empty
45 7
        if ($this->closureToCall === null) {
46 7
            $this->closureToCall    = $this->reflectionMethod->getClosure($this->instance);
47 7
            $this->previousInstance = $this->instance;
48
        }
49
50
        // Rebind the closure if instance was changed since last time
51 7
        if ($this->previousInstance !== $this->instance) {
52
            $this->closureToCall    = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class);
53
            $this->previousInstance = $this->instance;
54
        }
55
56 7
        return ($this->closureToCall)(...$this->arguments);
57
    }
58
}
59