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 | public function __construct(Application $application, CompletionContext $context = null) |
||
| 61 | |||
| 62 | public function setContext(CompletionContext $context) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return CompletionContext |
||
| 69 | */ |
||
| 70 | public function getContext() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param CompletionInterface[] $array |
||
| 77 | */ |
||
| 78 | public function addHandlers(array $array) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param CompletionInterface $helper |
||
| 85 | */ |
||
| 86 | public function addHandler(CompletionInterface $helper) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Do the actual completion, returning an array of strings to provide to the parent shell's completion system |
||
| 93 | * |
||
| 94 | * @throws \RuntimeException |
||
| 95 | * @return string[] |
||
| 96 | */ |
||
| 97 | public function runCompletion() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get an InputInterface representation of the completion context |
||
| 134 | * |
||
| 135 | * @return ArrayInput |
||
| 136 | */ |
||
| 137 | public function getInput() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Attempt to complete the current word as a long-form option (--my-option) |
||
| 149 | * |
||
| 150 | * @return array|false |
||
| 151 | */ |
||
| 152 | protected function completeForOptions() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Attempt to complete the current word as an option shortcut. |
||
| 171 | * |
||
| 172 | * If the shortcut exists it will be completed, but a list of possible shortcuts is never returned for completion. |
||
| 173 | * |
||
| 174 | * @return array|false |
||
| 175 | */ |
||
| 176 | protected function completeForOptionShortcuts() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Attempt to complete the current word as the value of an option shortcut |
||
| 193 | * |
||
| 194 | * @return array|false |
||
| 195 | */ |
||
| 196 | View Code Duplication | protected function completeForOptionShortcutValues() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Attemp to complete the current word as the value of a long-form option |
||
| 224 | * |
||
| 225 | * @return array|false |
||
| 226 | */ |
||
| 227 | View Code Duplication | protected function completeForOptionValues() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Attempt to complete the current word as a command name |
||
| 254 | * |
||
| 255 | * @return array|false |
||
| 256 | */ |
||
| 257 | protected function completeForCommandName() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Attempt to complete the current word as a command argument value |
||
| 268 | * |
||
| 269 | * @see Symfony\Component\Console\Input\InputArgument |
||
| 270 | * @return array|false |
||
| 271 | */ |
||
| 272 | protected function completeForCommandArguments() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Find a CompletionInterface that matches the current command, target name, and target type |
||
| 303 | * |
||
| 304 | * @param string $name |
||
| 305 | * @param string $type |
||
| 306 | * @return CompletionInterface|null |
||
| 307 | */ |
||
| 308 | protected function getCompletionHelper($name, $type) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Complete the value for the given option if a value completion is availble |
||
| 327 | * |
||
| 328 | * @param InputOption $option |
||
| 329 | * @return array|false |
||
| 330 | */ |
||
| 331 | protected function completeOption(InputOption $option) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Step through the command line to determine which word positions represent which argument values |
||
| 346 | * |
||
| 347 | * The word indexes of argument values are found by eliminating words that are known to not be arguments (options, |
||
| 348 | * option values, and command names). Any word that doesn't match for elimination is assumed to be an argument value, |
||
| 349 | * |
||
| 350 | * @param InputArgument[] $argumentDefinitions |
||
| 351 | * @return array as [argument name => word index on command line] |
||
| 352 | */ |
||
| 353 | protected function mapArgumentsToWords($argumentDefinitions) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Build a list of option words/flags that will have a value after them |
||
| 387 | * Options are returned in the format they appear as on the command line. |
||
| 388 | * |
||
| 389 | * @return string[] - eg. ['--myoption', '-m', ... ] |
||
| 390 | */ |
||
| 391 | protected function getOptionWordsWithValues() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Filter out results that don't match the current word on the command line |
||
| 410 | * |
||
| 411 | * @param string[] $array |
||
| 412 | * @return string[] |
||
| 413 | */ |
||
| 414 | protected function filterResults(array $array) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get the combined options of the application and entered command |
||
| 425 | * |
||
| 426 | * @return InputOption[] |
||
| 427 | */ |
||
| 428 | protected function getAllOptions() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get command names available for completion |
||
| 442 | * |
||
| 443 | * Filters out hidden commands where supported. |
||
| 444 | * |
||
| 445 | * @return string[] |
||
| 446 | */ |
||
| 447 | protected function getCommandNames() |
||
| 472 | } |
||
| 473 |