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 |
||
21 | class AnnotatedCommandFactory implements AutomaticOptionsProviderInterface |
||
22 | { |
||
23 | /** var CommandProcessor */ |
||
24 | protected $commandProcessor; |
||
25 | |||
26 | /** var CommandCreationListenerInterface[] */ |
||
27 | protected $listeners = []; |
||
28 | |||
29 | /** var AutomaticOptionsProvider[] */ |
||
30 | |||
31 | protected $automaticOptionsProviderList = []; |
||
32 | |||
33 | /** var boolean */ |
||
34 | protected $includeAllPublicMethods = true; |
||
35 | |||
36 | /** var CommandInfoAltererInterface */ |
||
37 | protected $commandInfoAlterers = []; |
||
38 | |||
39 | protected $dataStore; |
||
40 | |||
41 | public function __construct() |
||
46 | |||
47 | public function setCommandProcessor(CommandProcessor $commandProcessor) |
||
52 | |||
53 | /** |
||
54 | * @return CommandProcessor |
||
55 | */ |
||
56 | public function commandProcessor() |
||
60 | |||
61 | /** |
||
62 | * Set the 'include all public methods flag'. If true (the default), then |
||
63 | * every public method of each commandFile will be used to create commands. |
||
64 | * If it is false, then only those public methods annotated with @command |
||
65 | * or @name (deprecated) will be used to create commands. |
||
66 | */ |
||
67 | public function setIncludeAllPublicMethods($includeAllPublicMethods) |
||
72 | |||
73 | public function getIncludeAllPublicMethods() |
||
77 | |||
78 | /** |
||
79 | * @return HookManager |
||
80 | */ |
||
81 | public function hookManager() |
||
85 | |||
86 | /** |
||
87 | * Add a listener that is notified immediately before the command |
||
88 | * factory creates commands from a commandFile instance. This |
||
89 | * listener can use this opportunity to do more setup for the commandFile, |
||
90 | * and so on. |
||
91 | * |
||
92 | * @param CommandCreationListenerInterface $listener |
||
93 | */ |
||
94 | public function addListener(CommandCreationListenerInterface $listener) |
||
99 | |||
100 | /** |
||
101 | * Add a listener that's just a simple 'callable'. |
||
102 | * @param callable $listener |
||
103 | */ |
||
104 | public function addListernerCallback(callable $listener) |
||
109 | |||
110 | /** |
||
111 | * Call all command creation listeners |
||
112 | * |
||
113 | * @param object $commandFileInstance |
||
114 | */ |
||
115 | protected function notify($commandFileInstance) |
||
121 | |||
122 | public function addAutomaticOptionProvider(AutomaticOptionsProviderInterface $optionsProvider) |
||
126 | |||
127 | public function addCommandInfoAlterer(CommandInfoAltererInterface $alterer) |
||
131 | |||
132 | /** |
||
133 | * n.b. This registers all hooks from the commandfile instance as a side-effect. |
||
134 | */ |
||
135 | public function createCommandsFromClass($commandFileInstance, $includeAllPublicMethods = null) |
||
146 | |||
147 | public function getCommandInfoListFromClass($commandFileInstance) |
||
158 | |||
159 | protected function storeCommandInfoListInCache($commandFileInstance, $commandInfoList) |
||
174 | |||
175 | 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) |
||
232 | |||
233 | /** |
||
234 | * Get the data store attached to this factory. |
||
235 | */ |
||
236 | public function getDataStore() |
||
240 | |||
241 | protected function createCommandInfoListFromClass($classNameOrInstance) |
||
260 | |||
261 | public function createCommandInfo($classNameOrInstance, $commandMethodName) |
||
265 | |||
266 | public function createCommandsFromClassInfo($commandInfoList, $commandFileInstance, $includeAllPublicMethods = null) |
||
280 | |||
281 | public function createSelectedCommandsFromClassInfo($commandInfoList, $commandFileInstance, callable $commandSelector) |
||
294 | |||
295 | public static function isCommandMethod($commandInfo, $includeAllPublicMethods) |
||
313 | |||
314 | public function registerCommandHooksFromClassInfo($commandInfoList, $commandFileInstance) |
||
322 | |||
323 | /** |
||
324 | * Register a command hook given the CommandInfo for a method. |
||
325 | * |
||
326 | * The hook format is: |
||
327 | * |
||
328 | * @hook type name type |
||
329 | * |
||
330 | * For example, the pre-validate hook for the core:init command is: |
||
331 | * |
||
332 | * @hook pre-validate core:init |
||
333 | * |
||
334 | * If no command name is provided, then this hook will affect every |
||
335 | * command that is defined in the same file. |
||
336 | * |
||
337 | * If no hook is provided, then we will presume that ALTER_RESULT |
||
338 | * is intended. |
||
339 | * |
||
340 | * @param CommandInfo $commandInfo Information about the command hook method. |
||
341 | * @param object $commandFileInstance An instance of the CommandFile class. |
||
342 | */ |
||
343 | public function registerCommandHook(CommandInfo $commandInfo, $commandFileInstance) |
||
364 | |||
365 | protected function getNthWord($string, $n, $default = '', $delimiter = ' ') |
||
373 | |||
374 | public function createCommand(CommandInfo $commandInfo, $commandFileInstance) |
||
390 | |||
391 | /** |
||
392 | * Give plugins an opportunity to update the commandInfo |
||
393 | */ |
||
394 | public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance) |
||
400 | |||
401 | /** |
||
402 | * Get the options that are implied by annotations, e.g. @fields implies |
||
403 | * that there should be a --fields and a --format option. |
||
404 | * |
||
405 | * @return InputOption[] |
||
406 | */ |
||
407 | public function callAutomaticOptionsProviders(CommandInfo $commandInfo) |
||
415 | |||
416 | /** |
||
417 | * Get the options that are implied by annotations, e.g. @fields implies |
||
418 | * that there should be a --fields and a --format option. |
||
419 | * |
||
420 | * @return InputOption[] |
||
421 | */ |
||
422 | public function automaticOptions(CommandInfo $commandInfo) |
||
434 | } |
||
435 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.