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 |
||
| 20 | class Indexer implements Location, ListenerRegistry, \PhpParser\NodeVisitor { |
||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $project_root_path; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var I\Insert|null |
||
| 28 | */ |
||
| 29 | protected $insert; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \PhpParser\Parser |
||
| 33 | */ |
||
| 34 | protected $parser; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array string => array() |
||
| 38 | */ |
||
| 39 | protected $listeners_enter_entity; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array string => array() |
||
| 43 | */ |
||
| 44 | protected $listeners_leave_entity; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array string => array() |
||
| 48 | */ |
||
| 49 | protected $listeners_enter_misc; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array string => array() |
||
| 53 | */ |
||
| 54 | protected $listeners_leave_misc; |
||
| 55 | |||
| 56 | // state for parsing a file |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string|null |
||
| 60 | */ |
||
| 61 | protected $file_path = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string[]|null |
||
| 65 | */ |
||
| 66 | protected $file_content = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * This contains the stack of ids were currently in, i.e. the nesting of |
||
| 70 | * known code blocks we are in. |
||
| 71 | * |
||
| 72 | * @var array|null contain ($entity_type, $entity_id) |
||
| 73 | */ |
||
| 74 | protected $entity_stack = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $project_root_path |
||
| 78 | */ |
||
| 79 | 24 | public function __construct(\PhpParser\Parser $parser, $project_root_path, Insert $insert) { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $path |
||
| 100 | */ |
||
| 101 | 23 | public function index_file($path) { |
|
| 120 | |||
| 121 | // helper |
||
| 122 | |||
| 123 | 23 | private function lines_from_to($start, $end) { |
|
| 128 | |||
| 129 | // from ListenerRegistry |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @inheritdoc |
||
| 133 | */ |
||
| 134 | 1 | public function on_enter_entity($types, \Closure $listener) { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @inheritdoc |
||
| 141 | */ |
||
| 142 | 1 | public function on_leave_entity($types, \Closure $listener) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | 23 | public function on_enter_misc($classes, \Closure $listener) { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @inheritdoc |
||
| 157 | */ |
||
| 158 | 1 | public function on_leave_misc($classes, \Closure $listener) { |
|
| 162 | |||
| 163 | // generalizes over over on_enter/leave_xx |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $what |
||
| 167 | * @param array|null $things |
||
| 168 | */ |
||
| 169 | 23 | protected function on_enter_or_leave_something($what, $things, \Closure $listener) { |
|
| 184 | |||
| 185 | // generalizes over calls to misc listeners |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $which |
||
| 189 | * @param \PhpParser\Node $node |
||
| 190 | */ |
||
| 191 | 23 | View Code Duplication | protected function call_misc_listener($which, \PhpParser\Node $node) { |
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $which |
||
| 206 | * @param string $type |
||
| 207 | * @param int $type |
||
| 208 | * @param \PhpParser\Node|null $node |
||
| 209 | */ |
||
| 210 | 23 | View Code Duplication | protected function call_entity_listener($which, $type, $id, \PhpParser\Node $node = null) { |
| 221 | |||
| 222 | // from Location |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritdoc |
||
| 226 | */ |
||
| 227 | 17 | public function file_path() { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritdoc |
||
| 233 | */ |
||
| 234 | public function file_content($from_line = null, $to_line = null) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @inheritdoc |
||
| 247 | */ |
||
| 248 | 17 | public function in_entities() { |
|
| 251 | |||
| 252 | // from \PhpParser\NodeVisitor |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritdoc |
||
| 256 | */ |
||
| 257 | 23 | public function beforeTraverse(array $nodes) { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @inheritdoc |
||
| 278 | */ |
||
| 279 | 23 | public function afterTraverse(array $nodes) { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @inheritdoc |
||
| 289 | */ |
||
| 290 | 23 | public function enterNode(\PhpParser\Node $node) { |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @inheritdoc |
||
| 327 | */ |
||
| 328 | 23 | public function leaveNode(\PhpParser\Node $node) { |
|
| 340 | } |
||
| 341 |
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.