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 |
||
| 30 | class AnnotatedCommand extends Command implements HelpDocumentAlter |
||
| 31 | { |
||
| 32 | protected $commandCallback; |
||
| 33 | protected $commandProcessor; |
||
| 34 | protected $annotationData; |
||
| 35 | protected $examples = []; |
||
| 36 | protected $topics = []; |
||
| 37 | protected $usesInputInterface; |
||
| 38 | protected $usesOutputInterface; |
||
| 39 | protected $returnType; |
||
| 40 | |||
| 41 | public function __construct($name = null) |
||
| 63 | |||
| 64 | public function setCommandCallback($commandCallback) |
||
| 69 | |||
| 70 | public function setCommandProcessor($commandProcessor) |
||
| 75 | |||
| 76 | public function commandProcessor() |
||
| 89 | |||
| 90 | public function getReturnType() |
||
| 94 | |||
| 95 | public function setReturnType($returnType) |
||
| 100 | |||
| 101 | public function getAnnotationData() |
||
| 105 | |||
| 106 | public function setAnnotationData($annotationData) |
||
| 111 | |||
| 112 | public function getTopics() |
||
| 116 | |||
| 117 | public function setTopics($topics) |
||
| 122 | |||
| 123 | public function setCommandInfo($commandInfo) |
||
| 137 | |||
| 138 | protected function addUsageOrExample($usage, $description) |
||
| 145 | |||
| 146 | public function helpAlter(\DomDocument $originalDom) |
||
| 147 | { |
||
| 148 | $dom = new \DOMDocument('1.0', 'UTF-8'); |
||
| 149 | $dom->appendChild($commandXML = $dom->createElement('command')); |
||
| 150 | $commandXML->setAttribute('id', $this->getName()); |
||
| 151 | $commandXML->setAttribute('name', $this->getName()); |
||
| 152 | |||
| 153 | // Get the original <command> element and its top-level elements. |
||
| 154 | $originalCommandXML = $this->getSingleElementByTagName($dom, $originalDom, 'command'); |
||
| 155 | $originalUsagesXML = $this->getSingleElementByTagName($dom, $originalCommandXML, 'usages'); |
||
| 156 | $originalDescriptionXML = $this->getSingleElementByTagName($dom, $originalCommandXML, 'description'); |
||
| 157 | $originalHelpXML = $this->getSingleElementByTagName($dom, $originalCommandXML, 'help'); |
||
| 158 | $originalArgumentsXML = $this->getSingleElementByTagName($dom, $originalCommandXML, 'arguments'); |
||
| 159 | $originalOptionsXML = $this->getSingleElementByTagName($dom, $originalCommandXML, 'options'); |
||
| 160 | |||
| 161 | // Keep only the first of the <usage> elements |
||
| 162 | $newUsagesXML = $dom->createElement('usages'); |
||
| 163 | $firstUsageXML = $this->getSingleElementByTagName($dom, $originalUsagesXML, 'usage'); |
||
| 164 | $newUsagesXML->appendChild($firstUsageXML); |
||
| 165 | |||
| 166 | // Create our own <example> elements |
||
| 167 | $newExamplesXML = $dom->createElement('examples'); |
||
| 168 | foreach ($this->examples as $usage => $description) { |
||
| 169 | $newExamplesXML->appendChild($exampleXML = $dom->createElement('example')); |
||
| 170 | $exampleXML->appendChild($usageXML = $dom->createElement('usage', $usage)); |
||
| 171 | $exampleXML->appendChild($descriptionXML = $dom->createElement('description', $description)); |
||
| 172 | } |
||
| 173 | |||
| 174 | // Create our own <alias> elements |
||
| 175 | $newAliasesXML = $dom->createElement('aliases'); |
||
| 176 | foreach ($this->getAliases() as $alias) { |
||
| 177 | $newAliasesXML->appendChild($dom->createElement('alias', $alias)); |
||
| 178 | } |
||
| 179 | |||
| 180 | // Create our own <topic> elements |
||
| 181 | $newTopicsXML = $dom->createElement('topics'); |
||
| 182 | foreach ($this->getTopics() as $topic => $description) { |
||
| 183 | $newTopicsXML->appendChild($topicXML = $dom->createElement('topic')); |
||
| 184 | $topicXML->appendChild($nameXML = $dom->createElement('name', $topic)); |
||
| 185 | $topicXML->appendChild($descriptionXML = $dom->createElement('description', $description)); |
||
| 186 | } |
||
| 187 | |||
| 188 | // Place the different elements into the <command> element in the desired order |
||
| 189 | $commandXML->appendChild($newUsagesXML); |
||
| 190 | $commandXML->appendChild($newExamplesXML); |
||
| 191 | $commandXML->appendChild($originalDescriptionXML); |
||
| 192 | $commandXML->appendChild($originalArgumentsXML); |
||
| 193 | $commandXML->appendChild($originalOptionsXML); |
||
| 194 | $commandXML->appendChild($originalHelpXML); |
||
| 195 | $commandXML->appendChild($newAliasesXML); |
||
| 196 | $commandXML->appendChild($newTopicsXML); |
||
| 197 | |||
| 198 | return $dom; |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function getSingleElementByTagName($dom, $parent, $tagName) |
||
| 211 | |||
| 212 | protected function setCommandArguments($commandInfo) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Check whether the first parameter is an InputInterface. |
||
| 222 | */ |
||
| 223 | protected function checkUsesInputInterface($params) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Determine whether this command wants to get its inputs |
||
| 231 | * via an InputInterface or via its command parameters |
||
| 232 | */ |
||
| 233 | protected function setUsesInputInterface($commandInfo) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Determine whether this command wants to send its output directly |
||
| 242 | * to the provided OutputInterface, or whether it will returned |
||
| 243 | * structured output to be processed by the command processor. |
||
| 244 | */ |
||
| 245 | protected function setUsesOutputInterface($commandInfo) |
||
| 254 | |||
| 255 | protected function setCommandArgumentsFromParameters($commandInfo) |
||
| 266 | |||
| 267 | protected function getCommandArgumentMode($hasDefault, $defaultValue) |
||
| 277 | |||
| 278 | public function setCommandOptions($commandInfo, $automaticOptions = []) |
||
| 285 | |||
| 286 | public function addOptions($inputOptions, $automaticOptions = []) |
||
| 298 | |||
| 299 | protected static function inputOptionSetDescription($inputOption, $description) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns all of the hook names that may be called for this command. |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | public function getNames() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Add any options to this command that are defined by hook implementations |
||
| 338 | */ |
||
| 339 | public function optionsHook() |
||
| 347 | |||
| 348 | public function optionsHookForHookAnnotations($commandInfoList) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | protected function interact(InputInterface $input, OutputInterface $output) |
||
| 373 | |||
| 374 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * This function is available for use by a class that may |
||
| 397 | * wish to extend this class rather than use annotations to |
||
| 398 | * define commands. Using this technique does allow for the |
||
| 399 | * use of annotations to define hooks. |
||
| 400 | */ |
||
| 401 | public function processResults(InputInterface $input, OutputInterface $output, $results) |
||
| 418 | |||
| 419 | protected function createCommandData(InputInterface $input, OutputInterface $output) |
||
| 434 | } |
||
| 435 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: