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 AnnotationData |
||
| 63 | */ |
||
| 64 | protected $otherAnnotations; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $aliases = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $methodName; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $returnType; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $optionParamName; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Create a new CommandInfo class for a particular method of a class. |
||
| 88 | * |
||
| 89 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
| 90 | * instance of it. |
||
| 91 | * @param string $methodName The name of the method to get info about. |
||
| 92 | */ |
||
| 93 | public function __construct($classNameOrInstance, $methodName) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Recover the method name provided to the constructor. |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getMethodName() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Return the primary name for this command. |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getName() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Set the primary name for this command. |
||
| 133 | * |
||
| 134 | * @param string $name |
||
| 135 | */ |
||
| 136 | public function setName($name) |
||
| 141 | |||
| 142 | public function getReturnType() |
||
| 147 | |||
| 148 | public function setReturnType($returnType) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get any annotations included in the docblock comment for the |
||
| 156 | * implementation method of this command that are not already |
||
| 157 | * handled by the primary methods of this class. |
||
| 158 | * |
||
| 159 | * @return AnnotationData |
||
| 160 | */ |
||
| 161 | public function getRawAnnotations() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get any annotations included in the docblock comment, |
||
| 169 | * also including default values such as @command. We add |
||
| 170 | * in the default @command annotation late, and only in a |
||
| 171 | * copy of the annotation data because we use the existance |
||
| 172 | * of a @command to indicate that this CommandInfo is |
||
| 173 | * a command, and not a hook or anything else. |
||
| 174 | * |
||
| 175 | * @return AnnotationData |
||
| 176 | */ |
||
| 177 | public function getAnnotations() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return a specific named annotation for this command. |
||
| 189 | * |
||
| 190 | * @param string $annotation The name of the annotation. |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getAnnotation($annotation) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Check to see if the specified annotation exists for this command. |
||
| 204 | * |
||
| 205 | * @param string $annotation The name of the annotation. |
||
| 206 | * @return boolean |
||
| 207 | */ |
||
| 208 | public function hasAnnotation($annotation) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Save any tag that we do not explicitly recognize in the |
||
| 216 | * 'otherAnnotations' map. |
||
| 217 | */ |
||
| 218 | public function addAnnotation($name, $content) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Remove an annotation that was previoudly set. |
||
| 225 | */ |
||
| 226 | public function removeAnnotation($name) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get the synopsis of the command (~first line). |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getDescription() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Set the command description. |
||
| 244 | * |
||
| 245 | * @param string $description The description to set. |
||
| 246 | */ |
||
| 247 | public function setDescription($description) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the help text of the command (the description) |
||
| 255 | */ |
||
| 256 | public function getHelp() |
||
| 261 | /** |
||
| 262 | * Set the help text for this command. |
||
| 263 | * |
||
| 264 | * @param string $help The help text. |
||
| 265 | */ |
||
| 266 | public function setHelp($help) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Return the list of aliases for this command. |
||
| 274 | * @return string[] |
||
| 275 | */ |
||
| 276 | public function getAliases() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set aliases that can be used in place of the command's primary name. |
||
| 284 | * |
||
| 285 | * @param string|string[] $aliases |
||
| 286 | */ |
||
| 287 | public function setAliases($aliases) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Return the examples for this command. This is @usage instead of |
||
| 298 | * @example because the later is defined by the phpdoc standard to |
||
| 299 | * be example method calls. |
||
| 300 | * |
||
| 301 | * @return string[] |
||
| 302 | */ |
||
| 303 | public function getExampleUsages() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Add an example usage for this command. |
||
| 311 | * |
||
| 312 | * @param string $usage An example of the command, including the command |
||
| 313 | * name and all of its example arguments and options. |
||
| 314 | * @param string $description An explanation of what the example does. |
||
| 315 | */ |
||
| 316 | public function setExampleUsage($usage, $description) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return the topics for this command. |
||
| 324 | * |
||
| 325 | * @return string[] |
||
| 326 | */ |
||
| 327 | public function getTopics() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Return the list of refleaction parameters. |
||
| 338 | * |
||
| 339 | * @return ReflectionParameter[] |
||
| 340 | */ |
||
| 341 | public function getParameters() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Descriptions of commandline arguements for this command. |
||
| 348 | * |
||
| 349 | * @return DefaultsWithDescriptions |
||
| 350 | */ |
||
| 351 | public function arguments() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Descriptions of commandline options for this command. |
||
| 358 | * |
||
| 359 | * @return DefaultsWithDescriptions |
||
| 360 | */ |
||
| 361 | public function options() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Return the name of the last parameter if it holds the options. |
||
| 368 | */ |
||
| 369 | public function optionParamName() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get the inputOptions for the options associated with this CommandInfo |
||
| 376 | * object, e.g. via @option annotations, or from |
||
| 377 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
| 378 | * parameter list. |
||
| 379 | * |
||
| 380 | * @return InputOption[] |
||
| 381 | */ |
||
| 382 | public function inputOptions() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * An option might have a name such as 'silent|s'. In this |
||
| 410 | * instance, we will allow the @option or @default tag to |
||
| 411 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 412 | * instead of 'silent|s'). |
||
| 413 | * |
||
| 414 | * @param string $optionName |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function findMatchingOption($optionName) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $optionName |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | protected function findOptionAmongAlternatives($optionName) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $optionName |
||
| 452 | * @return string|null |
||
| 453 | */ |
||
| 454 | protected function findExistingOption($optionName) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Examine the parameters of the method for this command, and |
||
| 468 | * build a list of commandline arguements for them. |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | protected function determineAgumentClassifications() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Examine the provided parameter, and determine whether it |
||
| 488 | * is a parameter that will be filled in with a positional |
||
| 489 | * commandline argument. |
||
| 490 | */ |
||
| 491 | protected function addParameterToResult($result, $param) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Examine the parameters of the method for this command, and determine |
||
| 511 | * the disposition of the options from them. |
||
| 512 | * |
||
| 513 | * @return array |
||
| 514 | */ |
||
| 515 | protected function determineOptionsFromParameters() |
||
| 530 | |||
| 531 | protected function lastParameterName() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Helper; determine if an array is associative or not. An array |
||
| 543 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 544 | * from zero. All other arrays are considered to be associative. |
||
| 545 | * |
||
| 546 | * @param arrau $arr The array |
||
| 547 | * @return boolean |
||
| 548 | */ |
||
| 549 | protected function isAssoc($arr) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Convert from a method name to the corresponding command name. A |
||
| 559 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 560 | * become 'foo:bar-baz-boz'. |
||
| 561 | * |
||
| 562 | * @param string $camel method name. |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | protected function convertName($camel) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Parse the docBlock comment for this command, and set the |
||
| 575 | * fields of this class with the data thereby obtained. |
||
| 576 | */ |
||
| 577 | protected function parseDocBlock() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 589 | * convert the data into the last of these forms. |
||
| 590 | */ |
||
| 591 | protected static function convertListToCommaSeparated($text) |
||
| 595 | } |
||
| 596 |