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 |
||
24 | class AnnotatedCommandFactory implements AutomaticOptionsProviderInterface |
||
25 | { |
||
26 | /** var CommandProcessor */ |
||
27 | protected $commandProcessor; |
||
28 | |||
29 | /** var CommandCreationListenerInterface[] */ |
||
30 | protected $listeners = []; |
||
31 | |||
32 | /** var AutomaticOptionsProvider[] */ |
||
33 | protected $automaticOptionsProviderList = []; |
||
34 | |||
35 | /** var boolean */ |
||
36 | protected $includeAllPublicMethods = true; |
||
37 | |||
38 | /** var CommandInfoAltererInterface */ |
||
39 | protected $commandInfoAlterers = []; |
||
40 | |||
41 | /** var SimpleCacheInterface */ |
||
42 | protected $dataStore; |
||
43 | |||
44 | public function __construct() |
||
50 | |||
51 | public function setCommandProcessor(CommandProcessor $commandProcessor) |
||
56 | |||
57 | /** |
||
58 | * @return CommandProcessor |
||
59 | */ |
||
60 | public function commandProcessor() |
||
64 | |||
65 | /** |
||
66 | * Set the 'include all public methods flag'. If true (the default), then |
||
67 | * every public method of each commandFile will be used to create commands. |
||
68 | * If it is false, then only those public methods annotated with @command |
||
69 | * or @name (deprecated) will be used to create commands. |
||
70 | */ |
||
71 | public function setIncludeAllPublicMethods($includeAllPublicMethods) |
||
76 | |||
77 | public function getIncludeAllPublicMethods() |
||
81 | |||
82 | /** |
||
83 | * @return HookManager |
||
84 | */ |
||
85 | public function hookManager() |
||
89 | |||
90 | /** |
||
91 | * Add a listener that is notified immediately before the command |
||
92 | * factory creates commands from a commandFile instance. This |
||
93 | * listener can use this opportunity to do more setup for the commandFile, |
||
94 | * and so on. |
||
95 | * |
||
96 | * @param CommandCreationListenerInterface $listener |
||
97 | */ |
||
98 | public function addListener(CommandCreationListenerInterface $listener) |
||
103 | |||
104 | /** |
||
105 | * Add a listener that's just a simple 'callable'. |
||
106 | * @param callable $listener |
||
107 | */ |
||
108 | public function addListernerCallback(callable $listener) |
||
113 | |||
114 | /** |
||
115 | * Call all command creation listeners |
||
116 | * |
||
117 | * @param object $commandFileInstance |
||
118 | */ |
||
119 | protected function notify($commandFileInstance) |
||
125 | |||
126 | public function addAutomaticOptionProvider(AutomaticOptionsProviderInterface $optionsProvider) |
||
130 | |||
131 | public function addCommandInfoAlterer(CommandInfoAltererInterface $alterer) |
||
135 | |||
136 | /** |
||
137 | * n.b. This registers all hooks from the commandfile instance as a side-effect. |
||
138 | */ |
||
139 | public function createCommandsFromClass($commandFileInstance, $includeAllPublicMethods = null) |
||
150 | |||
151 | public function getCommandInfoListFromClass($commandFileInstance) |
||
162 | |||
163 | protected function storeCommandInfoListInCache($commandFileInstance, $commandInfoList) |
||
178 | |||
179 | /** |
||
180 | * Get the command info list from the cache |
||
181 | * |
||
182 | * @param mixed $commandFileInstance |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function getCommandInfoListFromCache($commandFileInstance) |
||
200 | |||
201 | /** |
||
202 | * Check to see if this factory has a cache datastore. |
||
203 | * @return boolean |
||
204 | */ |
||
205 | public function hasDataStore() |
||
209 | |||
210 | /** |
||
211 | * Set a cache datastore for this factory. Any object with 'set' and |
||
212 | * 'get' methods is acceptable. The key is the classname being cached, |
||
213 | * and the value is a nested associative array of strings. |
||
214 | * |
||
215 | * TODO: Typehint this to SimpleCacheInterface |
||
216 | * |
||
217 | * This is not done currently to allow clients to use a generic cache |
||
218 | * store that does not itself depend on the annotated-command library. |
||
219 | * |
||
220 | * @param Mixed $dataStore |
||
221 | * @return type |
||
222 | */ |
||
223 | public function setDataStore($dataStore) |
||
231 | |||
232 | /** |
||
233 | * Get the data store attached to this factory. |
||
234 | */ |
||
235 | public function getDataStore() |
||
239 | |||
240 | protected function createCommandInfoListFromClass($classNameOrInstance) |
||
259 | |||
260 | public function createCommandInfo($classNameOrInstance, $commandMethodName) |
||
264 | |||
265 | public function createCommandsFromClassInfo($commandInfoList, $commandFileInstance, $includeAllPublicMethods = null) |
||
279 | |||
280 | public function createSelectedCommandsFromClassInfo($commandInfoList, $commandFileInstance, callable $commandSelector) |
||
293 | |||
294 | public static function isCommandMethod($commandInfo, $includeAllPublicMethods) |
||
312 | |||
313 | public function registerCommandHooksFromClassInfo($commandInfoList, $commandFileInstance) |
||
321 | |||
322 | /** |
||
323 | * Register a command hook given the CommandInfo for a method. |
||
324 | * |
||
325 | * The hook format is: |
||
326 | * |
||
327 | * @hook type name type |
||
328 | * |
||
329 | * For example, the pre-validate hook for the core:init command is: |
||
330 | * |
||
331 | * @hook pre-validate core:init |
||
332 | * |
||
333 | * If no command name is provided, then this hook will affect every |
||
334 | * command that is defined in the same file. |
||
335 | * |
||
336 | * If no hook is provided, then we will presume that ALTER_RESULT |
||
337 | * is intended. |
||
338 | * |
||
339 | * @param CommandInfo $commandInfo Information about the command hook method. |
||
340 | * @param object $commandFileInstance An instance of the CommandFile class. |
||
341 | */ |
||
342 | public function registerCommandHook(CommandInfo $commandInfo, $commandFileInstance) |
||
363 | |||
364 | protected function getNthWord($string, $n, $default = '', $delimiter = ' ') |
||
372 | |||
373 | public function createCommand(CommandInfo $commandInfo, $commandFileInstance) |
||
389 | |||
390 | /** |
||
391 | * Give plugins an opportunity to update the commandInfo |
||
392 | */ |
||
393 | public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance) |
||
399 | |||
400 | /** |
||
401 | * Get the options that are implied by annotations, e.g. @fields implies |
||
402 | * that there should be a --fields and a --format option. |
||
403 | * |
||
404 | * @return InputOption[] |
||
405 | */ |
||
406 | public function callAutomaticOptionsProviders(CommandInfo $commandInfo) |
||
414 | |||
415 | /** |
||
416 | * Get the options that are implied by annotations, e.g. @fields implies |
||
417 | * that there should be a --fields and a --format option. |
||
418 | * |
||
419 | * @return InputOption[] |
||
420 | */ |
||
421 | public function automaticOptions(CommandInfo $commandInfo) |
||
433 | } |
||
434 |