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 | const PRE_STAGE = 'pre-'; |
||
25 | const PRIMARY_STAGE = ''; |
||
26 | const POST_STAGE = 'post-'; |
||
27 | |||
28 | public function __construct() |
||
31 | |||
32 | /** |
||
33 | * Add a hook |
||
34 | * |
||
35 | * @param string $name The name of the command to hook |
||
36 | * ('*' for all) |
||
37 | * @param string $hook The name of the hook to add |
||
38 | * @param mixed $callback The callback function to call |
||
39 | */ |
||
40 | public function add($name, $hook, callable $callback) |
||
44 | |||
45 | /** |
||
46 | * Add a validator hook |
||
47 | * |
||
48 | * @param type ValidatorInterface $validator |
||
49 | * @param type $name The name of the command to hook |
||
50 | * ('*' for all) |
||
51 | */ |
||
52 | public function addValidator(ValidatorInterface $validator, $name = '*', $stage = self::PRIMARY_STAGE) |
||
57 | |||
58 | /** |
||
59 | * Add a result processor. |
||
60 | * |
||
61 | * @param type ProcessResultInterface $resultProcessor |
||
62 | * @param type $name The name of the command to hook |
||
63 | * ('*' for all) |
||
64 | */ |
||
65 | public function addResultProcessor(ProcessResultInterface $resultProcessor, $name = '*', $stage = self::PRIMARY_STAGE) |
||
70 | |||
71 | /** |
||
72 | * Add a result alterer. After a result is processed |
||
73 | * by a result processor, an alter hook may be used |
||
74 | * to convert the result from one form to another. |
||
75 | * |
||
76 | * @param type AlterResultInterface $resultAlterer |
||
77 | * @param type $name The name of the command to hook |
||
78 | * ('*' for all) |
||
79 | */ |
||
80 | public function addAlterResult(AlterResultInterface $resultAlterer, $name = '*', $stage = self::PRIMARY_STAGE) |
||
85 | |||
86 | /** |
||
87 | * Add a status determiner. Usually, a command should return |
||
88 | * an integer on error, or a result object on success (which |
||
89 | * implies a status code of zero). If a result contains the |
||
90 | * status code in some other field, then a status determiner |
||
91 | * can be used to call the appropriate accessor method to |
||
92 | * determine the status code. This is usually not necessary, |
||
93 | * though; a command that fails may return a CommandError |
||
94 | * object, which contains a status code and a result message |
||
95 | * to display. |
||
96 | * @see CommandError::getExitCode() |
||
97 | * |
||
98 | * @param type StatusDeterminerInterface $statusDeterminer |
||
99 | * @param type $name The name of the command to hook |
||
100 | * ('*' for all) |
||
101 | */ |
||
102 | public function addStatusDeterminer(StatusDeterminerInterface $statusDeterminer, $name = '*') |
||
106 | |||
107 | /** |
||
108 | * Add an output extractor. If a command returns an object |
||
109 | * object, by default it is passed directly to the output |
||
110 | * formatter (if in use) for rendering. If the result object |
||
111 | * contains more information than just the data to render, though, |
||
112 | * then an output extractor can be used to call the appopriate |
||
113 | * accessor method of the result object to get the data to |
||
114 | * rendered. This is usually not necessary, though; it is preferable |
||
115 | * to have complex result objects implement the OutputDataInterface. |
||
116 | * @see OutputDataInterface::getOutputData() |
||
117 | * |
||
118 | * @param type ExtractOutputInterface $outputExtractor |
||
119 | * @param type $name The name of the command to hook |
||
120 | * ('*' for all) |
||
121 | */ |
||
122 | public function addOutputExtractor(ExtractOutputInterface $outputExtractor, $name = '*') |
||
126 | |||
127 | /** |
||
128 | * Get a set of hooks with the provided name(s). |
||
129 | * |
||
130 | * @param string|array $names The name of the function being hooked. |
||
131 | * @param string $hook The specific hook name (e.g. alter) |
||
132 | * |
||
133 | * @return callable[] |
||
134 | */ |
||
135 | public function get($names, $hook) |
||
143 | |||
144 | public function validateArguments($names, $args) |
||
158 | |||
159 | /** |
||
160 | * Process result and decide what to do with it. |
||
161 | * Allow client to add transformation / interpretation |
||
162 | * callbacks. |
||
163 | */ |
||
164 | public function alterResult($names, $result, $args) |
||
177 | |||
178 | /** |
||
179 | * Call all status determiners, and see if any of them |
||
180 | * know how to convert to a status code. |
||
181 | */ |
||
182 | public function determineStatusCode($names, $result) |
||
202 | |||
203 | /** |
||
204 | * Convert the result object to printable output in |
||
205 | * structured form. |
||
206 | */ |
||
207 | public function extractOutput($names, $result) |
||
223 | |||
224 | protected function getValidators($names) |
||
228 | |||
229 | protected function getStatusDeterminers($names) |
||
233 | |||
234 | protected function getProcessResultHooks($names) |
||
238 | |||
239 | protected function getAlterResultHooks($names) |
||
243 | |||
244 | protected function getOutputExtractors($names) |
||
248 | |||
249 | /** |
||
250 | * Get a set of hooks with the provided name(s). Include the |
||
251 | * pre- and post- hooks, and also include the global hooks ('*') |
||
252 | * in addition to the named hooks provided. |
||
253 | * |
||
254 | * @param string|array $names The name of the function being hooked. |
||
255 | * @param string $hook The specific hook name (e.g. alter) |
||
256 | * |
||
257 | * @return callable[] |
||
258 | */ |
||
259 | protected function getHooks($names, $hook) |
||
269 | |||
270 | /** |
||
271 | * Get a single named hook. |
||
272 | * |
||
273 | * @param string $name The name of the hooked method |
||
274 | * @param string $hook The specific hook name (e.g. alter) |
||
275 | * |
||
276 | * @return callable[] |
||
277 | */ |
||
278 | protected function getHook($name, $hook) |
||
285 | |||
286 | protected function callValidator($validator, $args) |
||
295 | |||
296 | protected function callProcessor($processor, $result, $args) |
||
310 | |||
311 | protected function callDeterminer($determiner, $result) |
||
320 | |||
321 | protected function callExtractor($extractor, $result) |
||
330 | |||
331 | protected function checkValidStage($stage) |
||
338 | } |
||
339 |