Completed
Pull Request — 2.x (#85)
by Akihito
02:08
created

AopTemplateConverter::enterNode()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 8
nc 3
nop 1
crap 5
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the Ray.Aop package.
6
 *
7
 * @license http://opensource.org/licenses/MIT MIT
8
 */
9
namespace Ray\Aop;
10
11
use PhpParser\Node;
12
use PhpParser\Node\Arg;
13
use PhpParser\Node\Expr\ArrayItem;
14
use PhpParser\Node\Expr\MethodCall;
15
use PhpParser\Node\Expr\StaticCall;
16
use PhpParser\Node\Expr\Variable;
17
18
final class AopTemplateConverter extends \PhpParser\NodeVisitorAbstract
19
{
20
    /**
21
     * @var string
22
     */
23
    private $method;
24
25
    /**
26
     * @var Arg[]
27
     */
28
    private $args = [];
29
30
    private $proceedArg;
31
32 13
    public function __construct(\ReflectionMethod $method)
33
    {
34 13
        $this->method = $method->name;
35 13
        $proceedArg = [];
36 13
        foreach ($method->getParameters() as $parameter) {
37 11
            $this->args[] = new Arg(new Variable($parameter->name));
38 11
            $proceedArg[] = new ArrayItem(new Variable($parameter->name));
39
        }
40 13
        $this->proceedArg = new Arg(new Node\Expr\Array_($proceedArg));
41 13
    }
42
43 13
    public function enterNode(Node $node)
44
    {
45 13
        if ($node instanceof StaticCall && $node->name === 'templateMethod') {
46 13
            $node->name = $this->method;
47 13
            $node->args = $this->args;
48
49 13
            return $node;
50
        }
51
        // change ReflectiveMethodInvocation 2nd parameter
52 13
        if ($node instanceof MethodCall && $node->name === 'proceed') {
53 13
            $node->var->args[2] = $this->proceedArg;
54
55 13
            return $node;
56
        }
57 13
    }
58
}
59