Completed
Push — master ( a74f2f...6e3ebc )
by Oskar
14s
created

NodeTraverser   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 99.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Ast;
11
12
use PhpParser\Node;
13
use PhpParser\NodeTraverser as Mother;
14
use PhpParser\NodeVisitor;
15
16
/**
17
 * Custom Ast Traverser
18
 *
19
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
20
 * @internal
21
 */
22
class Traverser
23
{
24
    protected $stopCondition;
25
26
    /** @var NodeTraverser */
27
    private $traverser;
28
29
    public function __construct(Mother $traverser, $stopCondition = null)
30
    {
31
        if(null === $stopCondition) {
32
            $stopCondition = function($node) {
33
                if ($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) {
34
                    return false;
35
                }
36
37
                return true;
38
            };
39
        }
40
41
        $this->stopCondition = $stopCondition;
42
        $this->traverser = $traverser;
0 ignored issues
show
Documentation Bug introduced by
It seems like $traverser of type object<PhpParser\NodeTraverser> is incompatible with the declared type object<Hal\Component\Ast\NodeTraverser> of property $traverser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    /**
46
     * @param array $nodes
47
     * @param array|NodeVisitor[] $visitors
48
     * @return array
49
     */
50
    public function traverseArray(array $nodes, array $visitors) {
51
        $doNodes = array();
52
53
        foreach ($nodes as $i => &$node) {
54
            if (is_array($node)) {
55
                $node = $this->traverseArray($node, $visitors);
56
            } elseif ($node instanceof Node) {
57
                $traverseChildren = call_user_func($this->stopCondition, $node);
58
59
                foreach ($visitors as $visitor) {
60
                    $return = $visitor->enterNode($node);
61
                    if (Mother::DONT_TRAVERSE_CHILDREN === $return) {
62
                        $traverseChildren = false;
63
                    } else if (null !== $return) {
64
                        $node = $return;
65
                    }
66
                }
67
68
                if ($traverseChildren) {
69
                    $node = $this->traverser->traverseNode($node);
70
                }
71
72
                foreach ($visitors as $visitor) {
73
                    $return = $visitor->leaveNode($node);
74
75
                    if (Mother::REMOVE_NODE === $return) {
76
                        $doNodes[] = array($i, array());
77
                        break;
78
                    } elseif (is_array($return)) {
79
                        $doNodes[] = array($i, $return);
80
                        break;
81
                    } elseif (null !== $return) {
82
                        $node = $return;
83
                    }
84
                }
85
            }
86
        }
87
88
        if (!empty($doNodes)) {
89
            while (list($i, $replace) = array_pop($doNodes)) {
90
                array_splice($nodes, $i, 1, $replace);
91
            }
92
        }
93
94
        return $nodes;
95
    }
96
}
97
98
if (PHP_VERSION_ID >= 70000) {
99
    class_alias(Php7NodeTraverser::class, __NAMESPACE__ . '\\NodeTraverser');
100
} else {
101
    class_alias(Php5NodeTraverser::class, __NAMESPACE__ . '\\NodeTraverser');
102
}
103