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)  | 
            ||
| 38 |     { | 
            ||
| 39 | $this->application = $application;  | 
            ||
| 40 | $this->context = $context;  | 
            ||
| 41 | |||
| 42 | $this->addHandler(  | 
            ||
| 43 | new Completion(  | 
            ||
| 44 | 'help',  | 
            ||
| 45 | 'command_name',  | 
            ||
| 46 | Completion::TYPE_ARGUMENT,  | 
            ||
| 47 | array_keys($application->all())  | 
            ||
| 48 | )  | 
            ||
| 49 | );  | 
            ||
| 50 | |||
| 51 | $this->addHandler(  | 
            ||
| 52 | new Completion(  | 
            ||
| 53 | 'list',  | 
            ||
| 54 | 'namespace',  | 
            ||
| 55 | Completion::TYPE_ARGUMENT,  | 
            ||
| 56 | $application->getNamespaces()  | 
            ||
| 57 | )  | 
            ||
| 58 | );  | 
            ||
| 59 | }  | 
            ||
| 60 | |||
| 61 | public function setContext(CompletionContext $context)  | 
            ||
| 65 | |||
| 66 | /**  | 
            ||
| 67 | * @return CompletionContext  | 
            ||
| 68 | */  | 
            ||
| 69 | public function getContext()  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * @param CompletionInterface[] $array  | 
            ||
| 76 | */  | 
            ||
| 77 | public function addHandlers(array $array)  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * @param CompletionInterface $helper  | 
            ||
| 84 | */  | 
            ||
| 85 | public function addHandler(CompletionInterface $helper)  | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * Do the actual completion, returning an array of strings to provide to the parent shell's completion system  | 
            ||
| 92 | *  | 
            ||
| 93 | * @throws \RuntimeException  | 
            ||
| 94 | * @return string[]  | 
            ||
| 95 | */  | 
            ||
| 96 | public function runCompletion()  | 
            ||
| 130 | |||
| 131 | /**  | 
            ||
| 132 | * Get an InputInterface representation of the completion context  | 
            ||
| 133 | *  | 
            ||
| 134 | * @return ArrayInput  | 
            ||
| 135 | */  | 
            ||
| 136 | public function getInput()  | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * Attempt to complete the current word as a long-form option (--my-option)  | 
            ||
| 148 | *  | 
            ||
| 149 | * @return array|false  | 
            ||
| 150 | */  | 
            ||
| 151 | protected function completeForOptions()  | 
            ||
| 167 | |||
| 168 | /**  | 
            ||
| 169 | * Attempt to complete the current word as an option shortcut.  | 
            ||
| 170 | *  | 
            ||
| 171 | * If the shortcut exists it will be completed, but a list of possible shortcuts is never returned for completion.  | 
            ||
| 172 | *  | 
            ||
| 173 | * @return array|false  | 
            ||
| 174 | */  | 
            ||
| 175 | protected function completeForOptionShortcuts()  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Attempt to complete the current word as the value of an option shortcut  | 
            ||
| 192 | *  | 
            ||
| 193 | * @return array|false  | 
            ||
| 194 | */  | 
            ||
| 195 | View Code Duplication | protected function completeForOptionShortcutValues()  | 
            |
| 220 | |||
| 221 | /**  | 
            ||
| 222 | * Attemp to complete the current word as the value of a long-form option  | 
            ||
| 223 | *  | 
            ||
| 224 | * @return array|false  | 
            ||
| 225 | */  | 
            ||
| 226 | View Code Duplication | protected function completeForOptionValues()  | 
            |
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * Attempt to complete the current word as a command name  | 
            ||
| 253 | *  | 
            ||
| 254 | * @return array|false  | 
            ||
| 255 | */  | 
            ||
| 256 | protected function completeForCommandName()  | 
            ||
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * Attempt to complete the current word as a command argument value  | 
            ||
| 274 | *  | 
            ||
| 275 | * @see Symfony\Component\Console\Input\InputArgument  | 
            ||
| 276 | * @return array|false  | 
            ||
| 277 | */  | 
            ||
| 278 | protected function completeForCommandArguments()  | 
            ||
| 306 | |||
| 307 | /**  | 
            ||
| 308 | * Find a CompletionInterface that matches the current command, target name, and target type  | 
            ||
| 309 | *  | 
            ||
| 310 | * @param string $name  | 
            ||
| 311 | * @param string $type  | 
            ||
| 312 | * @return CompletionInterface|null  | 
            ||
| 313 | */  | 
            ||
| 314 | protected function getCompletionHelper($name, $type)  | 
            ||
| 330 | |||
| 331 | /**  | 
            ||
| 332 | * Complete the value for the given option if a value completion is availble  | 
            ||
| 333 | *  | 
            ||
| 334 | * @param InputOption $option  | 
            ||
| 335 | * @return array|false  | 
            ||
| 336 | */  | 
            ||
| 337 | protected function completeOption(InputOption $option)  | 
            ||
| 349 | |||
| 350 | /**  | 
            ||
| 351 | * Step through the command line to determine which word positions represent which argument values  | 
            ||
| 352 | *  | 
            ||
| 353 | * The word indexes of argument values are found by eliminating words that are known to not be arguments (options,  | 
            ||
| 354 | * option values, and command names). Any word that doesn't match for elimination is assumed to be an argument value,  | 
            ||
| 355 | *  | 
            ||
| 356 | * @param InputArgument[] $argumentDefinitions  | 
            ||
| 357 | * @return array as [argument name => word index on command line]  | 
            ||
| 358 | */  | 
            ||
| 359 | protected function mapArgumentsToWords($argumentDefinitions)  | 
            ||
| 390 | |||
| 391 | /**  | 
            ||
| 392 | * Build a list of option words/flags that will have a value after them  | 
            ||
| 393 | * Options are returned in the format they appear as on the command line.  | 
            ||
| 394 | *  | 
            ||
| 395 | * @return string[] - eg. ['--myoption', '-m', ... ]  | 
            ||
| 396 | */  | 
            ||
| 397 | protected function getOptionWordsWithValues()  | 
            ||
| 413 | |||
| 414 | /**  | 
            ||
| 415 | * Filter out results that don't match the current word on the command line  | 
            ||
| 416 | *  | 
            ||
| 417 | * @param string[] $array  | 
            ||
| 418 | * @return string[]  | 
            ||
| 419 | */  | 
            ||
| 420 | protected function filterResults(array $array)  | 
            ||
| 428 | |||
| 429 | /**  | 
            ||
| 430 | * Get the combined options of the application and entered command  | 
            ||
| 431 | *  | 
            ||
| 432 | * @return InputOption[]  | 
            ||
| 433 | */  | 
            ||
| 434 | protected function getAllOptions()  | 
            ||
| 445 | }  | 
            ||
| 446 |