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 |
||
| 25 | class Indexer implements ListenerRegistry, \PhpParser\NodeVisitor { |
||
| 26 | /** |
||
| 27 | * @var Log |
||
| 28 | */ |
||
| 29 | protected $log; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Insert |
||
| 33 | */ |
||
| 34 | protected $insert; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \PhpParser\Parser |
||
| 38 | */ |
||
| 39 | protected $parser; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array string => array() |
||
| 43 | */ |
||
| 44 | protected $listeners_enter_definition; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array string => array() |
||
| 48 | */ |
||
| 49 | protected $listeners_enter_misc; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var LocationImpl|null |
||
| 53 | */ |
||
| 54 | protected $location = null; |
||
| 55 | |||
| 56 | 42 | public function __construct(Log $log, \PhpParser\Parser $parser, Insert $insert) { |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Index a directory. |
||
| 70 | * |
||
| 71 | * @param string $path |
||
| 72 | * @param array $ignore_paths |
||
| 73 | * @return null |
||
| 74 | */ |
||
| 75 | 7 | public function index_directory($path, array $ignore_paths) { |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Initialize the filesystem abstraction. |
||
| 99 | * |
||
| 100 | * @return Flightcontrol |
||
| 101 | */ |
||
| 102 | 7 | public function init_flightcontrol($path) { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $base_dir |
||
| 110 | * @param string $path |
||
| 111 | * @return null |
||
| 112 | */ |
||
| 113 | public function index_file($base_dir, $path) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $path |
||
| 127 | * @param string $content |
||
| 128 | * @return null |
||
| 129 | */ |
||
| 130 | 42 | public function index_content($path, $content) { |
|
| 146 | |||
| 147 | // from ListenerRegistry |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | public function on_enter_definition($types, \Closure $listener) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | 25 | public function on_enter_misc($classes, \Closure $listener) { |
|
| 164 | |||
| 165 | // generalizes over over on_enter |
||
| 166 | |||
| 167 | /** |
||
| 168 | * TODO: Maybe remove this in favour of duplicates. |
||
| 169 | * |
||
| 170 | * @param string $what |
||
| 171 | * @param array|null $things |
||
| 172 | */ |
||
| 173 | 25 | protected function on_enter_something($what, $things, \Closure $listener) { |
|
| 188 | |||
| 189 | // generalizes over calls to misc listeners |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $which |
||
| 193 | * @param \PhpParser\Node $node |
||
|
|
|||
| 194 | */ |
||
| 195 | 31 | View Code Duplication | protected function call_misc_listener($which) { |
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $which |
||
| 211 | * @param string $type |
||
| 212 | * @param int $type |
||
| 213 | * @param \PhpParser\Node|null $node |
||
| 214 | */ |
||
| 215 | 40 | View Code Duplication | protected function call_definition_listener($which, $type, $id) { |
| 227 | |||
| 228 | // from \PhpParser\NodeVisitor |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @inheritdoc |
||
| 232 | */ |
||
| 233 | 42 | public function beforeTraverse(array $nodes) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @inheritdoc |
||
| 240 | */ |
||
| 241 | 42 | public function afterTraverse(array $nodes) { |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritdoc |
||
| 246 | */ |
||
| 247 | 42 | public function enterNode(\PhpParser\Node $node) { |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @inheritdoc |
||
| 313 | */ |
||
| 314 | 42 | public function leaveNode(\PhpParser\Node $node) { |
|
| 324 | } |
||
| 325 |
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.