Completed
Push — master ( 3a46ea...cc3bc4 )
by Freek
02:08
created

DefineArrayReplacer::leaveNode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Spatie\Php7to5\NodeVisitors;
4
5
use PhpParser\Node;
6
use PhpParser\NodeVisitorAbstract;
7
8
/*
9
 * Converts define() arrays into const arrays
10
 */
11
12
class DefineArrayReplacer extends NodeVisitorAbstract
13
{
14
    public function leaveNode(Node $node)
15
    {
16
        if (!$node instanceof Node\Expr\FuncCall) {
17
            return null;
18
        }
19
20
        if ($node->name != 'define') {
21
            return null;
22
        }
23
24
        $nameNode = $node->args[0]->value;
25
        $valueNode = $node->args[1]->value;
26
27
        if (! $valueNode instanceof Node\Expr\Array_) {
28
            return null;
29
        }
30
31
        $constNode = new Node\Const_($nameNode->value, $valueNode);
32
33
        return new Node\Stmt\Const_([$constNode]);
34
    }
35
36
}