Completed
Pull Request — master (#41)
by Lars
01:31
created

DefineArrayReplacer::afterTraverse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.4426
c 0
b 0
f 0
cc 7
nc 7
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
    /**
15
     * {@inheritdoc}
16
     */
17
    public function afterTraverse(array $nodes)
18
    {
19
        foreach ($nodes as &$node) {
20
21
            if (!$node instanceof Node\Stmt\Expression) {
22
                continue;
23
            }
24
25
            if (!$node->expr instanceof Node\Expr\FuncCall) {
26
                continue;
27
            }
28
29
            if (!isset($node->expr->name)) {
30
                continue;
31
            }
32
33
            if ($node->expr->name->parts[0] != 'define') {
34
                continue;
35
            }
36
37
            $nameNode = $node->expr->args[0]->value;
38
            $valueNode = $node->expr->args[1]->value;
39
40
            if (!$valueNode instanceof Node\Expr\Array_) {
41
                continue;
42
            }
43
44
            $constNode = new Node\Const_($nameNode->value, $valueNode);
45
46
            $node = new Node\Stmt\Const_([$constNode]);
47
        }
48
49
        return $nodes;
50
    }
51
}
52