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) |
||
| 161 | |||
| 162 | 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) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Check to see if this factory has a cache datastore. |
||
| 202 | * @return boolean |
||
| 203 | */ |
||
| 204 | public function hasDataStore() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set a cache datastore for this factory. Any object with 'set' and |
||
| 211 | * 'get' methods is acceptable. The key is the classname being cached, |
||
| 212 | * and the value is a nested associative array of strings. |
||
| 213 | * |
||
| 214 | * TODO: Typehint this to SimpleCacheInterface |
||
| 215 | * |
||
| 216 | * This is not done currently to allow clients to use a generic cache |
||
| 217 | * store that does not itself depend on the annotated-command library. |
||
| 218 | * |
||
| 219 | * @param Mixed $dataStore |
||
| 220 | * @return type |
||
| 221 | */ |
||
| 222 | public function setDataStore($dataStore) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get the data store attached to this factory. |
||
| 233 | */ |
||
| 234 | public function getDataStore() |
||
| 238 | |||
| 239 | protected function createCommandInfoListFromClass($classNameOrInstance, $cachedCommandInfoList) |
||
| 261 | |||
| 262 | public function createCommandInfo($classNameOrInstance, $commandMethodName) |
||
| 266 | |||
| 267 | public function createCommandsFromClassInfo($commandInfoList, $commandFileInstance, $includeAllPublicMethods = null) |
||
| 281 | |||
| 282 | public function createSelectedCommandsFromClassInfo($commandInfoList, $commandFileInstance, callable $commandSelector) |
||
| 295 | |||
| 296 | public static function isCommandMethod($commandInfo, $includeAllPublicMethods) |
||
| 314 | |||
| 315 | public function registerCommandHooksFromClassInfo($commandInfoList, $commandFileInstance) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Register a command hook given the CommandInfo for a method. |
||
| 326 | * |
||
| 327 | * The hook format is: |
||
| 328 | * |
||
| 329 | * @hook type name type |
||
| 330 | * |
||
| 331 | * For example, the pre-validate hook for the core:init command is: |
||
| 332 | * |
||
| 333 | * @hook pre-validate core:init |
||
| 334 | * |
||
| 335 | * If no command name is provided, then this hook will affect every |
||
| 336 | * command that is defined in the same file. |
||
| 337 | * |
||
| 338 | * If no hook is provided, then we will presume that ALTER_RESULT |
||
| 339 | * is intended. |
||
| 340 | * |
||
| 341 | * @param CommandInfo $commandInfo Information about the command hook method. |
||
| 342 | * @param object $commandFileInstance An instance of the CommandFile class. |
||
| 343 | */ |
||
| 344 | public function registerCommandHook(CommandInfo $commandInfo, $commandFileInstance) |
||
| 365 | |||
| 366 | protected function getNthWord($string, $n, $default = '', $delimiter = ' ') |
||
| 374 | |||
| 375 | public function createCommand(CommandInfo $commandInfo, $commandFileInstance) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Give plugins an opportunity to update the commandInfo |
||
| 394 | */ |
||
| 395 | public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance) |
||
| 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 callAutomaticOptionsProviders(CommandInfo $commandInfo) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get the options that are implied by annotations, e.g. @fields implies |
||
| 419 | * that there should be a --fields and a --format option. |
||
| 420 | * |
||
| 421 | * @return InputOption[] |
||
| 422 | */ |
||
| 423 | public function automaticOptions(CommandInfo $commandInfo) |
||
| 435 | } |
||
| 436 |