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 | * Create a new CommandInfo class for a particular method of a class. |
||
| 74 | * |
||
| 75 | * @param string|mixed $classNameOrInstance The name of a class, or an |
||
| 76 | * instance of it. |
||
| 77 | * @param string $methodName The name of the method to get info about. |
||
| 78 | */ |
||
| 79 | public function __construct($classNameOrInstance, $methodName) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Recover the method name provided to the constructor. |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function getMethodName() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Return the primary name for this command. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function getName() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set the primary name for this command. |
||
| 113 | * |
||
| 114 | * @param string $name |
||
| 115 | */ |
||
| 116 | public function setName($name) |
||
| 120 | |||
| 121 | public function getReturnType() |
||
| 126 | |||
| 127 | public function setReturnType($returnType) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get any annotations included in the docblock comment for the |
||
| 134 | * implementation method of this command that are not already |
||
| 135 | * handled by the primary methods of this class. |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function getAnnotations() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Return a specific named annotation for this command. |
||
| 147 | * |
||
| 148 | * @param string $annotation The name of the annotation. |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getAnnotation($annotation) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Check to see if the specified annotation exists for this command. |
||
| 162 | * |
||
| 163 | * @param string $annotation The name of the annotation. |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | public function hasAnnotation($annotation) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Save any tag that we do not explicitly recognize in the |
||
| 174 | * 'otherAnnotations' map. |
||
| 175 | */ |
||
| 176 | public function addOtherAnnotation($name, $content) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the synopsis of the command (~first line). |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getDescription() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the command description. |
||
| 194 | * |
||
| 195 | * @param string $description The description to set. |
||
| 196 | */ |
||
| 197 | public function setDescription($description) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the help text of the command (the description) |
||
| 204 | */ |
||
| 205 | public function getHelp() |
||
| 210 | /** |
||
| 211 | * Set the help text for this command. |
||
| 212 | * |
||
| 213 | * @param string $help The help text. |
||
| 214 | */ |
||
| 215 | public function setHelp($help) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Return the list of aliases for this command. |
||
| 222 | * @return string[] |
||
| 223 | */ |
||
| 224 | public function getAliases() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Set aliases that can be used in place of the command's primary name. |
||
| 232 | * |
||
| 233 | * @param string|string[] $aliases |
||
| 234 | */ |
||
| 235 | public function setAliases($aliases) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return the examples for this command. This is @usage instead of |
||
| 245 | * @example because the later is defined by the phpdoc standard to |
||
| 246 | * be example method calls. |
||
| 247 | * |
||
| 248 | * @return string[] |
||
| 249 | */ |
||
| 250 | public function getExampleUsages() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Add an example usage for this command. |
||
| 258 | * |
||
| 259 | * @param string $usage An example of the command, including the command |
||
| 260 | * name and all of its example arguments and options. |
||
| 261 | * @param string $description An explanation of what the example does. |
||
| 262 | */ |
||
| 263 | public function setExampleUsage($usage, $description) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return the list of refleaction parameters. |
||
| 270 | * |
||
| 271 | * @return ReflectionParameter[] |
||
| 272 | */ |
||
| 273 | public function getParameters() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Descriptions of commandline arguements for this command. |
||
| 280 | * |
||
| 281 | * @return DefaultsWithDescriptions |
||
| 282 | */ |
||
| 283 | public function arguments() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Descriptions of commandline options for this command. |
||
| 290 | * |
||
| 291 | * @return DefaultsWithDescriptions |
||
| 292 | */ |
||
| 293 | public function options() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * An option might have a name such as 'silent|s'. In this |
||
| 300 | * instance, we will allow the @option or @default tag to |
||
| 301 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 302 | * instead of 'silent|s'). |
||
| 303 | * |
||
| 304 | * @param string $optionName |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function findMatchingOption($optionName) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $optionName |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | protected function findOptionAmongAlternatives($optionName) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $optionName |
||
| 342 | * @return string|null |
||
| 343 | */ |
||
| 344 | protected function findExistingOption($optionName) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Examine the parameters of the method for this command, and |
||
| 358 | * build a list of commandline arguements for them. |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | protected function determineAgumentClassifications() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Examine the provided parameter, and determine whether it |
||
| 378 | * is a parameter that will be filled in with a positional |
||
| 379 | * commandline argument. |
||
| 380 | */ |
||
| 381 | protected function addParameterToResult($result, $param) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Examine the parameters of the method for this command, and determine |
||
| 402 | * the disposition of the options from them. |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | protected function determineOptionsFromParameters() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Helper; determine if an array is associative or not. An array |
||
| 424 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 425 | * from zero. All other arrays are considered to be associative. |
||
| 426 | * |
||
| 427 | * @param arrau $arr The array |
||
| 428 | * @return boolean |
||
| 429 | */ |
||
| 430 | protected function isAssoc($arr) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Convert from a method name to the corresponding command name. A |
||
| 440 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 441 | * become 'foo:bar-baz-boz'. |
||
| 442 | * |
||
| 443 | * @param string $camel method name. |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | protected function convertName($camel) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Parse the docBlock comment for this command, and set the |
||
| 456 | * fields of this class with the data thereby obtained. |
||
| 457 | */ |
||
| 458 | protected function parseDocBlock() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 470 | * convert the data into the last of these forms. |
||
| 471 | */ |
||
| 472 | protected static function convertListToCommaSeparated($text) |
||
| 476 | } |
||
| 477 |