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 |
||
| 18 | class HookManager implements EventSubscriberInterface |
||
| 19 | { |
||
| 20 | protected $hooks = []; |
||
| 21 | |||
| 22 | const PRE_COMMAND_EVENT = 'pre-command'; |
||
| 23 | const COMMAND_EVENT = 'command'; |
||
| 24 | const POST_COMMAND_EVENT = 'post-command'; |
||
| 25 | const PRE_ARGUMENT_VALIDATOR = 'pre-validate'; |
||
| 26 | const ARGUMENT_VALIDATOR = 'validate'; |
||
| 27 | const POST_ARGUMENT_VALIDATOR = 'post-validate'; |
||
| 28 | const PRE_PROCESS_RESULT = 'pre-process'; |
||
| 29 | const PROCESS_RESULT = 'process'; |
||
| 30 | const POST_PROCESS_RESULT = 'post-process'; |
||
| 31 | const PRE_ALTER_RESULT = 'pre-alter'; |
||
| 32 | const ALTER_RESULT = 'alter'; |
||
| 33 | const POST_ALTER_RESULT = 'post-alter'; |
||
| 34 | const STATUS_DETERMINER = 'status'; |
||
| 35 | const EXTRACT_OUTPUT = 'extract'; |
||
| 36 | |||
| 37 | public function __construct() |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Add a hook |
||
| 43 | * |
||
| 44 | * @param mixed $callback The callback function to call |
||
| 45 | * @param string $hook The name of the hook to add |
||
| 46 | * @param string $name The name of the command to hook |
||
| 47 | * ('*' for all) |
||
| 48 | */ |
||
| 49 | public function add(callable $callback, $hook, $name = '*') |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Add a validator hook |
||
| 56 | * |
||
| 57 | * @param type ValidatorInterface $validator |
||
| 58 | * @param type $name The name of the command to hook |
||
| 59 | * ('*' for all) |
||
| 60 | */ |
||
| 61 | public function addValidator(ValidatorInterface $validator, $name = '*') |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add a result processor. |
||
| 68 | * |
||
| 69 | * @param type ProcessResultInterface $resultProcessor |
||
| 70 | * @param type $name The name of the command to hook |
||
| 71 | * ('*' for all) |
||
| 72 | */ |
||
| 73 | public function addResultProcessor(ProcessResultInterface $resultProcessor, $name = '*') |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Add a result alterer. After a result is processed |
||
| 80 | * by a result processor, an alter hook may be used |
||
| 81 | * to convert the result from one form to another. |
||
| 82 | * |
||
| 83 | * @param type AlterResultInterface $resultAlterer |
||
| 84 | * @param type $name The name of the command to hook |
||
| 85 | * ('*' for all) |
||
| 86 | */ |
||
| 87 | public function addAlterResult(AlterResultInterface $resultAlterer, $name = '*') |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Add a status determiner. Usually, a command should return |
||
| 94 | * an integer on error, or a result object on success (which |
||
| 95 | * implies a status code of zero). If a result contains the |
||
| 96 | * status code in some other field, then a status determiner |
||
| 97 | * can be used to call the appropriate accessor method to |
||
| 98 | * determine the status code. This is usually not necessary, |
||
| 99 | * though; a command that fails may return a CommandError |
||
| 100 | * object, which contains a status code and a result message |
||
| 101 | * to display. |
||
| 102 | * @see CommandError::getExitCode() |
||
| 103 | * |
||
| 104 | * @param type StatusDeterminerInterface $statusDeterminer |
||
| 105 | * @param type $name The name of the command to hook |
||
| 106 | * ('*' for all) |
||
| 107 | */ |
||
| 108 | public function addStatusDeterminer(StatusDeterminerInterface $statusDeterminer, $name = '*') |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add an output extractor. If a command returns an object |
||
| 115 | * object, by default it is passed directly to the output |
||
| 116 | * formatter (if in use) for rendering. If the result object |
||
| 117 | * contains more information than just the data to render, though, |
||
| 118 | * then an output extractor can be used to call the appopriate |
||
| 119 | * accessor method of the result object to get the data to |
||
| 120 | * rendered. This is usually not necessary, though; it is preferable |
||
| 121 | * to have complex result objects implement the OutputDataInterface. |
||
| 122 | * @see OutputDataInterface::getOutputData() |
||
| 123 | * |
||
| 124 | * @param type ExtractOutputInterface $outputExtractor |
||
| 125 | * @param type $name The name of the command to hook |
||
| 126 | * ('*' for all) |
||
| 127 | */ |
||
| 128 | public function addOutputExtractor(ExtractOutputInterface $outputExtractor, $name = '*') |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get a set of hooks with the provided name(s). |
||
| 135 | * |
||
| 136 | * @param string|array $names The name of the function being hooked. |
||
| 137 | * @param string $hook The specific hook name (e.g. alter) |
||
| 138 | * |
||
| 139 | * @return callable[] |
||
| 140 | */ |
||
| 141 | public function get($names, $hook) |
||
| 149 | |||
| 150 | public function validateArguments($names, $args) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Process result and decide what to do with it. |
||
| 167 | * Allow client to add transformation / interpretation |
||
| 168 | * callbacks. |
||
| 169 | */ |
||
| 170 | public function alterResult($names, $result, $args) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Call all status determiners, and see if any of them |
||
| 186 | * know how to convert to a status code. |
||
| 187 | */ |
||
| 188 | public function determineStatusCode($names, $result) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Convert the result object to printable output in |
||
| 211 | * structured form. |
||
| 212 | */ |
||
| 213 | public function extractOutput($names, $result) |
||
| 229 | |||
| 230 | protected function getValidators($names) |
||
| 234 | |||
| 235 | protected function getStatusDeterminers($names) |
||
| 239 | |||
| 240 | protected function getProcessResultHooks($names) |
||
| 244 | |||
| 245 | protected function getAlterResultHooks($names) |
||
| 249 | |||
| 250 | protected function getOutputExtractors($names) |
||
| 254 | |||
| 255 | protected function getCommandEvents($names) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get a set of hooks with the provided name(s). Include the |
||
| 262 | * pre- and post- hooks, and also include the global hooks ('*') |
||
| 263 | * in addition to the named hooks provided. |
||
| 264 | * |
||
| 265 | * @param string|array $names The name of the function being hooked. |
||
| 266 | * @param string $hook The specific hook name (e.g. alter) |
||
| 267 | * |
||
| 268 | * @return callable[] |
||
| 269 | */ |
||
| 270 | protected function getHooks($names, $hook) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get a single named hook. |
||
| 283 | * |
||
| 284 | * @param string $name The name of the hooked method |
||
| 285 | * @param string $hook The specific hook name (e.g. alter) |
||
| 286 | * |
||
| 287 | * @return callable[] |
||
| 288 | */ |
||
| 289 | protected function getHook($name, $hook) |
||
| 296 | |||
| 297 | protected function callValidator($validator, $args) |
||
| 306 | |||
| 307 | protected function callProcessor($processor, $result, $args) |
||
| 321 | |||
| 322 | protected function callDeterminer($determiner, $result) |
||
| 331 | |||
| 332 | protected function callExtractor($extractor, $result) |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * @param ConsoleCommandEvent $event |
||
| 345 | */ |
||
| 346 | public function callCommandEventHooks(ConsoleCommandEvent $event) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @{@inheritdoc} |
||
| 361 | */ |
||
| 362 | public static function getSubscribedEvents() |
||
| 366 | } |
||
| 367 |