Complex classes like AnnotatedCommand 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 AnnotatedCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class AnnotatedCommand extends Command |
||
| 30 | { |
||
| 31 | protected $commandCallback; |
||
| 32 | protected $commandProcessor; |
||
| 33 | protected $annotationData; |
||
| 34 | protected $usesInputInterface; |
||
| 35 | protected $usesOutputInterface; |
||
| 36 | protected $returnType; |
||
| 37 | |||
| 38 | public function __construct($name = null) |
||
| 39 | { |
||
| 40 | $commandInfo = false; |
||
| 41 | |||
| 42 | // If this is a subclass of AnnotatedCommand, check to see |
||
| 43 | // if the 'execute' method is annotated. We could do this |
||
| 44 | // unconditionally; it is a performance optimization to skip |
||
| 45 | // checking the annotations if $this is an instance of |
||
| 46 | // AnnotatedCommand. Alternately, we break out a new subclass. |
||
| 47 | // The command factory instantiates the subclass. |
||
| 48 | if (get_class($this) != 'Consolidation\AnnotatedCommand\AnnotatedCommand') { |
||
| 49 | $commandInfo = new CommandInfo($this, 'execute'); |
||
| 50 | if (!isset($name)) { |
||
| 51 | $name = $commandInfo->getName(); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | parent::__construct($name); |
||
| 55 | if ($commandInfo && $commandInfo->hasAnnotation('command')) { |
||
| 56 | $this->setCommandInfo($commandInfo); |
||
| 57 | $this->setCommandOptions($commandInfo); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | public function setCommandCallback($commandCallback) |
||
| 62 | { |
||
| 63 | $this->commandCallback = $commandCallback; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function setCommandProcessor($commandProcessor) |
||
| 67 | { |
||
| 68 | $this->commandProcessor = $commandProcessor; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function commandProcessor() |
||
| 72 | { |
||
| 73 | // If someone is using an AnnotatedCommand, and is NOT getting |
||
| 74 | // it from an AnnotatedCommandFactory OR not correctly injecting |
||
| 75 | // a command processor via setCommandProcessor() (ideally via the |
||
| 76 | // DI container), then we'll just give each annotated command its |
||
| 77 | // own command processor. This is not ideal; preferably, there would |
||
| 78 | // only be one instance of the command processor in the application. |
||
| 79 | if (!isset($this->commandProcessor)) { |
||
| 80 | $this->commandProcessor = new CommandProcessor(new HookManager()); |
||
| 81 | } |
||
| 82 | return $this->commandProcessor; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function getReturnType() |
||
| 86 | { |
||
| 87 | return $this->returnType; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function setReturnType($returnType) |
||
| 91 | { |
||
| 92 | $this->returnType = $returnType; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function setAnnotationData($annotationData) |
||
| 96 | { |
||
| 97 | $this->annotationData = $annotationData; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function setCommandInfo($commandInfo) |
||
| 101 | { |
||
| 102 | $this->setDescription($commandInfo->getDescription()); |
||
| 103 | $this->setHelp($commandInfo->getHelp()); |
||
| 104 | $this->setAliases($commandInfo->getAliases()); |
||
| 105 | $this->setAnnotationData($commandInfo->getAnnotationsForCommand()); |
||
| 106 | foreach ($commandInfo->getExampleUsages() as $usage => $description) { |
||
| 107 | // Symfony Console does not support attaching a description to a usage |
||
| 108 | $this->addUsage($usage); |
||
| 109 | } |
||
| 110 | $this->setCommandArguments($commandInfo); |
||
| 111 | $this->setReturnType($commandInfo->getReturnType()); |
||
| 112 | } |
||
| 113 | |||
| 114 | protected function setCommandArguments($commandInfo) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Check whether the first parameter is an InputInterface. |
||
| 123 | */ |
||
| 124 | protected function checkUsesInputInterface($params) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Determine whether this command wants to get its inputs |
||
| 132 | * via an InputInterface or via its command parameters |
||
| 133 | */ |
||
| 134 | protected function setUsesInputInterface($commandInfo) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Determine whether this command wants to send its output directly |
||
| 142 | * to the provided OutputInterface, or whether it will returned |
||
| 143 | * structured output to be processed by the command processor. |
||
| 144 | */ |
||
| 145 | protected function setUsesOutputInterface($commandInfo) |
||
| 153 | |||
| 154 | protected function setCommandArgumentsFromParameters($commandInfo) |
||
| 164 | |||
| 165 | protected function getCommandArgumentMode($hasDefault, $defaultValue) |
||
| 175 | |||
| 176 | public function setCommandOptions($commandInfo, $automaticOptions = []) |
||
| 177 | { |
||
| 178 | $explicitOptions = $this->explicitOptions($commandInfo); |
||
| 179 | |||
| 180 | $this->addOptions($explicitOptions + $automaticOptions, $automaticOptions); |
||
| 181 | } |
||
| 182 | |||
| 183 | protected function addOptions($inputOptions, $automaticOptions) |
||
| 184 | { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the options that are explicitly defined, e.g. via |
||
| 223 | * @option annotations, or via $options = ['someoption' => 'defaultvalue'] |
||
| 224 | * in the command method parameter list. |
||
| 225 | * |
||
| 226 | * @return InputOption[] |
||
| 227 | */ |
||
| 228 | protected function explicitOptions($commandInfo) |
||
| 253 | |||
| 254 | protected function getArgsWithPassThrough($input) |
||
| 268 | |||
| 269 | protected function getArgsAndOptions($input) |
||
| 279 | |||
| 280 | protected function appendPassThroughArgs($input, $args) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Returns all of the hook names that may be called for this command. |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | protected function getNames() |
||
| 308 | |||
| 309 | protected function getNamesUsingCommands() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | */ |
||
| 320 | protected function interact(InputInterface $input, OutputInterface $output) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 354 | |||
| 355 | public function processResults(InputInterface $input, OutputInterface $output, $results) |
||
| 375 | } |
||
| 376 |