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 |
||
| 17 | class CommandInfo |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var \ReflectionMethod |
||
| 21 | */ |
||
| 22 | protected $reflection; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var boolean |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $docBlockIsParsed; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $name; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $description = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $help = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var DefaultsWithDescriptions |
||
| 47 | */ |
||
| 48 | protected $options; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var DefaultsWithDescriptions |
||
| 52 | */ |
||
| 53 | protected $arguments; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $exampleUsage = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var AnnotationData |
||
| 62 | */ |
||
| 63 | protected $otherAnnotations; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $aliases = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $methodName; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $returnType; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $optionParamName; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Create a new CommandInfo class for a particular method of a class. |
||
| 87 | * |
||
| 88 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
| 89 | * instance of it. |
||
| 90 | * @param string $methodName The name of the method to get info about. |
||
| 91 | */ |
||
| 92 | public function __construct($classNameOrInstance, $methodName) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Recover the method name provided to the constructor. |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getMethodName() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Return the primary name for this command. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getName() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set the primary name for this command. |
||
| 132 | * |
||
| 133 | * @param string $name |
||
| 134 | */ |
||
| 135 | public function setName($name) |
||
| 139 | |||
| 140 | public function getReturnType() |
||
| 145 | |||
| 146 | public function setReturnType($returnType) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get any annotations included in the docblock comment for the |
||
| 153 | * implementation method of this command that are not already |
||
| 154 | * handled by the primary methods of this class. |
||
| 155 | * |
||
| 156 | * @return AnnotationData |
||
| 157 | */ |
||
| 158 | public function getAnnotations() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get any annotations included in the docblock comment, |
||
| 166 | * also including default values such as @command. We add |
||
| 167 | * in the default @command annotation late, and only in a |
||
| 168 | * copy of the annotation data because we use the existance |
||
| 169 | * of a @command to indicate that this CommandInfo is |
||
| 170 | * a command, and not a hook or anything else. |
||
| 171 | * |
||
| 172 | * @return AnnotationData |
||
| 173 | */ |
||
| 174 | public function getAnnotationsForCommand() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return a specific named annotation for this command. |
||
| 186 | * |
||
| 187 | * @param string $annotation The name of the annotation. |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getAnnotation($annotation) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Check to see if the specified annotation exists for this command. |
||
| 201 | * |
||
| 202 | * @param string $annotation The name of the annotation. |
||
| 203 | * @return boolean |
||
| 204 | */ |
||
| 205 | public function hasAnnotation($annotation) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Save any tag that we do not explicitly recognize in the |
||
| 213 | * 'otherAnnotations' map. |
||
| 214 | */ |
||
| 215 | public function addOtherAnnotation($name, $content) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get the synopsis of the command (~first line). |
||
| 222 | * |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function getDescription() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set the command description. |
||
| 233 | * |
||
| 234 | * @param string $description The description to set. |
||
| 235 | */ |
||
| 236 | public function setDescription($description) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the help text of the command (the description) |
||
| 243 | */ |
||
| 244 | public function getHelp() |
||
| 249 | /** |
||
| 250 | * Set the help text for this command. |
||
| 251 | * |
||
| 252 | * @param string $help The help text. |
||
| 253 | */ |
||
| 254 | public function setHelp($help) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return the list of aliases for this command. |
||
| 261 | * @return string[] |
||
| 262 | */ |
||
| 263 | public function getAliases() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Set aliases that can be used in place of the command's primary name. |
||
| 271 | * |
||
| 272 | * @param string|string[] $aliases |
||
| 273 | */ |
||
| 274 | public function setAliases($aliases) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Return the examples for this command. This is @usage instead of |
||
| 284 | * @example because the later is defined by the phpdoc standard to |
||
| 285 | * be example method calls. |
||
| 286 | * |
||
| 287 | * @return string[] |
||
| 288 | */ |
||
| 289 | public function getExampleUsages() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Add an example usage for this command. |
||
| 297 | * |
||
| 298 | * @param string $usage An example of the command, including the command |
||
| 299 | * name and all of its example arguments and options. |
||
| 300 | * @param string $description An explanation of what the example does. |
||
| 301 | */ |
||
| 302 | public function setExampleUsage($usage, $description) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return the list of refleaction parameters. |
||
| 309 | * |
||
| 310 | * @return ReflectionParameter[] |
||
| 311 | */ |
||
| 312 | public function getParameters() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Descriptions of commandline arguements for this command. |
||
| 319 | * |
||
| 320 | * @return DefaultsWithDescriptions |
||
| 321 | */ |
||
| 322 | public function arguments() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Descriptions of commandline options for this command. |
||
| 329 | * |
||
| 330 | * @return DefaultsWithDescriptions |
||
| 331 | */ |
||
| 332 | public function options() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Return the name of the last parameter if it holds the options. |
||
| 339 | */ |
||
| 340 | public function optionParamName() |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * An option might have a name such as 'silent|s'. In this |
||
| 348 | * instance, we will allow the @option or @default tag to |
||
| 349 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 350 | * instead of 'silent|s'). |
||
| 351 | * |
||
| 352 | * @param string $optionName |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function findMatchingOption($optionName) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $optionName |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | protected function findOptionAmongAlternatives($optionName) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $optionName |
||
| 390 | * @return string|null |
||
| 391 | */ |
||
| 392 | protected function findExistingOption($optionName) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Examine the parameters of the method for this command, and |
||
| 406 | * build a list of commandline arguements for them. |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | protected function determineAgumentClassifications() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Examine the provided parameter, and determine whether it |
||
| 426 | * is a parameter that will be filled in with a positional |
||
| 427 | * commandline argument. |
||
| 428 | */ |
||
| 429 | protected function addParameterToResult($result, $param) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Examine the parameters of the method for this command, and determine |
||
| 449 | * the disposition of the options from them. |
||
| 450 | * |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | protected function determineOptionsFromParameters() |
||
| 468 | |||
| 469 | protected function lastParameterName() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Helper; determine if an array is associative or not. An array |
||
| 481 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 482 | * from zero. All other arrays are considered to be associative. |
||
| 483 | * |
||
| 484 | * @param arrau $arr The array |
||
| 485 | * @return boolean |
||
| 486 | */ |
||
| 487 | protected function isAssoc($arr) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Convert from a method name to the corresponding command name. A |
||
| 497 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 498 | * become 'foo:bar-baz-boz'. |
||
| 499 | * |
||
| 500 | * @param string $camel method name. |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | protected function convertName($camel) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Parse the docBlock comment for this command, and set the |
||
| 513 | * fields of this class with the data thereby obtained. |
||
| 514 | */ |
||
| 515 | protected function parseDocBlock() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 527 | * convert the data into the last of these forms. |
||
| 528 | */ |
||
| 529 | protected static function convertListToCommaSeparated($text) |
||
| 533 | } |
||
| 534 |