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 |
||
19 | class ASTVisitor implements \PhpParser\NodeVisitor { |
||
20 | /** |
||
21 | * @var LocationImpl |
||
22 | */ |
||
23 | protected $location; |
||
24 | |||
25 | /** |
||
26 | * @var Insert |
||
27 | */ |
||
28 | protected $insert; |
||
29 | |||
30 | /** |
||
31 | * @var array string => array() |
||
32 | */ |
||
33 | protected $listeners_enter_definition; |
||
34 | |||
35 | /** |
||
36 | * @var array string => array() |
||
37 | */ |
||
38 | protected $listeners_enter_misc; |
||
39 | |||
40 | /** |
||
41 | * This is used to exit early in enterNode and to break enterNode apart |
||
42 | * into many methods. |
||
43 | * |
||
44 | * @var array<string,string> |
||
45 | */ |
||
46 | protected $jump_labels; |
||
47 | |||
48 | 53 | public function __construct(LocationImpl $location, Insert $insert, array &$listeners_enter_definition, array &$listeners_enter_misc) { |
|
62 | |||
63 | // generalizes over calls to misc listeners |
||
64 | |||
65 | /** |
||
66 | * @param string $which |
||
67 | */ |
||
68 | 38 | View Code Duplication | protected function call_misc_listener($which) { |
81 | |||
82 | /** |
||
83 | * @param string $which |
||
84 | * @param string $type |
||
85 | * @param int $type |
||
86 | */ |
||
87 | 51 | View Code Duplication | protected function call_definition_listener($which, $type, $id) { |
99 | |||
100 | // from \PhpParser\NodeVisitor |
||
101 | |||
102 | /** |
||
103 | * @inheritdoc |
||
104 | */ |
||
105 | 53 | public function beforeTraverse(array $nodes) { |
|
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | 53 | public function afterTraverse(array $nodes) { |
|
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 53 | public function enterNode(\PhpParser\Node $node) { |
|
140 | |||
141 | 7 | public function enterNamespace(N\Stmt\Namespace_ $node, $start_line, $end_line) { |
|
147 | |||
148 | 44 | View Code Duplication | public function enterClass(N\Stmt\Class_ $node, $start_line, $end_line) { |
158 | |||
159 | 3 | View Code Duplication | public function enterInterface(N\Stmt\Interface_ $node, $start_line, $end_line) { |
169 | |||
170 | 3 | View Code Duplication | public function enterTrait(N\Stmt\Trait_ $node, $start_line, $end_line) { |
180 | |||
181 | 38 | public function enterMethod(N\Stmt\ClassMethod $node, $start_line, $end_line) { |
|
191 | |||
192 | 4 | public function enterFunction(N\Stmt\Function_ $node, $start_line, $end_line) { |
|
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | 53 | public function leaveNode(\PhpParser\Node $node) { |
|
215 | } |
||
216 |
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..