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 |
||
14 | class HookManager |
||
15 | { |
||
16 | protected $hooks = []; |
||
17 | |||
18 | const ARGUMENT_VALIDATOR = 'validate'; |
||
19 | const PROCESS_RESULT = 'process'; |
||
20 | const ALTER_RESULT = 'alter'; |
||
21 | const STATUS_DETERMINER = 'status'; |
||
22 | const EXTRACT_OUTPUT = 'extract'; |
||
23 | |||
24 | public function __construct() |
||
27 | |||
28 | /** |
||
29 | * Add a hook |
||
30 | * |
||
31 | * @param string $name The name of the command to hook |
||
32 | * ('*' for all) |
||
33 | * @param string $hook The name of the hook to add |
||
34 | * @param mixed $callback The callback function to call |
||
35 | */ |
||
36 | public function add($name, $hook, callable $callback) |
||
40 | |||
41 | /** |
||
42 | * Add a validator hook |
||
43 | * |
||
44 | * @param type ValidatorInterface $validator |
||
45 | * @param type $name The name of the command to hook |
||
46 | * ('*' for all) |
||
47 | */ |
||
48 | public function addValidator(ValidatorInterface $validator, $name = '*') |
||
52 | |||
53 | /** |
||
54 | * Add a result processor. |
||
55 | * |
||
56 | * @param type ProcessResultInterface $resultProcessor |
||
57 | * @param type $name The name of the command to hook |
||
58 | * ('*' for all) |
||
59 | */ |
||
60 | public function addResultProcessor(ProcessResultInterface $resultProcessor, $name = '*') |
||
64 | |||
65 | /** |
||
66 | * Add a result alterer. After a result is processed |
||
67 | * by a result processor, an alter hook may be used |
||
68 | * to convert the result from one form to another. |
||
69 | * |
||
70 | * @param type AlterResultInterface $resultAlterer |
||
71 | * @param type $name The name of the command to hook |
||
72 | * ('*' for all) |
||
73 | */ |
||
74 | public function addAlterResult(AlterResultInterface $resultAlterer, $name = '*') |
||
78 | |||
79 | /** |
||
80 | * Add a status determiner. Usually, a command should return |
||
81 | * an integer on error, or a result object on success (which |
||
82 | * implies a status code of zero). If a result contains the |
||
83 | * status code in some other field, then a status determiner |
||
84 | * can be used to call the appropriate accessor method to |
||
85 | * determine the status code. This is usually not necessary, |
||
86 | * though; a command that fails may return a CommandError |
||
87 | * object, which contains a status code and a result message |
||
88 | * to display. |
||
89 | * @see CommandError::getExitCode() |
||
90 | * |
||
91 | * @param type StatusDeterminerInterface $statusDeterminer |
||
92 | * @param type $name The name of the command to hook |
||
93 | * ('*' for all) |
||
94 | */ |
||
95 | public function addStatusDeterminer(StatusDeterminerInterface $statusDeterminer, $name = '*') |
||
99 | |||
100 | /** |
||
101 | * Add an output extractor. If a command returns an object |
||
102 | * object, by default it is passed directly to the output |
||
103 | * formatter (if in use) for rendering. If the result object |
||
104 | * contains more information than just the data to render, though, |
||
105 | * then an output extractor can be used to call the appopriate |
||
106 | * accessor method of the result object to get the data to |
||
107 | * rendered. This is usually not necessary, though; it is preferable |
||
108 | * to have complex result objects implement the OutputDataInterface. |
||
109 | * @see OutputDataInterface::getOutputData() |
||
110 | * |
||
111 | * @param type ExtractOutputInterface $outputExtractor |
||
112 | * @param type $name The name of the command to hook |
||
113 | * ('*' for all) |
||
114 | */ |
||
115 | public function addOutputExtractor(ExtractOutputInterface $outputExtractor, $name = '*') |
||
119 | |||
120 | /** |
||
121 | * Get a set of hooks with the provided name(s). |
||
122 | * |
||
123 | * @param string|array $names The name of the function being hooked. |
||
124 | * @param string $hook The specific hook name (e.g. alter) |
||
125 | * |
||
126 | * @return callable[] |
||
127 | */ |
||
128 | public function get($names, $hook) |
||
136 | |||
137 | public function validateArguments($names, $args) |
||
151 | |||
152 | /** |
||
153 | * Process result and decide what to do with it. |
||
154 | * Allow client to add transformation / interpretation |
||
155 | * callbacks. |
||
156 | */ |
||
157 | public function alterResult($names, $result, $args) |
||
170 | |||
171 | /** |
||
172 | * Call all status determiners, and see if any of them |
||
173 | * know how to convert to a status code. |
||
174 | */ |
||
175 | public function determineStatusCode($names, $result) |
||
195 | |||
196 | /** |
||
197 | * Convert the result object to printable output in |
||
198 | * structured form. |
||
199 | */ |
||
200 | public function extractOutput($names, $result) |
||
216 | |||
217 | protected function getValidators($names) |
||
221 | |||
222 | protected function getStatusDeterminers($names) |
||
226 | |||
227 | protected function getProcessResultHooks($names) |
||
231 | |||
232 | protected function getAlterResultHooks($names) |
||
236 | |||
237 | protected function getOutputExtractors($names) |
||
241 | |||
242 | /** |
||
243 | * Get a set of hooks with the provided name(s). Include the |
||
244 | * pre- and post- hooks, and also include the global hooks ('*') |
||
245 | * in addition to the named hooks provided. |
||
246 | * |
||
247 | * @param string|array $names The name of the function being hooked. |
||
248 | * @param string $hook The specific hook name (e.g. alter) |
||
249 | * |
||
250 | * @return callable[] |
||
251 | */ |
||
252 | protected function getHooks($names, $hook) |
||
262 | |||
263 | /** |
||
264 | * Get a single named hook. |
||
265 | * |
||
266 | * @param string $name The name of the hooked method |
||
267 | * @param string $hook The specific hook name (e.g. alter) |
||
268 | * |
||
269 | * @return callable[] |
||
270 | */ |
||
271 | protected function getHook($name, $hook) |
||
278 | |||
279 | protected function callValidator($validator, $args) |
||
288 | |||
289 | protected function callProcessor($processor, $result, $args) |
||
303 | |||
304 | protected function callDeterminer($determiner, $result) |
||
313 | |||
314 | protected function callExtractor($extractor, $result) |
||
323 | } |
||
324 |