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

AopTemplateConverter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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