Complex classes like ConfigCommand 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 ConfigCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ConfigCommand extends AbstractCommand implements IAggregatorAwareCommand |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Editor. |
||
| 31 | * |
||
| 32 | * @var InteractiveEditor |
||
| 33 | */ |
||
| 34 | private $_editor; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Config editor. |
||
| 38 | * |
||
| 39 | * @var ConfigEditor |
||
| 40 | */ |
||
| 41 | private $_configEditor; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Config settings. |
||
| 45 | * |
||
| 46 | * @var AbstractConfigSetting[] |
||
| 47 | */ |
||
| 48 | protected $configSettings = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | protected function configure() |
||
| 54 | { |
||
| 55 | $this |
||
| 56 | ->setName('config') |
||
| 57 | ->setDescription('Change configuration settings, that are used by other commands') |
||
| 58 | ->addArgument( |
||
| 59 | 'path', |
||
| 60 | InputArgument::OPTIONAL, |
||
| 61 | 'Working copy path', |
||
| 62 | '.' |
||
| 63 | ) |
||
| 64 | ->addOption( |
||
| 65 | 'show', |
||
| 66 | 's', |
||
| 67 | InputOption::VALUE_REQUIRED, |
||
| 68 | 'Shows only given (instead of all) setting value' |
||
| 69 | ) |
||
| 70 | ->addOption( |
||
| 71 | 'edit', |
||
| 72 | 'e', |
||
| 73 | InputOption::VALUE_REQUIRED, |
||
| 74 | 'Change setting value in the Interactive Editor' |
||
| 75 | ) |
||
| 76 | ->addOption( |
||
| 77 | 'delete', |
||
| 78 | 'd', |
||
| 79 | InputOption::VALUE_REQUIRED, |
||
| 80 | 'Delete setting' |
||
| 81 | ) |
||
| 82 | ->addOption( |
||
| 83 | 'global', |
||
| 84 | 'g', |
||
| 85 | InputOption::VALUE_NONE, |
||
| 86 | 'Operate on global instead of working copy-specific settings' |
||
| 87 | ); |
||
| 88 | |||
| 89 | parent::configure(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Prepare dependencies. |
||
| 94 | * |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | protected function prepareDependencies() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return possible values for the named option |
||
| 110 | * |
||
| 111 | * @param string $optionName Option name. |
||
| 112 | * @param CompletionContext $context Completion context. |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function completeOptionValues($optionName, CompletionContext $context) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Shows setting value. |
||
| 141 | * |
||
| 142 | * @return boolean |
||
| 143 | */ |
||
| 144 | protected function processShow() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Changes setting value. |
||
| 159 | * |
||
| 160 | * @return boolean |
||
| 161 | */ |
||
| 162 | protected function processEdit() |
||
| 163 | { |
||
| 164 | $setting_name = $this->io->getOption('edit'); |
||
| 165 | |||
| 166 | if ( $setting_name === null ) { |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | |||
| 170 | $config_setting = $this->getConfigSetting($setting_name); |
||
| 171 | $value = $config_setting->getValue($this->getValueFilter()); |
||
| 172 | $retry = false; |
||
| 173 | |||
| 174 | if ( $config_setting instanceof ArrayConfigSetting ) { |
||
| 175 | $value = implode(PHP_EOL, $value); |
||
| 176 | } |
||
| 177 | |||
| 178 | do { |
||
| 179 | try { |
||
| 180 | $retry = false; |
||
| 181 | |||
| 182 | if ( $config_setting instanceof ChoiceConfigSetting ) { |
||
| 183 | $value = $this->io->choose( |
||
| 184 | 'Please choose value for "' . $setting_name . '" config setting [' . $value . ']:', |
||
| 185 | $config_setting->getChoices(), |
||
| 186 | (string)$value, |
||
| 187 | 'Option value "%s" is invalid.' |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | else { |
||
| 191 | $value = $this->openEditor($value); |
||
| 192 | } |
||
| 193 | |||
| 194 | $config_setting->setValue($value, $this->getScopeFilter()); |
||
| 195 | $this->io->writeln('Setting <info>' . $setting_name . '</info> was edited.'); |
||
| 196 | } |
||
| 197 | catch ( \InvalidArgumentException $e ) { |
||
| 198 | $this->io->writeln(array('<error>' . $e->getMessage() . '</error>', '')); |
||
| 199 | |||
| 200 | if ( $this->io->askConfirmation('Retry editing', false) ) { |
||
| 201 | $retry = true; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } while ( $retry ); |
||
| 205 | |||
| 206 | return true; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Opens value editing. |
||
| 211 | * |
||
| 212 | * @param mixed $value Value. |
||
| 213 | * |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | protected function openEditor($value) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Deletes setting value. |
||
| 226 | * |
||
| 227 | * @return boolean |
||
| 228 | */ |
||
| 229 | protected function processDelete() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Lists values for every stored setting. |
||
| 246 | * |
||
| 247 | * @param string $setting_name Setting name. |
||
| 248 | * |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | protected function listSettings($setting_name = null) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Returns config settings filtered by scope. |
||
| 293 | * |
||
| 294 | * @param integer $scope_filter Scope filter. |
||
| 295 | * |
||
| 296 | * @return AbstractConfigSetting[] |
||
| 297 | */ |
||
| 298 | protected function getConfigSettingsByScope($scope_filter) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Validates setting name. |
||
| 313 | * |
||
| 314 | * @param string $name Setting name. |
||
| 315 | * |
||
| 316 | * @return AbstractConfigSetting |
||
| 317 | * @throws \InvalidArgumentException When non-existing/outside of scope setting given. |
||
| 318 | */ |
||
| 319 | protected function getConfigSetting($name) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns scope filter for viewing config settings. |
||
| 336 | * |
||
| 337 | * @return integer |
||
| 338 | */ |
||
| 339 | protected function getScopeFilter() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns value filter for editing config settings. |
||
| 346 | * |
||
| 347 | * @return integer |
||
| 348 | */ |
||
| 349 | protected function getValueFilter() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns possible settings with their defaults. |
||
| 356 | * |
||
| 357 | * @return AbstractConfigSetting[] |
||
| 358 | */ |
||
| 359 | protected function getConfigSettings() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Determines if global only config settings should be used. |
||
| 385 | * |
||
| 386 | * @return boolean |
||
| 387 | */ |
||
| 388 | protected function isGlobal() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns option names, that makes sense to use in aggregation mode. |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function getAggregatedOptions() |
||
| 407 | |||
| 408 | } |
||
| 409 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..