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 | public function __construct() |
||
| 44 | |||
| 45 | public function setCommandProcessor(CommandProcessor $commandProcessor) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return CommandProcessor |
||
| 52 | */ |
||
| 53 | public function commandProcessor() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set the 'include all public methods flag'. If true (the default), then |
||
| 60 | * every public method of each commandFile will be used to create commands. |
||
| 61 | * If it is false, then only those public methods annotated with @command |
||
| 62 | * or @name (deprecated) will be used to create commands. |
||
| 63 | */ |
||
| 64 | public function setIncludeAllPublicMethods($includeAllPublicMethods) |
||
| 68 | |||
| 69 | public function getIncludeAllPublicMethods() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return HookManager |
||
| 76 | */ |
||
| 77 | public function hookManager() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Add a listener that is notified immediately before the command |
||
| 84 | * factory creates commands from a commandFile instance. This |
||
| 85 | * listener can use this opportunity to do more setup for the commandFile, |
||
| 86 | * and so on. |
||
| 87 | * |
||
| 88 | * @param CommandCreationListenerInterface $listener |
||
| 89 | */ |
||
| 90 | public function addListener(CommandCreationListenerInterface $listener) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Call all command creation listeners |
||
| 97 | * |
||
| 98 | * @param object $commandFileInstance |
||
| 99 | */ |
||
| 100 | protected function notify($commandFileInstance) |
||
| 106 | |||
| 107 | public function addAutomaticOptionProvider(AutomaticOptionsProviderInterface $optionsProvider) |
||
| 111 | |||
| 112 | public function addCommandInfoAlterer(CommandInfoAltererInterface $alterer) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * n.b. This registers all hooks from the commandfile instance as a side-effect. |
||
| 119 | */ |
||
| 120 | public function createCommandsFromClass($commandFileInstance, $includeAllPublicMethods = null) |
||
| 131 | |||
| 132 | public function getCommandInfoListFromClass($classNameOrInstance) |
||
| 152 | |||
| 153 | public function createCommandInfo($classNameOrInstance, $commandMethodName) |
||
| 157 | |||
| 158 | public function createCommandsFromClassInfo($commandInfoList, $commandFileInstance, $includeAllPublicMethods = null) |
||
| 172 | |||
| 173 | public function createSelectedCommandsFromClassInfo($commandInfoList, $commandFileInstance, callable $commandSelector) |
||
| 186 | |||
| 187 | public static function isCommandMethod($commandInfo, $includeAllPublicMethods) |
||
| 194 | |||
| 195 | public function registerCommandHooksFromClassInfo($commandInfoList, $commandFileInstance) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Register a command hook given the CommandInfo for a method. |
||
| 206 | * |
||
| 207 | * The hook format is: |
||
| 208 | * |
||
| 209 | * @hook type name type |
||
| 210 | * |
||
| 211 | * For example, the pre-validate hook for the core:init command is: |
||
| 212 | * |
||
| 213 | * @hook pre-validate core:init |
||
| 214 | * |
||
| 215 | * If no command name is provided, then this hook will affect every |
||
| 216 | * command that is defined in the same file. |
||
| 217 | * |
||
| 218 | * If no hook is provided, then we will presume that ALTER_RESULT |
||
| 219 | * is intended. |
||
| 220 | * |
||
| 221 | * @param CommandInfo $commandInfo Information about the command hook method. |
||
| 222 | * @param object $commandFileInstance An instance of the CommandFile class. |
||
| 223 | */ |
||
| 224 | public function registerCommandHook(CommandInfo $commandInfo, $commandFileInstance) |
||
| 245 | |||
| 246 | protected function getNthWord($string, $n, $default = '', $delimiter = ' ') |
||
| 254 | |||
| 255 | public function createCommand(CommandInfo $commandInfo, $commandFileInstance) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Give plugins an opportunity to update the commandInfo |
||
| 274 | */ |
||
| 275 | public function alterCommandInfo(CommandInfo $commandInfo, $commandFileInstance) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the options that are implied by annotations, e.g. @fields implies |
||
| 284 | * that there should be a --fields and a --format option. |
||
| 285 | * |
||
| 286 | * @return InputOption[] |
||
| 287 | */ |
||
| 288 | public function callAutomaticOptionsProviders(CommandInfo $commandInfo) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the options that are implied by annotations, e.g. @fields implies |
||
| 299 | * that there should be a --fields and a --format option. |
||
| 300 | * |
||
| 301 | * @return InputOption[] |
||
| 302 | */ |
||
| 303 | public function automaticOptions(CommandInfo $commandInfo) |
||
| 315 | } |
||
| 316 |