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 |
||
19 | class HookManager implements EventSubscriberInterface |
||
20 | { |
||
21 | protected $hooks = []; |
||
22 | |||
23 | const PRE_COMMAND_EVENT = 'pre-command'; |
||
24 | const COMMAND_EVENT = 'command'; |
||
25 | const POST_COMMAND_EVENT = 'post-command'; |
||
26 | const PRE_ARGUMENT_VALIDATOR = 'pre-validate'; |
||
27 | const ARGUMENT_VALIDATOR = 'validate'; |
||
28 | const POST_ARGUMENT_VALIDATOR = 'post-validate'; |
||
29 | const PRE_PROCESS_RESULT = 'pre-process'; |
||
30 | const PROCESS_RESULT = 'process'; |
||
31 | const POST_PROCESS_RESULT = 'post-process'; |
||
32 | const PRE_ALTER_RESULT = 'pre-alter'; |
||
33 | const ALTER_RESULT = 'alter'; |
||
34 | const POST_ALTER_RESULT = 'post-alter'; |
||
35 | const STATUS_DETERMINER = 'status'; |
||
36 | const EXTRACT_OUTPUT = 'extract'; |
||
37 | |||
38 | public function __construct() |
||
41 | |||
42 | /** |
||
43 | * Add a hook |
||
44 | * |
||
45 | * @param mixed $callback The callback function to call |
||
46 | * @param string $hook The name of the hook to add |
||
47 | * @param string $name The name of the command to hook |
||
48 | * ('*' for all) |
||
49 | */ |
||
50 | public function add(callable $callback, $hook, $name = '*') |
||
54 | |||
55 | /** |
||
56 | * Add a pre-validator hook |
||
57 | * |
||
58 | * @param type ValidatorInterface $validator |
||
59 | * @param type $name The name of the command to hook |
||
60 | * ('*' for all) |
||
61 | */ |
||
62 | public function addPreValidator(ValidatorInterface $validator, $name = '*') |
||
66 | |||
67 | /** |
||
68 | * Add a validator hook |
||
69 | * |
||
70 | * @param type ValidatorInterface $validator |
||
71 | * @param type $name The name of the command to hook |
||
72 | * ('*' for all) |
||
73 | */ |
||
74 | public function addValidator(ValidatorInterface $validator, $name = '*') |
||
78 | |||
79 | /** |
||
80 | * Add a result processor. |
||
81 | * |
||
82 | * @param type ProcessResultInterface $resultProcessor |
||
83 | * @param type $name The name of the command to hook |
||
84 | * ('*' for all) |
||
85 | */ |
||
86 | public function addResultProcessor(ProcessResultInterface $resultProcessor, $name = '*') |
||
90 | |||
91 | /** |
||
92 | * Add a result alterer. After a result is processed |
||
93 | * by a result processor, an alter hook may be used |
||
94 | * to convert the result from one form to another. |
||
95 | * |
||
96 | * @param type AlterResultInterface $resultAlterer |
||
97 | * @param type $name The name of the command to hook |
||
98 | * ('*' for all) |
||
99 | */ |
||
100 | public function addAlterResult(AlterResultInterface $resultAlterer, $name = '*') |
||
104 | |||
105 | /** |
||
106 | * Add a status determiner. Usually, a command should return |
||
107 | * an integer on error, or a result object on success (which |
||
108 | * implies a status code of zero). If a result contains the |
||
109 | * status code in some other field, then a status determiner |
||
110 | * can be used to call the appropriate accessor method to |
||
111 | * determine the status code. This is usually not necessary, |
||
112 | * though; a command that fails may return a CommandError |
||
113 | * object, which contains a status code and a result message |
||
114 | * to display. |
||
115 | * @see CommandError::getExitCode() |
||
116 | * |
||
117 | * @param type StatusDeterminerInterface $statusDeterminer |
||
118 | * @param type $name The name of the command to hook |
||
119 | * ('*' for all) |
||
120 | */ |
||
121 | public function addStatusDeterminer(StatusDeterminerInterface $statusDeterminer, $name = '*') |
||
125 | |||
126 | /** |
||
127 | * Add an output extractor. If a command returns an object |
||
128 | * object, by default it is passed directly to the output |
||
129 | * formatter (if in use) for rendering. If the result object |
||
130 | * contains more information than just the data to render, though, |
||
131 | * then an output extractor can be used to call the appopriate |
||
132 | * accessor method of the result object to get the data to |
||
133 | * rendered. This is usually not necessary, though; it is preferable |
||
134 | * to have complex result objects implement the OutputDataInterface. |
||
135 | * @see OutputDataInterface::getOutputData() |
||
136 | * |
||
137 | * @param type ExtractOutputInterface $outputExtractor |
||
138 | * @param type $name The name of the command to hook |
||
139 | * ('*' for all) |
||
140 | */ |
||
141 | public function addOutputExtractor(ExtractOutputInterface $outputExtractor, $name = '*') |
||
145 | |||
146 | View Code Duplication | public function validateArguments($names, $args, AnnotationData $annotationData) |
|
160 | |||
161 | /** |
||
162 | * Process result and decide what to do with it. |
||
163 | * Allow client to add transformation / interpretation |
||
164 | * callbacks. |
||
165 | */ |
||
166 | public function alterResult($names, $result, $args, AnnotationData $annotationData) |
||
179 | |||
180 | /** |
||
181 | * Call all status determiners, and see if any of them |
||
182 | * know how to convert to a status code. |
||
183 | */ |
||
184 | public function determineStatusCode($names, $result) |
||
204 | |||
205 | /** |
||
206 | * Convert the result object to printable output in |
||
207 | * structured form. |
||
208 | */ |
||
209 | public function extractOutput($names, $result) |
||
225 | |||
226 | protected function getValidators($names, AnnotationData $annotationData) |
||
230 | |||
231 | protected function getStatusDeterminers($names) |
||
235 | |||
236 | protected function getProcessResultHooks($names, AnnotationData $annotationData) |
||
240 | |||
241 | protected function getAlterResultHooks($names, AnnotationData $annotationData) |
||
245 | |||
246 | protected function getOutputExtractors($names) |
||
250 | |||
251 | protected function getCommandEvents($names) |
||
255 | |||
256 | /** |
||
257 | * Get a set of hooks with the provided name(s). Include the |
||
258 | * pre- and post- hooks, and also include the global hooks ('*') |
||
259 | * in addition to the named hooks provided. |
||
260 | * |
||
261 | * @param string|array $names The name of the function being hooked. |
||
262 | * @param string $hook The specific hook name (e.g. alter) |
||
263 | * |
||
264 | * @return callable[] |
||
265 | */ |
||
266 | protected function getHooks($names, $hook, $annotationData = null) |
||
281 | |||
282 | /** |
||
283 | * Get a set of hooks with the provided name(s). |
||
284 | * |
||
285 | * @param string|array $names The name of the function being hooked. |
||
286 | * @param string $hook The specific hook name (e.g. alter) |
||
287 | * |
||
288 | * @return callable[] |
||
289 | */ |
||
290 | public function get($names, $hook) |
||
298 | |||
299 | /** |
||
300 | * Get a single named hook. |
||
301 | * |
||
302 | * @param string $name The name of the hooked method |
||
303 | * @param string $hook The specific hook name (e.g. alter) |
||
304 | * |
||
305 | * @return callable[] |
||
306 | */ |
||
307 | protected function getHook($name, $hook) |
||
314 | |||
315 | protected function callValidator($validator, $args, AnnotationData $annotationData) |
||
327 | |||
328 | protected function callProcessor($processor, $result, $args, AnnotationData $annotationData) |
||
345 | |||
346 | protected function callDeterminer($determiner, $result) |
||
355 | |||
356 | protected function callExtractor($extractor, $result) |
||
365 | |||
366 | /** |
||
367 | * @param ConsoleCommandEvent $event |
||
368 | */ |
||
369 | public function callCommandEventHooks(ConsoleCommandEvent $event) |
||
381 | |||
382 | /** |
||
383 | * @{@inheritdoc} |
||
384 | */ |
||
385 | public static function getSubscribedEvents() |
||
389 | } |
||
390 |
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.