Completed
Pull Request — 2.x (#85)
by Akihito
01:54
created

AopTemplateConverter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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