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:
Complex classes like CompletionHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CompletionHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class CompletionHandler |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Application to complete for |
||
| 17 | * @var \Symfony\Component\Console\Application |
||
| 18 | */ |
||
| 19 | protected $application; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Command |
||
| 23 | */ |
||
| 24 | protected $command; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var CompletionContext |
||
| 28 | */ |
||
| 29 | protected $context; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Array of completion helpers. |
||
| 33 | * @var CompletionInterface[] |
||
| 34 | */ |
||
| 35 | protected $helpers = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Index the command name was detected at |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | private $commandWordIndex; |
||
| 42 | |||
| 43 | public function __construct(Application $application, CompletionContext $context = null) |
||
| 67 | |||
| 68 | public function setContext(CompletionContext $context) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return CompletionContext |
||
| 75 | */ |
||
| 76 | public function getContext() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param CompletionInterface[] $array |
||
| 83 | */ |
||
| 84 | public function addHandlers(array $array) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param CompletionInterface $helper |
||
| 91 | */ |
||
| 92 | public function addHandler(CompletionInterface $helper) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Do the actual completion, returning an array of strings to provide to the parent shell's completion system |
||
| 99 | * |
||
| 100 | * @throws \RuntimeException |
||
| 101 | * @return string[] |
||
| 102 | */ |
||
| 103 | public function runCompletion() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get an InputInterface representation of the completion context |
||
| 135 | * |
||
| 136 | * @deprecated Incorrectly uses the ArrayInput API and is no longer needed. |
||
| 137 | * This will be removed in the next major version. |
||
| 138 | * |
||
| 139 | * @return ArrayInput |
||
| 140 | */ |
||
| 141 | public function getInput() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Attempt to complete the current word as a long-form option (--my-option) |
||
| 153 | * |
||
| 154 | * @return array|false |
||
| 155 | */ |
||
| 156 | protected function completeForOptions() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Attempt to complete the current word as an option shortcut. |
||
| 175 | * |
||
| 176 | * If the shortcut exists it will be completed, but a list of possible shortcuts is never returned for completion. |
||
| 177 | * |
||
| 178 | * @return array|false |
||
| 179 | */ |
||
| 180 | protected function completeForOptionShortcuts() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Attempt to complete the current word as the value of an option shortcut |
||
| 197 | * |
||
| 198 | * @return array|false |
||
| 199 | */ |
||
| 200 | View Code Duplication | protected function completeForOptionShortcutValues() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Attemp to complete the current word as the value of a long-form option |
||
| 228 | * |
||
| 229 | * @return array|false |
||
| 230 | */ |
||
| 231 | View Code Duplication | protected function completeForOptionValues() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Attempt to complete the current word as a command name |
||
| 258 | * |
||
| 259 | * @return array|false |
||
| 260 | */ |
||
| 261 | protected function completeForCommandName() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Attempt to complete the current word as a command argument value |
||
| 272 | * |
||
| 273 | * @see Symfony\Component\Console\Input\InputArgument |
||
| 274 | * @return array|false |
||
| 275 | */ |
||
| 276 | protected function completeForCommandArguments() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Find a CompletionInterface that matches the current command, target name, and target type |
||
| 307 | * |
||
| 308 | * @param string $name |
||
| 309 | * @param string $type |
||
| 310 | * @return CompletionInterface|null |
||
| 311 | */ |
||
| 312 | protected function getCompletionHelper($name, $type) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Complete the value for the given option if a value completion is availble |
||
| 331 | * |
||
| 332 | * @param InputOption $option |
||
| 333 | * @return array|false |
||
| 334 | */ |
||
| 335 | protected function completeOption(InputOption $option) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Step through the command line to determine which word positions represent which argument values |
||
| 350 | * |
||
| 351 | * The word indexes of argument values are found by eliminating words that are known to not be arguments (options, |
||
| 352 | * option values, and command names). Any word that doesn't match for elimination is assumed to be an argument value, |
||
| 353 | * |
||
| 354 | * @param InputArgument[] $argumentDefinitions |
||
| 355 | * @return array as [argument name => word index on command line] |
||
| 356 | */ |
||
| 357 | protected function mapArgumentsToWords($argumentDefinitions) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Build a list of option words/flags that will have a value after them |
||
| 392 | * Options are returned in the format they appear as on the command line. |
||
| 393 | * |
||
| 394 | * @return string[] - eg. ['--myoption', '-m', ... ] |
||
| 395 | */ |
||
| 396 | protected function getOptionWordsWithValues() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Filter out results that don't match the current word on the command line |
||
| 415 | * |
||
| 416 | * @param string[] $array |
||
| 417 | * @return string[] |
||
| 418 | */ |
||
| 419 | protected function filterResults(array $array) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Get the combined options of the application and entered command |
||
| 430 | * |
||
| 431 | * @return InputOption[] |
||
| 432 | */ |
||
| 433 | protected function getAllOptions() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get command names available for completion |
||
| 447 | * |
||
| 448 | * Filters out hidden commands where supported. |
||
| 449 | * |
||
| 450 | * @return string[] |
||
| 451 | */ |
||
| 452 | protected function getCommandNames() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Find the current command name in the command-line |
||
| 480 | * |
||
| 481 | * Note this only cares about flag-type options. Options with values cannot |
||
| 482 | * appear before a command name in Symfony Console application. |
||
| 483 | * |
||
| 484 | * @return Command|null |
||
| 485 | */ |
||
| 486 | private function detectCommand() |
||
| 517 | } |
||
| 518 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.