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 HookManager 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 HookManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class HookManager implements EventSubscriberInterface |
||
22 | { |
||
23 | protected $hooks = []; |
||
24 | /** var CommandInfo[] */ |
||
25 | protected $hookOptions = []; |
||
26 | |||
27 | const PRE_COMMAND_EVENT = 'pre-command-event'; |
||
28 | const COMMAND_EVENT = 'command-event'; |
||
29 | const POST_COMMAND_EVENT = 'post-command-event'; |
||
30 | const PRE_OPTION_HOOK = 'pre-option'; |
||
31 | const OPTION_HOOK = 'option'; |
||
32 | const POST_OPTION_HOOK = 'post-option'; |
||
33 | const PRE_INITIALIZE = 'pre-init'; |
||
34 | const INITIALIZE = 'init'; |
||
35 | const POST_INITIALIZE = 'post-init'; |
||
36 | const PRE_INTERACT = 'pre-interact'; |
||
37 | const INTERACT = 'interact'; |
||
38 | const POST_INTERACT = 'post-interact'; |
||
39 | const PRE_ARGUMENT_VALIDATOR = 'pre-validate'; |
||
40 | const ARGUMENT_VALIDATOR = 'validate'; |
||
41 | const POST_ARGUMENT_VALIDATOR = 'post-validate'; |
||
42 | const PRE_COMMAND_HOOK = 'pre-command'; |
||
43 | const COMMAND_HOOK = 'command'; |
||
44 | const POST_COMMAND_HOOK = 'post-command'; |
||
45 | const PRE_PROCESS_RESULT = 'pre-process'; |
||
46 | const PROCESS_RESULT = 'process'; |
||
47 | const POST_PROCESS_RESULT = 'post-process'; |
||
48 | const PRE_ALTER_RESULT = 'pre-alter'; |
||
49 | const ALTER_RESULT = 'alter'; |
||
50 | const POST_ALTER_RESULT = 'post-alter'; |
||
51 | const STATUS_DETERMINER = 'status'; |
||
52 | const EXTRACT_OUTPUT = 'extract'; |
||
53 | const ON_EVENT = 'on-event'; |
||
54 | |||
55 | public function __construct() |
||
58 | |||
59 | public function getAllHooks() |
||
63 | |||
64 | /** |
||
65 | * Add a hook |
||
66 | * |
||
67 | * @param mixed $callback The callback function to call |
||
68 | * @param string $hook The name of the hook to add |
||
69 | * @param string $name The name of the command to hook |
||
70 | * ('*' for all) |
||
71 | */ |
||
72 | public function add(callable $callback, $hook, $name = '*') |
||
80 | |||
81 | public function recordHookOptions($commandInfo, $name) |
||
86 | |||
87 | public static function getNames($command, $callback) |
||
96 | |||
97 | protected static function getNamesUsingCommands($command) |
||
104 | |||
105 | /** |
||
106 | * If a command hook does not specify any particular command |
||
107 | * name that it should be attached to, then it will be applied |
||
108 | * to every command that is defined in the same class as the hook. |
||
109 | * This is controlled by using the namespace + class name of |
||
110 | * the implementing class of the callback hook. |
||
111 | */ |
||
112 | protected static function getClassNameFromCallback($callback) |
||
120 | |||
121 | /** |
||
122 | * Add an configuration provider hook |
||
123 | * |
||
124 | * @param type InitializeHookInterface $provider |
||
125 | * @param type $name The name of the command to hook |
||
126 | * ('*' for all) |
||
127 | */ |
||
128 | public function addInitializeHook(InitializeHookInterface $initializeHook, $name = '*') |
||
133 | |||
134 | /** |
||
135 | * Add an option hook |
||
136 | * |
||
137 | * @param type ValidatorInterface $validator |
||
138 | * @param type $name The name of the command to hook |
||
139 | * ('*' for all) |
||
140 | */ |
||
141 | public function addOptionHook(OptionHookInterface $interactor, $name = '*') |
||
146 | |||
147 | /** |
||
148 | * Add an interact hook |
||
149 | * |
||
150 | * @param type ValidatorInterface $validator |
||
151 | * @param type $name The name of the command to hook |
||
152 | * ('*' for all) |
||
153 | */ |
||
154 | public function addInteractor(InteractorInterface $interactor, $name = '*') |
||
159 | |||
160 | /** |
||
161 | * Add a pre-validator hook |
||
162 | * |
||
163 | * @param type ValidatorInterface $validator |
||
164 | * @param type $name The name of the command to hook |
||
165 | * ('*' for all) |
||
166 | */ |
||
167 | public function addPreValidator(ValidatorInterface $validator, $name = '*') |
||
172 | |||
173 | /** |
||
174 | * Add a validator hook |
||
175 | * |
||
176 | * @param type ValidatorInterface $validator |
||
177 | * @param type $name The name of the command to hook |
||
178 | * ('*' for all) |
||
179 | */ |
||
180 | public function addValidator(ValidatorInterface $validator, $name = '*') |
||
185 | |||
186 | /** |
||
187 | * Add a pre-command hook. This is the same as a validator hook, except |
||
188 | * that it will run after all of the post-validator hooks. |
||
189 | * |
||
190 | * @param type ValidatorInterface $preCommand |
||
191 | * @param type $name The name of the command to hook |
||
192 | * ('*' for all) |
||
193 | */ |
||
194 | public function addPreCommandHook(ValidatorInterface $preCommand, $name = '*') |
||
199 | |||
200 | /** |
||
201 | * Add a post-command hook. This is the same as a pre-process hook, |
||
202 | * except that it will run before the first pre-process hook. |
||
203 | * |
||
204 | * @param type ProcessResultInterface $postCommand |
||
205 | * @param type $name The name of the command to hook |
||
206 | * ('*' for all) |
||
207 | */ |
||
208 | public function addPostCommandHook(ProcessResultInterface $postCommand, $name = '*') |
||
213 | |||
214 | /** |
||
215 | * Add a result processor. |
||
216 | * |
||
217 | * @param type ProcessResultInterface $resultProcessor |
||
218 | * @param type $name The name of the command to hook |
||
219 | * ('*' for all) |
||
220 | */ |
||
221 | public function addResultProcessor(ProcessResultInterface $resultProcessor, $name = '*') |
||
226 | |||
227 | /** |
||
228 | * Add a result alterer. After a result is processed |
||
229 | * by a result processor, an alter hook may be used |
||
230 | * to convert the result from one form to another. |
||
231 | * |
||
232 | * @param type AlterResultInterface $resultAlterer |
||
233 | * @param type $name The name of the command to hook |
||
234 | * ('*' for all) |
||
235 | */ |
||
236 | public function addAlterResult(AlterResultInterface $resultAlterer, $name = '*') |
||
241 | |||
242 | /** |
||
243 | * Add a status determiner. Usually, a command should return |
||
244 | * an integer on error, or a result object on success (which |
||
245 | * implies a status code of zero). If a result contains the |
||
246 | * status code in some other field, then a status determiner |
||
247 | * can be used to call the appropriate accessor method to |
||
248 | * determine the status code. This is usually not necessary, |
||
249 | * though; a command that fails may return a CommandError |
||
250 | * object, which contains a status code and a result message |
||
251 | * to display. |
||
252 | * @see CommandError::getExitCode() |
||
253 | * |
||
254 | * @param type StatusDeterminerInterface $statusDeterminer |
||
255 | * @param type $name The name of the command to hook |
||
256 | * ('*' for all) |
||
257 | */ |
||
258 | public function addStatusDeterminer(StatusDeterminerInterface $statusDeterminer, $name = '*') |
||
263 | |||
264 | /** |
||
265 | * Add an output extractor. If a command returns an object |
||
266 | * object, by default it is passed directly to the output |
||
267 | * formatter (if in use) for rendering. If the result object |
||
268 | * contains more information than just the data to render, though, |
||
269 | * then an output extractor can be used to call the appopriate |
||
270 | * accessor method of the result object to get the data to |
||
271 | * rendered. This is usually not necessary, though; it is preferable |
||
272 | * to have complex result objects implement the OutputDataInterface. |
||
273 | * @see OutputDataInterface::getOutputData() |
||
274 | * |
||
275 | * @param type ExtractOutputInterface $outputExtractor |
||
276 | * @param type $name The name of the command to hook |
||
277 | * ('*' for all) |
||
278 | */ |
||
279 | public function addOutputExtractor(ExtractOutputInterface $outputExtractor, $name = '*') |
||
284 | |||
285 | public function initializeHook( |
||
295 | |||
296 | public function optionsHook( |
||
308 | |||
309 | public function getHookOptionsForCommand($command) |
||
314 | |||
315 | /** |
||
316 | * @return CommandInfo[] |
||
317 | */ |
||
318 | public function getHookOptions($names) |
||
328 | |||
329 | public function interact( |
||
340 | |||
341 | public function validateArguments($names, CommandData $commandData) |
||
354 | |||
355 | /** |
||
356 | * Process result and decide what to do with it. |
||
357 | * Allow client to add transformation / interpretation |
||
358 | * callbacks. |
||
359 | */ |
||
360 | public function alterResult($names, $result, CommandData $commandData) |
||
373 | |||
374 | /** |
||
375 | * Call all status determiners, and see if any of them |
||
376 | * know how to convert to a status code. |
||
377 | */ |
||
378 | public function determineStatusCode($names, $result) |
||
398 | |||
399 | /** |
||
400 | * Convert the result object to printable output in |
||
401 | * structured form. |
||
402 | */ |
||
403 | public function extractOutput($names, $result) |
||
419 | |||
420 | protected function getCommandEventHooks($names) |
||
431 | |||
432 | protected function getInitializeHooks($names, AnnotationData $annotationData) |
||
444 | |||
445 | protected function getOptionHooks($names, AnnotationData $annotationData) |
||
457 | |||
458 | protected function getInteractors($names, AnnotationData $annotationData) |
||
470 | |||
471 | View Code Duplication | protected function getValidators($names, AnnotationData $annotationData) |
|
485 | |||
486 | protected function getProcessResultHooks($names, AnnotationData $annotationData) |
||
498 | |||
499 | View Code Duplication | protected function getAlterResultHooks($names, AnnotationData $annotationData) |
|
512 | |||
513 | protected function getStatusDeterminers($names) |
||
522 | |||
523 | protected function getOutputExtractors($names) |
||
532 | |||
533 | /** |
||
534 | * Get a set of hooks with the provided name(s). Include the |
||
535 | * pre- and post- hooks, and also include the global hooks ('*') |
||
536 | * in addition to the named hooks provided. |
||
537 | * |
||
538 | * @param string|array $names The name of the function being hooked. |
||
539 | * @param string[] $hooks A list of hooks (e.g. [HookManager::ALTER_RESULT]) |
||
540 | * |
||
541 | * @return callable[] |
||
542 | */ |
||
543 | public function getHooks($names, $hooks, $annotationData = null) |
||
547 | |||
548 | protected function addWildcardHooksToNames($names, $annotationData = null) |
||
559 | |||
560 | /** |
||
561 | * Get a set of hooks with the provided name(s). |
||
562 | * |
||
563 | * @param string|array $names The name of the function being hooked. |
||
564 | * @param string[] $hooks The list of hook names (e.g. [HookManager::ALTER_RESULT]) |
||
565 | * |
||
566 | * @return callable[] |
||
567 | */ |
||
568 | public function get($names, $hooks) |
||
578 | |||
579 | /** |
||
580 | * Get a single named hook. |
||
581 | * |
||
582 | * @param string $name The name of the hooked method |
||
583 | * @param string $hook The specific hook name (e.g. alter) |
||
584 | * |
||
585 | * @return callable[] |
||
586 | */ |
||
587 | public function getHook($name, $hook) |
||
594 | |||
595 | protected function callInitializeHook($provider, $input, AnnotationData $annotationData) |
||
596 | { |
||
597 | if ($provider instanceof InitializeHookInterface) { |
||
598 | return $provider->initialize($input, $annotationData); |
||
599 | } |
||
600 | if (is_callable($provider)) { |
||
601 | return $provider($input, $annotationData); |
||
602 | } |
||
603 | } |
||
604 | |||
605 | protected function callOptionHook($optionHook, $command, AnnotationData $annotationData) |
||
614 | |||
615 | protected function callInteractor($interactor, $input, $output, AnnotationData $annotationData) |
||
624 | |||
625 | protected function callValidator($validator, CommandData $commandData) |
||
634 | |||
635 | protected function callProcessor($processor, $result, CommandData $commandData) |
||
649 | |||
650 | protected function callDeterminer($determiner, $result) |
||
659 | |||
660 | protected function callExtractor($extractor, $result) |
||
669 | |||
670 | /** |
||
671 | * @param ConsoleCommandEvent $event |
||
672 | */ |
||
673 | public function callCommandEventHooks(ConsoleCommandEvent $event) |
||
685 | |||
686 | public function findAndAddHookOptions($command) |
||
693 | |||
694 | /** |
||
695 | * @{@inheritdoc} |
||
696 | */ |
||
697 | public static function getSubscribedEvents() |
||
701 | } |
||
702 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.