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 primary name for this command. |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function getName() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set the primary name for this command. |
||
| 108 | * |
||
| 109 | * @param string $name |
||
| 110 | */ |
||
| 111 | public function setName($name) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get any annotations included in the docblock comment for the |
||
| 118 | * implementation method of this command that are not already |
||
| 119 | * handled by the primary methods of this class. |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function getAnnotations() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Return a specific named annotation for this command. |
||
| 131 | * |
||
| 132 | * @param string $annotation The name of the annotation. |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getAnnotation($annotation) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check to see if the specified annotation exists for this command. |
||
| 146 | * |
||
| 147 | * @param string $annotation The name of the annotation. |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | public function hasAnnotation($annotation) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Save any tag that we do not explicitly recognize in the |
||
| 158 | * 'otherAnnotations' map. |
||
| 159 | */ |
||
| 160 | public function addOtherAnnotation($name, $content) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the synopsis of the command (~first line). |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getDescription() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set the command description. |
||
| 178 | * |
||
| 179 | * @param string $description The description to set. |
||
| 180 | */ |
||
| 181 | public function setDescription($description) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the help text of the command (the description) |
||
| 188 | */ |
||
| 189 | public function getHelp() |
||
| 194 | /** |
||
| 195 | * Set the help text for this command. |
||
| 196 | * |
||
| 197 | * @param string $help The help text. |
||
| 198 | */ |
||
| 199 | public function setHelp($help) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return the list of aliases for this command. |
||
| 206 | * @return string[] |
||
| 207 | */ |
||
| 208 | public function getAliases() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set aliases that can be used in place of the command's primary name. |
||
| 216 | * |
||
| 217 | * @param string|string[] $aliases |
||
| 218 | */ |
||
| 219 | public function setAliases($aliases) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return the examples for this command. This is @usage instead of |
||
| 229 | * @example because the later is defined by the phpdoc standard to |
||
| 230 | * be example method calls. |
||
| 231 | * |
||
| 232 | * @return string[] |
||
| 233 | */ |
||
| 234 | public function getExampleUsages() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add an example usage for this command. |
||
| 242 | * |
||
| 243 | * @param string $usage An example of the command, including the command |
||
| 244 | * name and all of its example arguments and options. |
||
| 245 | * @param string $description An explanation of what the example does. |
||
| 246 | */ |
||
| 247 | public function setExampleUsage($usage, $description) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return the list of refleaction parameters. |
||
| 254 | * |
||
| 255 | * @return ReflectionParameter[] |
||
| 256 | */ |
||
| 257 | public function getParameters() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Descriptions of commandline arguements for this command. |
||
| 264 | * |
||
| 265 | * @return DefaultsWithDescriptions |
||
| 266 | */ |
||
| 267 | public function arguments() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Descriptions of commandline options for this command. |
||
| 274 | * |
||
| 275 | * @return DefaultsWithDescriptions |
||
| 276 | */ |
||
| 277 | public function options() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * An option might have a name such as 'silent|s'. In this |
||
| 284 | * instance, we will allow the @option or @default tag to |
||
| 285 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 286 | * instead of 'silent|s'). |
||
| 287 | * |
||
| 288 | * @param string $optionName |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function findMatchingOption($optionName) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $optionName |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | protected function findOptionAmongAlternatives($optionName) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $optionName |
||
| 326 | * @return string|null |
||
| 327 | */ |
||
| 328 | protected function findExistingOption($optionName) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Examine the parameters of the method for this command, and |
||
| 342 | * build a list of commandline arguements for them. |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | protected function determineAgumentClassifications() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Examine the provided parameter, and determine whether it |
||
| 364 | * is a parameter that will be filled in with a positional |
||
| 365 | * commandline argument. |
||
| 366 | * |
||
| 367 | * @return false|null|string|array |
||
| 368 | */ |
||
| 369 | protected function getArgumentClassification($param) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Examine the parameters of the method for this command, and determine |
||
| 391 | * the disposition of the options from them. |
||
| 392 | * |
||
| 393 | * @return array |
||
| 394 | */ |
||
| 395 | protected function determineOptionsFromParameters() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Helper; determine if an array is associative or not. An array |
||
| 413 | * is not associative if its keys are numeric, and numbered sequentially |
||
| 414 | * from zero. All other arrays are considered to be associative. |
||
| 415 | * |
||
| 416 | * @param arrau $arr The array |
||
| 417 | * @return boolean |
||
| 418 | */ |
||
| 419 | protected function isAssoc($arr) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Convert from a method name to the corresponding command name. A |
||
| 429 | * method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will |
||
| 430 | * become 'foo:bar-baz-boz'. |
||
| 431 | * |
||
| 432 | * @param string $camel method name. |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | protected function convertName($camel) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Parse the docBlock comment for this command, and set the |
||
| 445 | * fields of this class with the data thereby obtained. |
||
| 446 | */ |
||
| 447 | protected function parseDocBlock() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 459 | * convert the data into the last of these forms. |
||
| 460 | */ |
||
| 461 | protected static function convertListToCommaSeparated($text) |
||
| 465 | } |
||
| 466 |