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 \ReflectionParameter |
||
| 13 | */ |
||
| 14 | protected $params; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $name; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $description = ''; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $help = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $options = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $arguments = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $argumentDescriptions = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $optionDescriptions = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $exampleUsage = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $otherAnnotations = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $aliases = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $methodName; |
||
| 70 | |||
| 71 | public function __construct($classNameOrInstance, $methodName) |
||
| 79 | |||
| 80 | public function getMethodName() |
||
| 84 | |||
| 85 | public function getParameters() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the synopsis of the command (~first line). |
||
| 92 | */ |
||
| 93 | public function getDescription() |
||
| 97 | |||
| 98 | public function setDescription($description) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the help text of the command (the description) |
||
| 105 | */ |
||
| 106 | public function getHelp() |
||
| 110 | |||
| 111 | public function setHelp($help) |
||
| 115 | |||
| 116 | public function getAliases() |
||
| 120 | |||
| 121 | public function setAliases($aliases) |
||
| 128 | |||
| 129 | public function getExampleUsages() |
||
| 133 | |||
| 134 | public function getName() |
||
| 138 | |||
| 139 | public function setName($name) |
||
| 143 | |||
| 144 | public function getArguments() |
||
| 148 | |||
| 149 | public function hasArgument($name) |
||
| 153 | |||
| 154 | public function setArgumentDefaultValue($name, $defaultValue) |
||
| 158 | |||
| 159 | public function addArgument($name, $description, $defaultValue = null) |
||
| 169 | |||
| 170 | public function getOptions() |
||
| 174 | |||
| 175 | public function hasOption($name) |
||
| 179 | |||
| 180 | public function setOptionDefaultValue($name, $defaultValue) |
||
| 184 | |||
| 185 | public function addOption($name, $description, $defaultValue = false) |
||
| 195 | |||
| 196 | public function getArgumentDescription($name) |
||
| 204 | |||
| 205 | public function getOptionDescription($name) |
||
| 213 | |||
| 214 | public function getAnnotations() |
||
| 218 | |||
| 219 | public function getAnnotation($annotation) |
||
| 227 | |||
| 228 | public function hasAnnotation($annotation) |
||
| 232 | |||
| 233 | public function setExampleUsage($usage, $description) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Save any tag that we do not explicitly recognize in the |
||
| 240 | * 'otherAnnotations' map. |
||
| 241 | */ |
||
| 242 | public function addOtherAnnotation($name, $content) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * An option might have a name such as 'silent|s'. In this |
||
| 249 | * instance, we will allow the @option or @default tag to |
||
| 250 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 251 | * instead of 'silent|s'). |
||
| 252 | */ |
||
| 253 | public function findMatchingOption($optionName) |
||
| 281 | |||
| 282 | protected function initializeFromParameters($params) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Parse the docBlock comment for this command, and set the |
||
| 293 | * fields of this class with the data thereby obtained. |
||
| 294 | */ |
||
| 295 | protected function parseDocBlock($docblock) |
||
| 300 | |||
| 301 | protected function determineAgumentClassifications($params) |
||
| 315 | |||
| 316 | protected function determineOptionsFromParameters($params) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Examine the provided parameter, and determine whether it |
||
| 333 | * is a parameter that will be filled in with a positional |
||
| 334 | * commandline argument. |
||
| 335 | * |
||
| 336 | * @return false|null|string|array |
||
| 337 | */ |
||
| 338 | protected function getArgumentClassification($param) |
||
| 357 | |||
| 358 | protected function setDefaultName($methodName) |
||
| 362 | |||
| 363 | protected function convertName($camel) |
||
| 370 | |||
| 371 | protected function isAssoc($arr) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 381 | * convert the data into the last of these forms. |
||
| 382 | */ |
||
| 383 | protected static function convertListToCommaSeparated($text) |
||
| 387 | } |
||
| 388 |