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) |
||
| 72 | { |
||
| 73 | $reflectionMethod = new \ReflectionMethod($classNameOrInstance, $methodName); |
||
| 74 | $this->methodName = $methodName; |
||
| 75 | $this->setDefaultName($methodName); |
||
| 76 | $this->initializeFromParameters($reflectionMethod->getParameters()); |
||
| 77 | $this->parseDocBlock($reflectionMethod->getDocComment()); |
||
| 78 | } |
||
| 79 | |||
| 80 | protected function initializeFromParameters($params) |
||
| 81 | { |
||
| 82 | // Set up a default name for the command from the method name. |
||
| 83 | // This can be overridden via @command or @name annotations. |
||
| 84 | $this->params = $params; |
||
| 85 | $this->options = $this->determineOptionsFromParameters($this->params); |
||
| 86 | $this->arguments = $this->determineAgumentClassifications($this->params); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getMethodName() |
||
| 90 | { |
||
| 91 | return $this->methodName; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getParameters() |
||
| 95 | { |
||
| 96 | return $this->params; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the synopsis of the command (~first line). |
||
| 101 | */ |
||
| 102 | public function getDescription() |
||
| 103 | { |
||
| 104 | return $this->description; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function setDescription($description) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the help text of the command (the description) |
||
| 114 | */ |
||
| 115 | public function getHelp() |
||
| 116 | { |
||
| 119 | |||
| 120 | public function setHelp($help) |
||
| 124 | |||
| 125 | public function getAliases() |
||
| 129 | |||
| 130 | public function setAliases($aliases) |
||
| 137 | |||
| 138 | public function getExampleUsages() |
||
| 142 | |||
| 143 | public function getName() |
||
| 147 | |||
| 148 | public function setDefaultName($methodName) |
||
| 152 | |||
| 153 | public function setName($name) |
||
| 157 | |||
| 158 | protected function determineAgumentClassifications($params) |
||
| 172 | |||
| 173 | public function getArguments() |
||
| 177 | |||
| 178 | public function hasArgument($name) |
||
| 182 | |||
| 183 | public function setArgumentDefaultValue($name, $defaultValue) |
||
| 187 | |||
| 188 | public function addArgument($name, $description, $defaultValue = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Examine the provided parameter, and determine whether it |
||
| 201 | * is a parameter that will be filled in with a positional |
||
| 202 | * commandline argument. |
||
| 203 | * |
||
| 204 | * @return false|null|string|array |
||
| 205 | */ |
||
| 206 | protected function getArgumentClassification($param) |
||
| 225 | |||
| 226 | public function getOptions() |
||
| 230 | |||
| 231 | public function hasOption($name) |
||
| 235 | |||
| 236 | public function setOptionDefaultValue($name, $defaultValue) |
||
| 240 | |||
| 241 | public function addOption($name, $description, $defaultValue = false) |
||
| 251 | |||
| 252 | public function determineOptionsFromParameters($params) |
||
| 266 | |||
| 267 | public function getArgumentDescription($name) |
||
| 275 | |||
| 276 | public function getOptionDescription($name) |
||
| 284 | |||
| 285 | protected function isAssoc($arr) |
||
| 292 | |||
| 293 | public function getAnnotations() |
||
| 297 | |||
| 298 | public function getAnnotation($annotation) |
||
| 306 | |||
| 307 | public function hasAnnotation($annotation) |
||
| 311 | |||
| 312 | protected function convertName($camel) |
||
| 319 | |||
| 320 | public function setExampleUsage($usage, $description) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Parse the docBlock comment for this command, and set the |
||
| 327 | * fields of this class with the data thereby obtained. |
||
| 328 | */ |
||
| 329 | protected function parseDocBlock($docblock) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Save any tag that we do not explicitly recognize in the |
||
| 337 | * 'otherAnnotations' map. |
||
| 338 | */ |
||
| 339 | public function addOtherAnnotation($name, $content) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * An option might have a name such as 'silent|s'. In this |
||
| 346 | * instance, we will allow the @option or @default tag to |
||
| 347 | * reference the option only by name (e.g. 'silent' or 's' |
||
| 348 | * instead of 'silent|s'). |
||
| 349 | */ |
||
| 350 | public function findMatchingOption($optionName) |
||
| 378 | /** |
||
| 379 | * Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', |
||
| 380 | * convert the data into the last of these forms. |
||
| 381 | */ |
||
| 382 | protected static function convertListToCommaSeparated($text) |
||
| 386 | } |
||
| 387 |