Complex classes like CommandInfo 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 CommandInfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class CommandInfo |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var \ReflectionMethod |
||
| 22 | */ |
||
| 23 | protected $reflection; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var boolean |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $docBlockIsParsed; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $description = ''; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $help = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DefaultsWithDescriptions |
||
| 48 | */ |
||
| 49 | protected $options; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var DefaultsWithDescriptions |
||
| 53 | */ |
||
| 54 | protected $arguments; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $exampleUsage = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $topics = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var AnnotationData |
||
| 68 | */ |
||
| 69 | protected $otherAnnotations; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $aliases = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $methodName; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $returnType; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $optionParamName; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Create a new CommandInfo class for a particular method of a class. |
||
| 93 | * |
||
| 94 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
| 95 | * instance of it. |
||
| 96 | * @param string $methodName The name of the method to get info about. |
||
| 97 | */ |
||
| 98 | public function __construct($classNameOrInstance, $methodName) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Recover the method name provided to the constructor. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getMethodName() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return the primary name for this command. |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getName() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set the primary name for this command. |
||
| 138 | * |
||
| 139 | * @param string $name |
||
| 140 | */ |
||
| 141 | public function setName($name) |
||
| 146 | |||
| 147 | public function getReturnType() |
||
| 152 | |||
| 153 | public function setReturnType($returnType) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get any annotations included in the docblock comment for the |
||
| 161 | * implementation method of this command that are not already |
||
| 162 | * handled by the primary methods of this class. |
||
| 163 | * |
||
| 164 | * @return AnnotationData |
||
| 165 | */ |
||
| 166 | public function getRawAnnotations() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get any annotations included in the docblock comment, |
||
| 174 | * also including default values such as @command. We add |
||
| 175 | * in the default @command annotation late, and only in a |
||
| 176 | * copy of the annotation data because we use the existance |
||
| 177 | * of a @command to indicate that this CommandInfo is |
||
| 178 | * a command, and not a hook or anything else. |
||
| 179 | * |
||
| 180 | * @return AnnotationData |
||
| 181 | */ |
||
| 182 | public function getAnnotations() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Return a specific named annotation for this command. |
||
| 194 | * |
||
| 195 | * @param string $annotation The name of the annotation. |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getAnnotation($annotation) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Check to see if the specified annotation exists for this command. |
||
| 209 | * |
||
| 210 | * @param string $annotation The name of the annotation. |
||
| 211 | * @return boolean |
||
| 212 | */ |
||
| 213 | public function hasAnnotation($annotation) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Save any tag that we do not explicitly recognize in the |
||
| 221 | * 'otherAnnotations' map. |
||
| 222 | */ |
||
| 223 | public function addAnnotation($name, $content) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Remove an annotation that was previoudly set. |
||
| 230 | */ |
||
| 231 | public function removeAnnotation($name) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get the synopsis of the command (~first line). |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getDescription() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set the command description. |
||
| 249 | * |
||
| 250 | * @param string $description The description to set. |
||
| 251 | */ |
||
| 252 | public function setDescription($description) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the help text of the command (the description) |
||
| 260 | */ |
||
| 261 | public function getHelp() |
||
| 266 | /** |
||
| 267 | * Set the help text for this command. |
||
| 268 | * |
||
| 269 | * @param string $help The help text. |
||
| 270 | */ |
||
| 271 | public function setHelp($help) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Return the list of aliases for this command. |
||
| 279 | * @return string[] |
||
| 280 | */ |
||
| 281 | public function getAliases() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set aliases that can be used in place of the command's primary name. |
||
| 289 | * |
||
| 290 | * @param string|string[] $aliases |
||
| 291 | */ |
||
| 292 | public function setAliases($aliases) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Return the examples for this command. This is @usage instead of |
||
| 303 | * @example because the later is defined by the phpdoc standard to |
||
| 304 | * be example method calls. |
||
| 305 | * |
||
| 306 | * @return string[] |
||
| 307 | */ |
||
| 308 | public function getExampleUsages() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Add an example usage for this command. |
||
| 316 | * |
||
| 317 | * @param string $usage An example of the command, including the command |
||
| 318 | * name and all of its example arguments and options. |
||
| 319 | * @param string $description An explanation of what the example does. |
||
| 320 | */ |
||
| 321 | public function setExampleUsage($usage, $description) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Return the topics for this command. |
||
| 329 | * |
||
| 330 | * @return string[] |
||
| 331 | */ |
||
| 332 | public function getTopics() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Add a topic |
||
| 340 | * |
||
| 341 | * @param string $topic The topic command |
||
| 342 | * @param string $description An explanation of what the topic is about. |
||
| 343 | */ |
||
| 344 | public function setTopic($topic, $description) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return the list of refleaction parameters. |
||
| 352 | * |
||
| 353 | * @return ReflectionParameter[] |
||
| 354 | */ |
||
| 355 | public function getParameters() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Descriptions of commandline arguements for this command. |
||
| 362 | * |
||
| 363 | * @return DefaultsWithDescriptions |
||
| 364 | */ |
||
| 365 | public function arguments() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Descriptions of commandline options for this command. |
||
| 372 | * |
||
| 373 | * @return DefaultsWithDescriptions |
||
| 374 | */ |
||
| 375 | public function options() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Return the name of the last parameter if it holds the options. |
||
| 382 | */ |
||
| 383 | public function optionParamName() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the inputOptions for the options associated with this CommandInfo |
||
| 390 | * object, e.g. via @option annotations, or from |
||
| 391 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
| 392 | * parameter list. |
||
| 393 | * |
||
| 394 | * @return InputOption[] |
||
| 395 | */ |
||
| 396 | public function inputOptions() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * An option might have a name such as 'silent|s'. In this |
||
| 424 | * instance, we will allow the @option or @default tag to |
||
| 425 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 426 | * instead of 'silent|s'). |
||
| 427 | * |
||
| 428 | * @param string $optionName |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function findMatchingOption($optionName) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $optionName |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | protected function findOptionAmongAlternatives($optionName) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $optionName |
||
| 466 | * @return string|null |
||
| 467 | */ |
||
| 468 | protected function findExistingOption($optionName) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Examine the parameters of the method for this command, and |
||
| 482 | * build a list of commandline arguements for them. |
||
| 483 | * |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | protected function determineAgumentClassifications() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Examine the provided parameter, and determine whether it |
||
| 502 | * is a parameter that will be filled in with a positional |
||
| 503 | * commandline argument. |
||
| 504 | */ |
||
| 505 | protected function addParameterToResult($result, $param) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Examine the parameters of the method for this command, and determine |
||
| 525 | * the disposition of the options from them. |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | protected function determineOptionsFromParameters() |
||
| 544 | |||
| 545 | protected function lastParameterName() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Helper; determine if an array is associative or not. An array |
||
| 557 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 558 | * from zero. All other arrays are considered to be associative. |
||
| 559 | * |
||
| 560 | * @param arrau $arr The array |
||
| 561 | * @return boolean |
||
| 562 | */ |
||
| 563 | protected function isAssoc($arr) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Convert from a method name to the corresponding command name. A |
||
| 573 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 574 | * become 'foo:bar-baz-boz'. |
||
| 575 | * |
||
| 576 | * @param string $camel method name. |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | protected function convertName($camel) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Parse the docBlock comment for this command, and set the |
||
| 589 | * fields of this class with the data thereby obtained. |
||
| 590 | */ |
||
| 591 | protected function parseDocBlock() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 603 | * convert the data into the last of these forms. |
||
| 604 | */ |
||
| 605 | protected static function convertListToCommaSeparated($text) |
||
| 609 | } |
||
| 610 |