Complex classes like AnnotatedCommandFactory 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 AnnotatedCommandFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class AnnotatedCommandFactory implements AutomaticOptionsProviderInterface |
||
27 | { |
||
28 | /** var CommandProcessor */ |
||
29 | protected $commandProcessor; |
||
30 | |||
31 | /** var CommandCreationListenerInterface[] */ |
||
32 | protected $listeners = []; |
||
33 | |||
34 | /** var AutomaticOptionsProvider[] */ |
||
35 | protected $automaticOptionsProviderList = []; |
||
36 | |||
37 | /** var boolean */ |
||
38 | protected $includeAllPublicMethods = true; |
||
39 | |||
40 | /** var CommandInfoAltererInterface */ |
||
41 | protected $commandInfoAlterers = []; |
||
42 | |||
43 | /** var SimpleCacheInterface */ |
||
44 | protected $dataStore; |
||
45 | |||
46 | public function __construct() |
||
52 | |||
53 | public function setCommandProcessor(CommandProcessor $commandProcessor) |
||
58 | |||
59 | /** |
||
60 | * @return CommandProcessor |
||
61 | */ |
||
62 | public function commandProcessor() |
||
66 | |||
67 | /** |
||
68 | * Set the 'include all public methods flag'. If true (the default), then |
||
69 | * every public method of each commandFile will be used to create commands. |
||
70 | * If it is false, then only those public methods annotated with @command |
||
71 | * or @name (deprecated) will be used to create commands. |
||
72 | */ |
||
73 | public function setIncludeAllPublicMethods($includeAllPublicMethods) |
||
78 | |||
79 | public function getIncludeAllPublicMethods() |
||
83 | |||
84 | /** |
||
85 | * @return HookManager |
||
86 | */ |
||
87 | public function hookManager() |
||
91 | |||
92 | /** |
||
93 | * Add a listener that is notified immediately before the command |
||
94 | * factory creates commands from a commandFile instance. This |
||
95 | * listener can use this opportunity to do more setup for the commandFile, |
||
96 | * and so on. |
||
97 | * |
||
98 | * @param CommandCreationListenerInterface $listener |
||
99 | */ |
||
100 | public function addListener(CommandCreationListenerInterface $listener) |
||
105 | |||
106 | /** |
||
107 | * Add a listener that's just a simple 'callable'. |
||
108 | * @param callable $listener |
||
109 | */ |
||
110 | public function addListernerCallback(callable $listener) |
||
115 | |||
116 | /** |
||
117 | * Call all command creation listeners |
||
118 | * |
||
119 | * @param object $commandFileInstance |
||
120 | */ |
||
121 | protected function notify($commandFileInstance) |
||
127 | |||
128 | public function addAutomaticOptionProvider(AutomaticOptionsProviderInterface $optionsProvider) |
||
132 | |||
133 | public function addCommandInfoAlterer(CommandInfoAltererInterface $alterer) |
||
137 | |||
138 | /** |
||
139 | * n.b. This registers all hooks from the commandfile instance as a side-effect. |
||
140 | */ |
||
141 | public function createCommandsFromClass($commandFileInstance, $includeAllPublicMethods = null) |
||
152 | |||
153 | public function getCommandInfoListFromClass($commandFileInstance) |
||
163 | |||
164 | protected function storeCommandInfoListInCache($commandFileInstance, $commandInfoList) |
||
177 | |||
178 | /** |
||
179 | * Get the command info list from the cache |
||
180 | * |
||
181 | * @param mixed $commandFileInstance |
||
182 | * @return array |
||
183 | */ |
||
184 | protected function getCommandInfoListFromCache($commandFileInstance) |
||
204 | |||
205 | /** |
||
206 | * Check to see if this factory has a cache datastore. |
||
207 | * @return boolean |
||
208 | */ |
||
209 | public function hasDataStore() |
||
213 | |||
214 | /** |
||
215 | * Set a cache datastore for this factory. Any object with 'set' and |
||
216 | * 'get' methods is acceptable. The key is the classname being cached, |
||
217 | * and the value is a nested associative array of strings. |
||
218 | * |
||
219 | * TODO: Typehint this to SimpleCacheInterface |
||
220 | * |
||
221 | * This is not done currently to allow clients to use a generic cache |
||
222 | * store that does not itself depend on the annotated-command library. |
||
223 | * |
||
224 | * @param Mixed $dataStore |
||
225 | * @return type |
||
226 | */ |
||
227 | public function setDataStore($dataStore) |
||
235 | |||
236 | /** |
||
237 | * Get the data store attached to this factory. |
||
238 | */ |
||
239 | public function getDataStore() |
||
243 | |||
244 | protected function createCommandInfoListFromClass($classNameOrInstance, $cachedCommandInfoList) |
||
270 | |||
271 | public function createCommandInfo($classNameOrInstance, $commandMethodName) |
||
275 | |||
276 | public function createCommandsFromClassInfo($commandInfoList, $commandFileInstance, $includeAllPublicMethods = null) |
||
290 | |||
291 | public function createSelectedCommandsFromClassInfo($commandInfoList, $commandFileInstance, callable $commandSelector) |
||
301 | |||
302 | protected function filterCommandInfoList($commandInfoList, callable $commandSelector) |
||
306 | |||
307 | public static function isCommandOrHookMethod($commandInfo, $includeAllPublicMethods) |
||
311 | |||
312 | public static function isHookMethod($commandInfo) |
||
316 | |||
317 | public static function isCommandMethod($commandInfo, $includeAllPublicMethods) |
||
340 | |||
341 | public function registerCommandHooksFromClassInfo($commandInfoList, $commandFileInstance) |
||
349 | |||
350 | /** |
||
351 | * Register a command hook given the CommandInfo for a method. |
||
352 | * |
||
353 | * The hook format is: |
||
354 | * |
||
355 | * @hook type name type |
||
356 | * |
||
357 | * For example, the pre-validate hook for the core:init command is: |
||
358 | * |
||
359 | * @hook pre-validate core:init |
||
360 | * |
||
361 | * If no command name is provided, then this hook will affect every |
||
362 | * command that is defined in the same file. |
||
363 | * |
||
364 | * If no hook is provided, then we will presume that ALTER_RESULT |
||
365 | * is intended. |
||
366 | * |
||
367 | * @param CommandInfo $commandInfo Information about the command hook method. |
||
368 | * @param object $commandFileInstance An instance of the CommandFile class. |
||
369 | */ |
||
370 | public function registerCommandHook(CommandInfo $commandInfo, $commandFileInstance) |
||
391 | |||
392 | protected function getNthWord($string, $n, $default = '', $delimiter = ' ') |
||
400 | |||
401 | public function createCommand(CommandInfo $commandInfo, $commandFileInstance) |
||
417 | |||
418 | /** |
||
419 | * Give plugins an opportunity to update the commandInfo |
||
420 | */ |
||
421 | public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance) |
||
427 | |||
428 | /** |
||
429 | * Get the options that are implied by annotations, e.g. @fields implies |
||
430 | * that there should be a --fields and a --format option. |
||
431 | * |
||
432 | * @return InputOption[] |
||
433 | */ |
||
434 | public function callAutomaticOptionsProviders(CommandInfo $commandInfo) |
||
442 | |||
443 | /** |
||
444 | * Get the options that are implied by annotations, e.g. @fields implies |
||
445 | * that there should be a --fields and a --format option. |
||
446 | * |
||
447 | * @return InputOption[] |
||
448 | */ |
||
449 | public function automaticOptions(CommandInfo $commandInfo) |
||
461 | } |
||
462 |