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 | * @param Schema[] $rule_schemas |
||
|
|
|||
| 79 | */ |
||
| 80 | 24 | public function __construct(\PhpParser\Parser $parser, $project_root_path, Insert $insert) { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $path |
||
| 101 | */ |
||
| 102 | 23 | public function index_file($path) { |
|
| 129 | |||
| 130 | // helper |
||
| 131 | |||
| 132 | 23 | private function lines_from_to($start, $end) { |
|
| 137 | |||
| 138 | // from ListenerRegistry |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritdoc |
||
| 142 | */ |
||
| 143 | 1 | public function on_enter_entity($types, \Closure $listener) { |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | 1 | public function on_leave_entity($types, \Closure $listener) { |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | 23 | public function on_enter_misc($classes, \Closure $listener) { |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @inheritdoc |
||
| 166 | */ |
||
| 167 | 1 | public function on_leave_misc($classes, \Closure $listener) { |
|
| 171 | |||
| 172 | // generalizes over over on_enter/leave_xx |
||
| 173 | 23 | protected function on_enter_or_leave_something($what, $things, \Closure $listener) { |
|
| 188 | |||
| 189 | // generalizes over calls to misc listeners |
||
| 190 | 23 | View Code Duplication | protected function call_misc_listener($which, $node) { |
| 202 | |||
| 203 | 23 | View Code Duplication | protected function call_entity_listener($which, $type, $id, $node) { |
| 214 | |||
| 215 | // from Location |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @inheritdoc |
||
| 219 | */ |
||
| 220 | 17 | public function file_path() { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritdoc |
||
| 226 | */ |
||
| 227 | public function file_content($from_line = null, $to_line = null) { |
||
| 228 | if ($from_line !== null) { |
||
| 229 | assert('$to_line !== null'); |
||
| 230 | return $this->lines_from_to($from_line, $to_line); |
||
| 231 | } |
||
| 232 | else { |
||
| 233 | assert('$to_line === null'); |
||
| 234 | return implode("\n", $this->file_content); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @inheritdoc |
||
| 240 | */ |
||
| 241 | 17 | public function in_entities() { |
|
| 244 | |||
| 245 | // from \PhpParser\NodeVisitor |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @inheritdoc |
||
| 249 | */ |
||
| 250 | 23 | public function beforeTraverse(array $nodes) { |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @inheritdoc |
||
| 271 | */ |
||
| 272 | 23 | public function afterTraverse(array $nodes) { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @inheritdoc |
||
| 282 | */ |
||
| 283 | 23 | public function enterNode(\PhpParser\Node $node) { |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @inheritdoc |
||
| 338 | */ |
||
| 339 | 23 | public function leaveNode(\PhpParser\Node $node) { |
|
| 351 | } |
||
| 352 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.