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 | * Serialization schema version. Incremented every time the serialization schema changes. |
||
| 22 | */ |
||
| 23 | const SERIALIZATION_SCHEMA_VERSION = 2; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var \ReflectionMethod |
||
| 27 | */ |
||
| 28 | protected $reflection; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var boolean |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $docBlockIsParsed = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $name; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $description = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $help = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var DefaultsWithDescriptions |
||
| 53 | */ |
||
| 54 | protected $options; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var DefaultsWithDescriptions |
||
| 58 | */ |
||
| 59 | protected $arguments; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $exampleUsage = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var AnnotationData |
||
| 68 | */ |
||
| 69 | protected $otherAnnotations; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $aliases = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var InputOption[] |
||
| 78 | */ |
||
| 79 | protected $inputOptions; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $methodName; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $returnType; |
||
| 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, or an array of cached data. |
||
| 96 | * @param string $methodName The name of the method to get info about. |
||
| 97 | * @param array $cache Cached data |
||
| 98 | * @deprecated Use CommandInfo::create() or CommandInfo::deserialize() |
||
| 99 | * instead. In the future, this constructor will be protected. |
||
| 100 | */ |
||
| 101 | public function __construct($classNameOrInstance, $methodName, $cache = []) |
||
| 118 | |||
| 119 | public static function create($classNameOrInstance, $methodName) |
||
| 123 | |||
| 124 | public static function deserialize($cache) |
||
| 129 | |||
| 130 | public function cachedFileIsModified($cache) |
||
| 135 | |||
| 136 | protected function constructFromClassAndMethod($classNameOrInstance, $methodName) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Recover the method name provided to the constructor. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getMethodName() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return the primary name for this command. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getName() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Set the primary name for this command. |
||
| 169 | * |
||
| 170 | * @param string $name |
||
| 171 | */ |
||
| 172 | public function setName($name) |
||
| 177 | |||
| 178 | public function getReturnType() |
||
| 183 | |||
| 184 | public function setReturnType($returnType) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get any annotations included in the docblock comment for the |
||
| 192 | * implementation method of this command that are not already |
||
| 193 | * handled by the primary methods of this class. |
||
| 194 | * |
||
| 195 | * @return AnnotationData |
||
| 196 | */ |
||
| 197 | public function getRawAnnotations() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Replace the annotation data. |
||
| 205 | */ |
||
| 206 | public function replaceRawAnnotations($annotationData) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get any annotations included in the docblock comment, |
||
| 214 | * also including default values such as @command. We add |
||
| 215 | * in the default @command annotation late, and only in a |
||
| 216 | * copy of the annotation data because we use the existance |
||
| 217 | * of a @command to indicate that this CommandInfo is |
||
| 218 | * a command, and not a hook or anything else. |
||
| 219 | * |
||
| 220 | * @return AnnotationData |
||
| 221 | */ |
||
| 222 | public function getAnnotations() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return a specific named annotation for this command. |
||
| 240 | * |
||
| 241 | * @param string $annotation The name of the annotation. |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getAnnotation($annotation) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Check to see if the specified annotation exists for this command. |
||
| 255 | * |
||
| 256 | * @param string $annotation The name of the annotation. |
||
| 257 | * @return boolean |
||
| 258 | */ |
||
| 259 | public function hasAnnotation($annotation) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Save any tag that we do not explicitly recognize in the |
||
| 267 | * 'otherAnnotations' map. |
||
| 268 | */ |
||
| 269 | public function addAnnotation($name, $content) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Remove an annotation that was previoudly set. |
||
| 276 | */ |
||
| 277 | public function removeAnnotation($name) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get the synopsis of the command (~first line). |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function getDescription() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set the command description. |
||
| 295 | * |
||
| 296 | * @param string $description The description to set. |
||
| 297 | */ |
||
| 298 | public function setDescription($description) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the help text of the command (the description) |
||
| 306 | */ |
||
| 307 | public function getHelp() |
||
| 312 | /** |
||
| 313 | * Set the help text for this command. |
||
| 314 | * |
||
| 315 | * @param string $help The help text. |
||
| 316 | */ |
||
| 317 | public function setHelp($help) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Return the list of aliases for this command. |
||
| 325 | * @return string[] |
||
| 326 | */ |
||
| 327 | public function getAliases() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set aliases that can be used in place of the command's primary name. |
||
| 335 | * |
||
| 336 | * @param string|string[] $aliases |
||
| 337 | */ |
||
| 338 | public function setAliases($aliases) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Return the examples for this command. This is @usage instead of |
||
| 349 | * @example because the later is defined by the phpdoc standard to |
||
| 350 | * be example method calls. |
||
| 351 | * |
||
| 352 | * @return string[] |
||
| 353 | */ |
||
| 354 | public function getExampleUsages() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Add an example usage for this command. |
||
| 362 | * |
||
| 363 | * @param string $usage An example of the command, including the command |
||
| 364 | * name and all of its example arguments and options. |
||
| 365 | * @param string $description An explanation of what the example does. |
||
| 366 | */ |
||
| 367 | public function setExampleUsage($usage, $description) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Overwrite all example usages |
||
| 375 | */ |
||
| 376 | public function replaceExampleUsages($usages) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return the topics for this command. |
||
| 384 | * |
||
| 385 | * @return string[] |
||
| 386 | */ |
||
| 387 | public function getTopics() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Return the list of refleaction parameters. |
||
| 398 | * |
||
| 399 | * @return ReflectionParameter[] |
||
| 400 | */ |
||
| 401 | public function getParameters() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Descriptions of commandline arguements for this command. |
||
| 408 | * |
||
| 409 | * @return DefaultsWithDescriptions |
||
| 410 | */ |
||
| 411 | public function arguments() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Descriptions of commandline options for this command. |
||
| 418 | * |
||
| 419 | * @return DefaultsWithDescriptions |
||
| 420 | */ |
||
| 421 | public function options() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get the inputOptions for the options associated with this CommandInfo |
||
| 428 | * object, e.g. via @option annotations, or from |
||
| 429 | * $options = ['someoption' => 'defaultvalue'] in the command method |
||
| 430 | * parameter list. |
||
| 431 | * |
||
| 432 | * @return InputOption[] |
||
| 433 | */ |
||
| 434 | public function inputOptions() |
||
| 441 | |||
| 442 | protected function createInputOptions() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * An option might have a name such as 'silent|s'. In this |
||
| 479 | * instance, we will allow the @option or @default tag to |
||
| 480 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 481 | * instead of 'silent|s'). |
||
| 482 | * |
||
| 483 | * @param string $optionName |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public function findMatchingOption($optionName) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param string $optionName |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | protected function findOptionAmongAlternatives($optionName) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $optionName |
||
| 521 | * @return string|null |
||
| 522 | */ |
||
| 523 | protected function findExistingOption($optionName) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Examine the parameters of the method for this command, and |
||
| 537 | * build a list of commandline arguements for them. |
||
| 538 | * |
||
| 539 | * @return array |
||
| 540 | */ |
||
| 541 | protected function determineAgumentClassifications() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Examine the provided parameter, and determine whether it |
||
| 557 | * is a parameter that will be filled in with a positional |
||
| 558 | * commandline argument. |
||
| 559 | */ |
||
| 560 | protected function addParameterToResult($result, $param) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Examine the parameters of the method for this command, and determine |
||
| 580 | * the disposition of the options from them. |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | protected function determineOptionsFromParameters() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Helper; determine if an array is associative or not. An array |
||
| 602 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 603 | * from zero. All other arrays are considered to be associative. |
||
| 604 | * |
||
| 605 | * @param array $arr The array |
||
| 606 | * @return boolean |
||
| 607 | */ |
||
| 608 | protected function isAssoc($arr) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Convert from a method name to the corresponding command name. A |
||
| 618 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 619 | * become 'foo:bar-baz-boz'. |
||
| 620 | * |
||
| 621 | * @param string $camel method name. |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | protected function convertName($camel) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Parse the docBlock comment for this command, and set the |
||
| 634 | * fields of this class with the data thereby obtained. |
||
| 635 | */ |
||
| 636 | protected function parseDocBlock() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 648 | * convert the data into the last of these forms. |
||
| 649 | */ |
||
| 650 | protected static function convertListToCommaSeparated($text) |
||
| 654 | } |
||
| 655 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.