Completed
Push — 2.x ( f7c839...266f66 )
by Akihito
01:45
created

updateReflectiveMethodInvocation2ndParam()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
crap 3
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
52 13
        return $this->updateReflectiveMethodInvocation2ndParam($node);
53
    }
54
55 13
    private function updateReflectiveMethodInvocation2ndParam(Node $node) : Node
56
    {
57 13
        if ($node instanceof MethodCall && $node->name === 'proceed') {
58 13
            $node->var->args[2] = $this->proceedArg;
59
60 13
            return $node;
61
        }
62
63 13
        return $node;
64
    }
65
}
66