Completed
Push — cs ( 9c9fc7 )
by Akihito
01:43
created

AopTemplateConverter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 48
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A enterNode() 0 11 4
A updateReflectiveMethodInvocation2ndParam() 0 10 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use PhpParser\Node;
8
use PhpParser\Node\Arg;
9
use PhpParser\Node\Expr\ArrayItem;
10
use PhpParser\Node\Expr\MethodCall;
11
use PhpParser\Node\Expr\New_;
12
use PhpParser\Node\Expr\StaticCall;
13
use PhpParser\Node\Expr\Variable;
14
use PhpParser\Node\Identifier;
15
use PhpParser\NodeVisitorAbstract;
16
17
final class AopTemplateConverter extends NodeVisitorAbstract
18
{
19
    /**
20
     * @var string
21
     */
22
    private $method;
23
24
    /**
25
     * @var Arg[]
26
     */
27
    private $args = [];
28
29
    private $proceedArg;
30
31
    public function __construct(\ReflectionMethod $method)
32
    {
33
        $this->method = $method->name;
34
        $proceedArg = [];
35
        foreach ($method->getParameters() as $parameter) {
36
            $this->args[] = new Arg(new Variable($parameter->name));
37
            $proceedArg[] = new ArrayItem(new Variable($parameter->name));
38
        }
39
        $this->proceedArg = new Arg(new Node\Expr\Array_($proceedArg));
40
    }
41
42
    public function enterNode(Node $node)
43
    {
44
        if ($node instanceof StaticCall && $node->name instanceof Identifier && $node->name->name === 'templateMethod') {
45
            $node->name = new Identifier($this->method);
46
            $node->args = $this->args;
47
48
            return $node;
49
        }
50
51
        return $this->updateReflectiveMethodInvocation2ndParam($node);
52
    }
53
54
    private function updateReflectiveMethodInvocation2ndParam(Node $node) : Node
55
    {
56
        if ($node instanceof MethodCall && $node->name instanceof Identifier && $node->name->name === 'proceed' && $node->var instanceof New_) {
57
            $node->var->args[2] = $this->proceedArg;
58
59
            return $node;
60
        }
61
62
        return $node;
63
    }
64
}
65