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 |
||
| 9 | class CommandInfo |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var \ReflectionMethod |
||
| 13 | */ |
||
| 14 | protected $reflection; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var boolean |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $docBlockIsParsed; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $name; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $description = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $help = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var DefaultsWithDescriptions |
||
| 39 | */ |
||
| 40 | protected $options = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var DefaultsWithDescriptions |
||
| 44 | */ |
||
| 45 | protected $arguments = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $exampleUsage = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $otherAnnotations = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $aliases = []; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $methodName; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Create a new CommandInfo class for a particular method of a class. |
||
| 69 | * |
||
| 70 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
| 71 | * instance of it. |
||
| 72 | * @param string $methodName The name of the method to get info about. |
||
| 73 | */ |
||
| 74 | public function __construct($classNameOrInstance, $methodName) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Recover the method name provided to the constructor. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getMethodName() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Return the list of refleaction parameters. |
||
| 97 | * |
||
| 98 | * @return ReflectionParameter[] |
||
| 99 | */ |
||
| 100 | public function getParameters() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the synopsis of the command (~first line). |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getDescription() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set the command description. |
||
| 118 | * |
||
| 119 | * @param string $description The description to set. |
||
| 120 | */ |
||
| 121 | public function setDescription($description) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the help text of the command (the description) |
||
| 128 | */ |
||
| 129 | public function getHelp() |
||
| 134 | /** |
||
| 135 | * Set the help text for this command. |
||
| 136 | * |
||
| 137 | * @param string $help The help text. |
||
| 138 | */ |
||
| 139 | public function setHelp($help) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the list of aliases for this command. |
||
| 146 | * @return string[] |
||
| 147 | */ |
||
| 148 | public function getAliases() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set aliases that can be used in place of the command's primary name. |
||
| 156 | * |
||
| 157 | * @param string|string[] $aliases |
||
| 158 | */ |
||
| 159 | public function setAliases($aliases) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the examples for this command. This is @usage instead of |
||
| 169 | * @example because the later is defined by the phpdoc standard to |
||
| 170 | * be example method calls. |
||
| 171 | * |
||
| 172 | * @return string[] |
||
| 173 | */ |
||
| 174 | public function getExampleUsages() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Return the primary name for this command. |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public function getName() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Set the primary name for this command. |
||
| 193 | * |
||
| 194 | * @param string $name |
||
| 195 | */ |
||
| 196 | public function setName($name) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Examine the parameters of the method for this command, and |
||
| 203 | * build a list of commandline arguements for them. |
||
| 204 | * |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | protected function determineAgumentClassifications() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Return the commandline arguments for this command. The key |
||
| 225 | * contains the name of the argument, and the value contains its |
||
| 226 | * default value. Required commands have a 'null' value. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getArguments() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Check to see if an argument with the specified name exits. |
||
| 237 | * |
||
| 238 | * @param string $name Argument to test for. |
||
| 239 | * @return boolean |
||
| 240 | */ |
||
| 241 | public function hasArgument($name) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Set the default value for an argument. A default value of 'null' |
||
| 248 | * indicates that the argument is required. |
||
| 249 | * |
||
| 250 | * @param string $name Name of argument to modify. |
||
| 251 | * @param string $defaultValue New default value for that argument. |
||
| 252 | */ |
||
| 253 | public function setArgumentDefaultValue($name, $defaultValue) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Add another argument to this command. |
||
| 260 | * |
||
| 261 | * @param string $name Name of the argument. |
||
| 262 | * @param string $description Help text for the argument. |
||
| 263 | * @param string $defaultValue The default value for the argument. |
||
| 264 | */ |
||
| 265 | public function addArgument($name, $description, $defaultValue = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Examine the provided parameter, and determine whether it |
||
| 272 | * is a parameter that will be filled in with a positional |
||
| 273 | * commandline argument. |
||
| 274 | * |
||
| 275 | * @return false|null|string|array |
||
| 276 | */ |
||
| 277 | protected function getArgumentClassification($param) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return the options for is command. The key is the options name, |
||
| 299 | * and the value is its default value. |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function getOptions() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Check to see if the specified option exists. |
||
| 310 | * |
||
| 311 | * @param string $name Name of the option to check. |
||
| 312 | * @return boolean |
||
| 313 | */ |
||
| 314 | public function hasOption($name) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Change the default value for an option. |
||
| 321 | * |
||
| 322 | * @param string $name Option name. |
||
| 323 | * @param string $defaultValue Option default value. |
||
| 324 | */ |
||
| 325 | public function setOptionDefaultValue($name, $defaultValue) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Add another option to this command. |
||
| 332 | * |
||
| 333 | * @param string $name Option name. |
||
| 334 | * @param string $description Option description. |
||
| 335 | * @param string $defaultValue Option default value. |
||
| 336 | */ |
||
| 337 | public function addOption($name, $description, $defaultValue = null) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Examine the parameters of the method for this command, and determine |
||
| 344 | * the disposition of the options from them. |
||
| 345 | * |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | public function determineOptionsFromParameters() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get the description of one argument. |
||
| 366 | * |
||
| 367 | * @param string $name The name of the argument. |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function getArgumentDescription($name) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the description of one argument. |
||
| 378 | * |
||
| 379 | * @param string $name The name of the option. |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getOptionDescription($name) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Helper; determine if an array is associative or not. An array |
||
| 390 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 391 | * from zero. All other arrays are considered to be associative. |
||
| 392 | * |
||
| 393 | * @param arrau $arr The array |
||
| 394 | * @return boolean |
||
| 395 | */ |
||
| 396 | protected function isAssoc($arr) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get any annotations included in the docblock comment for the |
||
| 406 | * implementation method of this command that are not already |
||
| 407 | * handled by the primary methods of this class. |
||
| 408 | * |
||
| 409 | * @return array |
||
| 410 | */ |
||
| 411 | public function getAnnotations() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Return a specific named annotation for this command. |
||
| 419 | * |
||
| 420 | * @param string $annotation The name of the annotation. |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function getAnnotation($annotation) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Check to see if the specified annotation exists for this command. |
||
| 434 | * |
||
| 435 | * @param string $annotation The name of the annotation. |
||
| 436 | * @return boolean |
||
| 437 | */ |
||
| 438 | public function hasAnnotation($annotation) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Convert from a method name to the corresponding command name. A |
||
| 446 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 447 | * become 'foo:bar-baz-boz'. |
||
| 448 | * |
||
| 449 | * @param type $camel method name. |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | protected function convertName($camel) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Add an example usage for this command. |
||
| 462 | * |
||
| 463 | * @param string $usage An example of the command, including the command |
||
| 464 | * name and all of its example arguments and options. |
||
| 465 | * @param string $description An explanation of what the example does. |
||
| 466 | */ |
||
| 467 | public function setExampleUsage($usage, $description) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Parse the docBlock comment for this command, and set the |
||
| 474 | * fields of this class with the data thereby obtained. |
||
| 475 | */ |
||
| 476 | protected function parseDocBlock() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Save any tag that we do not explicitly recognize in the |
||
| 488 | * 'otherAnnotations' map. |
||
| 489 | */ |
||
| 490 | public function addOtherAnnotation($name, $content) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * An option might have a name such as 'silent|s'. In this |
||
| 497 | * instance, we will allow the @option or @default tag to |
||
| 498 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 499 | * instead of 'silent|s'). |
||
| 500 | */ |
||
| 501 | public function findMatchingOption($optionName) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 531 | * convert the data into the last of these forms. |
||
| 532 | */ |
||
| 533 | protected static function convertListToCommaSeparated($text) |
||
| 537 | } |
||
| 538 |