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

AopTemplateConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 48
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A enterNode() 0 11 3
A updateReflectiveMethodInvocation2ndParam() 0 10 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