Completed
Push — master ( 0086da...95aa9c )
by Oskar
22:17 queued 15:41
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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A traverseNode() 4 4 1
A traverseArray() 4 4 1

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
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
$traverser is of type object<PhpParser\NodeTraverser>, but the property $traverser was declared to be of type object<Hal\Component\Ast\NodeTraverser>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
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 View Code Duplication
    class NodeTraverser extends Mother
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
100
    {
101
        /** @var Traverser */
102
        private $traverser;
103
104
        public function __construct($cloneNodes = false, $stopCondition = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cloneNodes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
        {
106
            parent::__construct();
107
            $this->traverser = new Traverser($this, $stopCondition);
108
        }
109
110
        public function traverseNode(Node $node): Node
111
        {
112
            return parent::traverseNode($node);
113
        }
114
115
        protected function traverseArray(array $nodes): array
116
        {
117
            return $this->traverser->traverseArray($nodes, $this->visitors);
118
        }
119
    }
120
} else {
121 View Code Duplication
    class NodeTraverser extends Mother
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Hal\Component\Ast\NodeTraverser has been defined more than once; this definition is ignored, only the first definition in this file (L99-119) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
122
    {
123
        /** @var Traverser */
124
        private $traverser;
125
126
        public function __construct($cloneNodes = false, $stopCondition = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cloneNodes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
        {
128
            parent::__construct();
129
            $this->traverser = new Traverser($this, $stopCondition);
130
        }
131
132
        public function traverseNode(Node $node)
133
        {
134
            return parent::traverseNode($node);
135
        }
136
137
        protected function traverseArray(array $nodes)
138
        {
139
            return $this->traverser->traverseArray($nodes, $this->visitors);
140
        }
141
    }
142
}