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

DynamicClosureMethodInvocation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 42
ccs 8
cts 10
cp 0.8
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A proceed() 0 20 4
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