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 | * @var string |
||
| 69 | */ |
||
| 70 | protected $returnType; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $optionParamName; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Create a new CommandInfo class for a particular method of a class. |
||
| 79 | * |
||
| 80 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
| 81 | * instance of it. |
||
| 82 | * @param string $methodName The name of the method to get info about. |
||
| 83 | */ |
||
| 84 | public function __construct($classNameOrInstance, $methodName) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Recover the method name provided to the constructor. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getMethodName() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Return the primary name for this command. |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getName() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set the primary name for this command. |
||
| 123 | * |
||
| 124 | * @param string $name |
||
| 125 | */ |
||
| 126 | public function setName($name) |
||
| 130 | |||
| 131 | public function getReturnType() |
||
| 136 | |||
| 137 | public function setReturnType($returnType) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get any annotations included in the docblock comment for the |
||
| 144 | * implementation method of this command that are not already |
||
| 145 | * handled by the primary methods of this class. |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getAnnotations() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Return a specific named annotation for this command. |
||
| 157 | * |
||
| 158 | * @param string $annotation The name of the annotation. |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getAnnotation($annotation) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Check to see if the specified annotation exists for this command. |
||
| 172 | * |
||
| 173 | * @param string $annotation The name of the annotation. |
||
| 174 | * @return boolean |
||
| 175 | */ |
||
| 176 | public function hasAnnotation($annotation) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Save any tag that we do not explicitly recognize in the |
||
| 184 | * 'otherAnnotations' map. |
||
| 185 | */ |
||
| 186 | public function addOtherAnnotation($name, $content) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the synopsis of the command (~first line). |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getDescription() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Set the command description. |
||
| 204 | * |
||
| 205 | * @param string $description The description to set. |
||
| 206 | */ |
||
| 207 | public function setDescription($description) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the help text of the command (the description) |
||
| 214 | */ |
||
| 215 | public function getHelp() |
||
| 220 | /** |
||
| 221 | * Set the help text for this command. |
||
| 222 | * |
||
| 223 | * @param string $help The help text. |
||
| 224 | */ |
||
| 225 | public function setHelp($help) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return the list of aliases for this command. |
||
| 232 | * @return string[] |
||
| 233 | */ |
||
| 234 | public function getAliases() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Set aliases that can be used in place of the command's primary name. |
||
| 242 | * |
||
| 243 | * @param string|string[] $aliases |
||
| 244 | */ |
||
| 245 | public function setAliases($aliases) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Return the examples for this command. This is @usage instead of |
||
| 255 | * @example because the later is defined by the phpdoc standard to |
||
| 256 | * be example method calls. |
||
| 257 | * |
||
| 258 | * @return string[] |
||
| 259 | */ |
||
| 260 | public function getExampleUsages() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Add an example usage for this command. |
||
| 268 | * |
||
| 269 | * @param string $usage An example of the command, including the command |
||
| 270 | * name and all of its example arguments and options. |
||
| 271 | * @param string $description An explanation of what the example does. |
||
| 272 | */ |
||
| 273 | public function setExampleUsage($usage, $description) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Return the list of refleaction parameters. |
||
| 280 | * |
||
| 281 | * @return ReflectionParameter[] |
||
| 282 | */ |
||
| 283 | public function getParameters() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Descriptions of commandline arguements for this command. |
||
| 290 | * |
||
| 291 | * @return DefaultsWithDescriptions |
||
| 292 | */ |
||
| 293 | public function arguments() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Descriptions of commandline options for this command. |
||
| 300 | * |
||
| 301 | * @return DefaultsWithDescriptions |
||
| 302 | */ |
||
| 303 | public function options() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Return the name of the last parameter if it holds the options. |
||
| 310 | */ |
||
| 311 | public function optionParamName() |
||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * An option might have a name such as 'silent|s'. In this |
||
| 319 | * instance, we will allow the @option or @default tag to |
||
| 320 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 321 | * instead of 'silent|s'). |
||
| 322 | * |
||
| 323 | * @param string $optionName |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | public function findMatchingOption($optionName) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $optionName |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | protected function findOptionAmongAlternatives($optionName) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $optionName |
||
| 361 | * @return string|null |
||
| 362 | */ |
||
| 363 | protected function findExistingOption($optionName) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Examine the parameters of the method for this command, and |
||
| 377 | * build a list of commandline arguements for them. |
||
| 378 | * |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | protected function determineAgumentClassifications() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Examine the provided parameter, and determine whether it |
||
| 397 | * is a parameter that will be filled in with a positional |
||
| 398 | * commandline argument. |
||
| 399 | */ |
||
| 400 | protected function addParameterToResult($result, $param) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Examine the parameters of the method for this command, and determine |
||
| 420 | * the disposition of the options from them. |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | protected function determineOptionsFromParameters() |
||
| 439 | |||
| 440 | protected function lastParameterName() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Helper; determine if an array is associative or not. An array |
||
| 452 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 453 | * from zero. All other arrays are considered to be associative. |
||
| 454 | * |
||
| 455 | * @param arrau $arr The array |
||
| 456 | * @return boolean |
||
| 457 | */ |
||
| 458 | protected function isAssoc($arr) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Convert from a method name to the corresponding command name. A |
||
| 468 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 469 | * become 'foo:bar-baz-boz'. |
||
| 470 | * |
||
| 471 | * @param string $camel method name. |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | protected function convertName($camel) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Parse the docBlock comment for this command, and set the |
||
| 484 | * fields of this class with the data thereby obtained. |
||
| 485 | */ |
||
| 486 | protected function parseDocBlock() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 498 | * convert the data into the last of these forms. |
||
| 499 | */ |
||
| 500 | protected static function convertListToCommaSeparated($text) |
||
| 504 | } |
||
| 505 |