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 |
||
| 12 | class CommandInfo |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var \ReflectionMethod |
||
| 16 | */ |
||
| 17 | protected $reflection; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var boolean |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $docBlockIsParsed; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $name; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $description = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $help = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $tagProcessors = [ |
||
| 44 | 'command' => 'processCommandTag', |
||
| 45 | 'name' => 'processCommandTag', |
||
| 46 | 'param' => 'processArgumentTag', |
||
| 47 | 'option' => 'processOptionTag', |
||
| 48 | 'default' => 'processDefaultTag', |
||
| 49 | 'aliases' => 'processAliases', |
||
| 50 | 'usage' => 'processUsageTag', |
||
| 51 | 'description' => 'processAlternateDescriptionTag', |
||
| 52 | 'desc' => 'processAlternateDescriptionTag', |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $options = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $arguments = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $argumentDescriptions = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $optionDescriptions = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $exampleUsage = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | protected $otherAnnotations = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $aliases = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $methodName; |
||
| 94 | |||
| 95 | public function __construct($classNameOrInstance, $methodName) |
||
| 105 | |||
| 106 | public function getMethodName() |
||
| 110 | |||
| 111 | public function getParameters() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the synopsis of the command (~first line). |
||
| 118 | */ |
||
| 119 | public function getDescription() |
||
| 124 | |||
| 125 | public function setDescription($description) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the help text of the command (the description) |
||
| 132 | */ |
||
| 133 | public function getHelp() |
||
| 138 | |||
| 139 | public function setHelp($help) |
||
| 143 | |||
| 144 | public function getAliases() |
||
| 149 | |||
| 150 | public function setAliases($aliases) |
||
| 157 | |||
| 158 | public function getExampleUsages() |
||
| 163 | |||
| 164 | public function getName() |
||
| 169 | |||
| 170 | public function setDefaultName() |
||
| 174 | |||
| 175 | protected function determineAgumentClassifications() |
||
| 190 | |||
| 191 | public function getArguments() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Examine the provided parameter, and determine whether it |
||
| 198 | * is a parameter that will be filled in with a positional |
||
| 199 | * commandline argument. |
||
| 200 | * |
||
| 201 | * @return false|null|string|array |
||
| 202 | */ |
||
| 203 | protected function getArgumentClassification($param) |
||
| 222 | |||
| 223 | public function getOptions() |
||
| 227 | |||
| 228 | public function determineOptionsFromParameters() |
||
| 243 | |||
| 244 | public function getArgumentDescription($name) |
||
| 253 | |||
| 254 | public function getOptionDescription($name) |
||
| 263 | |||
| 264 | protected function isAssoc($arr) |
||
| 271 | |||
| 272 | public function getAnnotations() |
||
| 277 | |||
| 278 | public function getAnnotation($annotation) |
||
| 286 | |||
| 287 | public function hasAnnotation($annotation) |
||
| 292 | |||
| 293 | protected function convertName($camel) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Parse the docBlock comment for this command, and set the |
||
| 303 | * fields of this class with the data thereby obtained. |
||
| 304 | */ |
||
| 305 | protected function parseDocBlock() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Save any tag that we do not explicitly recognize in the |
||
| 329 | * 'otherAnnotations' map. |
||
| 330 | */ |
||
| 331 | protected function processGenericTag($tag) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Set the name of the command from a @command or @name annotation. |
||
| 338 | */ |
||
| 339 | protected function processCommandTag($tag) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * The @description and @desc annotations may be used in |
||
| 349 | * place of the synopsis (which we call 'description'). |
||
| 350 | * This is discouraged. |
||
| 351 | * |
||
| 352 | * @deprecated |
||
| 353 | */ |
||
| 354 | protected function processAlternateDescriptionTag($tag) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Store the data from a @param annotation in our argument descriptions. |
||
| 361 | */ |
||
| 362 | protected function processArgumentTag($tag) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Given a docblock description in the form "$variable description", |
||
| 377 | * return the variable name and description via the 'match' parameter. |
||
| 378 | */ |
||
| 379 | protected function pregMatchNameAndDescription($source, &$match) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Store the data from an @option annotation in our option descriptions. |
||
| 390 | */ |
||
| 391 | protected function processOptionTag($tag) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * An option might have a name such as 'silent|s'. In this |
||
| 406 | * instance, we will allow the @option or @default tag to |
||
| 407 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 408 | * instead of 'silent|s'). |
||
| 409 | */ |
||
| 410 | protected function findMatchingOption($optionName) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Store the data from a @default annotation in our argument or option store, |
||
| 441 | * as appropriate. |
||
| 442 | */ |
||
| 443 | protected function processDefaultTag($tag) |
||
| 459 | |||
| 460 | protected function interpretDefaultValue($defaultValue) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Process the comma-separated list of aliases |
||
| 479 | */ |
||
| 480 | protected function processAliases($tag) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Store the data from a @usage annotation in our example usage list. |
||
| 487 | */ |
||
| 488 | protected function processUsageTag($tag) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 499 | * convert the data into the last of these forms. |
||
| 500 | */ |
||
| 501 | protected static function convertListToCommaSeparated($text) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Take a multiline description and convert it into a single |
||
| 508 | * long unbroken line. |
||
| 509 | */ |
||
| 510 | protected static function removeLineBreaks($text) |
||
| 514 | } |
||
| 515 |