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 | * @var bool |
||
| 39 | */ |
||
| 40 | protected $commandsHidable; |
||
| 41 | |||
| 42 | public function __construct(Application $application, CompletionContext $context = null) |
||
| 66 | |||
| 67 | public function setContext(CompletionContext $context) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return CompletionContext |
||
| 74 | */ |
||
| 75 | public function getContext() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param CompletionInterface[] $array |
||
| 82 | */ |
||
| 83 | public function addHandlers(array $array) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param CompletionInterface $helper |
||
| 90 | */ |
||
| 91 | public function addHandler(CompletionInterface $helper) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Do the actual completion, returning an array of strings to provide to the parent shell's completion system |
||
| 98 | * |
||
| 99 | * @throws \RuntimeException |
||
| 100 | * @return string[] |
||
| 101 | */ |
||
| 102 | public function runCompletion() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get an InputInterface representation of the completion context |
||
| 139 | * |
||
| 140 | * @return ArrayInput |
||
| 141 | */ |
||
| 142 | public function getInput() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Attempt to complete the current word as a long-form option (--my-option) |
||
| 154 | * |
||
| 155 | * @return array|false |
||
| 156 | */ |
||
| 157 | protected function completeForOptions() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Attempt to complete the current word as an option shortcut. |
||
| 176 | * |
||
| 177 | * If the shortcut exists it will be completed, but a list of possible shortcuts is never returned for completion. |
||
| 178 | * |
||
| 179 | * @return array|false |
||
| 180 | */ |
||
| 181 | protected function completeForOptionShortcuts() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Attempt to complete the current word as the value of an option shortcut |
||
| 198 | * |
||
| 199 | * @return array|false |
||
| 200 | */ |
||
| 201 | View Code Duplication | protected function completeForOptionShortcutValues() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Attemp to complete the current word as the value of a long-form option |
||
| 229 | * |
||
| 230 | * @return array|false |
||
| 231 | */ |
||
| 232 | View Code Duplication | protected function completeForOptionValues() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Attempt to complete the current word as a command name |
||
| 259 | * |
||
| 260 | * @return array|false |
||
| 261 | */ |
||
| 262 | protected function completeForCommandName() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Attempt to complete the current word as a command argument value |
||
| 273 | * |
||
| 274 | * @see Symfony\Component\Console\Input\InputArgument |
||
| 275 | * @return array|false |
||
| 276 | */ |
||
| 277 | protected function completeForCommandArguments() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Find a CompletionInterface that matches the current command, target name, and target type |
||
| 308 | * |
||
| 309 | * @param string $name |
||
| 310 | * @param string $type |
||
| 311 | * @return CompletionInterface|null |
||
| 312 | */ |
||
| 313 | protected function getCompletionHelper($name, $type) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Complete the value for the given option if a value completion is availble |
||
| 332 | * |
||
| 333 | * @param InputOption $option |
||
| 334 | * @return array|false |
||
| 335 | */ |
||
| 336 | protected function completeOption(InputOption $option) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Step through the command line to determine which word positions represent which argument values |
||
| 351 | * |
||
| 352 | * The word indexes of argument values are found by eliminating words that are known to not be arguments (options, |
||
| 353 | * option values, and command names). Any word that doesn't match for elimination is assumed to be an argument value, |
||
| 354 | * |
||
| 355 | * @param InputArgument[] $argumentDefinitions |
||
| 356 | * @return array as [argument name => word index on command line] |
||
| 357 | */ |
||
| 358 | 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 | protected function getCommandNames() |
||
| 469 | } |
||
| 470 |