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) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Check to see if this factory has a cache datastore. |
||
| 199 | * @return boolean |
||
| 200 | */ |
||
| 201 | public function hasDataStore() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set a cache datastore for this factory. Any object with 'set' and |
||
| 208 | * 'get' methods is acceptable. The key is the classname being cached, |
||
| 209 | * and the value is a nested associative array of strings. |
||
| 210 | * |
||
| 211 | * @param type $dataStore |
||
| 212 | * @return type |
||
| 213 | */ |
||
| 214 | public function setDataStore($dataStore) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the data store attached to this factory. |
||
| 221 | */ |
||
| 222 | public function getDataStore() |
||
| 226 | |||
| 227 | protected function createCommandInfoListFromClass($classNameOrInstance) |
||
| 246 | |||
| 247 | public function createCommandInfo($classNameOrInstance, $commandMethodName) |
||
| 251 | |||
| 252 | public function createCommandsFromClassInfo($commandInfoList, $commandFileInstance, $includeAllPublicMethods = null) |
||
| 266 | |||
| 267 | public function createSelectedCommandsFromClassInfo($commandInfoList, $commandFileInstance, callable $commandSelector) |
||
| 280 | |||
| 281 | public static function isCommandMethod($commandInfo, $includeAllPublicMethods) |
||
| 299 | |||
| 300 | public function registerCommandHooksFromClassInfo($commandInfoList, $commandFileInstance) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Register a command hook given the CommandInfo for a method. |
||
| 311 | * |
||
| 312 | * The hook format is: |
||
| 313 | * |
||
| 314 | * @hook type name type |
||
| 315 | * |
||
| 316 | * For example, the pre-validate hook for the core:init command is: |
||
| 317 | * |
||
| 318 | * @hook pre-validate core:init |
||
| 319 | * |
||
| 320 | * If no command name is provided, then this hook will affect every |
||
| 321 | * command that is defined in the same file. |
||
| 322 | * |
||
| 323 | * If no hook is provided, then we will presume that ALTER_RESULT |
||
| 324 | * is intended. |
||
| 325 | * |
||
| 326 | * @param CommandInfo $commandInfo Information about the command hook method. |
||
| 327 | * @param object $commandFileInstance An instance of the CommandFile class. |
||
| 328 | */ |
||
| 329 | public function registerCommandHook(CommandInfo $commandInfo, $commandFileInstance) |
||
| 350 | |||
| 351 | protected function getNthWord($string, $n, $default = '', $delimiter = ' ') |
||
| 359 | |||
| 360 | public function createCommand(CommandInfo $commandInfo, $commandFileInstance) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Give plugins an opportunity to update the commandInfo |
||
| 379 | */ |
||
| 380 | public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the options that are implied by annotations, e.g. @fields implies |
||
| 389 | * that there should be a --fields and a --format option. |
||
| 390 | * |
||
| 391 | * @return InputOption[] |
||
| 392 | */ |
||
| 393 | public function callAutomaticOptionsProviders(CommandInfo $commandInfo) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get the options that are implied by annotations, e.g. @fields implies |
||
| 404 | * that there should be a --fields and a --format option. |
||
| 405 | * |
||
| 406 | * @return InputOption[] |
||
| 407 | */ |
||
| 408 | public function automaticOptions(CommandInfo $commandInfo) |
||
| 420 | } |
||
| 421 |
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.