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) |
||
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) |
||
78 | { |
||
79 | $this->helpers = array_merge($this->helpers, $array); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param CompletionInterface $helper |
||
84 | */ |
||
85 | public function addHandler(CompletionInterface $helper) |
||
86 | { |
||
87 | $this->helpers[] = $helper; |
||
88 | } |
||
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() |
||
137 | { |
||
138 | // Filter the command line content to suit ArrayInput |
||
139 | $words = $this->context->getWords(); |
||
140 | array_shift($words); |
||
141 | $words = array_filter($words); |
||
142 | |||
143 | return new ArrayInput($words); |
||
144 | } |
||
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() |
||
264 | |||
265 | /** |
||
266 | * Attempt to complete the current word as a command argument value |
||
267 | * |
||
268 | * @see Symfony\Component\Console\Input\InputArgument |
||
269 | * @return array|false |
||
270 | */ |
||
271 | protected function completeForCommandArguments() |
||
299 | |||
300 | /** |
||
301 | * Find a CompletionInterface that matches the current command, target name, and target type |
||
302 | * |
||
303 | * @param string $name |
||
304 | * @param string $type |
||
305 | * @return CompletionInterface|null |
||
306 | */ |
||
307 | protected function getCompletionHelper($name, $type) |
||
323 | |||
324 | /** |
||
325 | * Complete the value for the given option if a value completion is availble |
||
326 | * |
||
327 | * @param InputOption $option |
||
328 | * @return array|false |
||
329 | */ |
||
330 | protected function completeOption(InputOption $option) |
||
342 | |||
343 | /** |
||
344 | * Step through the command line to determine which word positions represent which argument values |
||
345 | * |
||
346 | * The word indexes of argument values are found by eliminating words that are known to not be arguments (options, |
||
347 | * option values, and command names). Any word that doesn't match for elimination is assumed to be an argument value, |
||
348 | * |
||
349 | * @param InputArgument[] $argumentDefinitions |
||
350 | * @return array as [argument name => word index on command line] |
||
351 | */ |
||
352 | protected function mapArgumentsToWords($argumentDefinitions) |
||
383 | |||
384 | /** |
||
385 | * Build a list of option words/flags that will have a value after them |
||
386 | * Options are returned in the format they appear as on the command line. |
||
387 | * |
||
388 | * @return string[] - eg. ['--myoption', '-m', ... ] |
||
389 | */ |
||
390 | protected function getOptionWordsWithValues() |
||
406 | |||
407 | /** |
||
408 | * Filter out results that don't match the current word on the command line |
||
409 | * |
||
410 | * @param string[] $array |
||
411 | * @return string[] |
||
412 | */ |
||
413 | protected function filterResults(array $array) |
||
421 | |||
422 | /** |
||
423 | * Get the combined options of the application and entered command |
||
424 | * |
||
425 | * @return InputOption[] |
||
426 | */ |
||
427 | protected function getAllOptions() |
||
438 | |||
439 | protected function getCommandNames() |
||
453 | } |
||
454 |