Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SilverstripeCommand 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 SilverstripeCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | abstract class SilverstripeCommand extends SymfonyCommand |
||
|
|
|||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The input interface implementation. |
||
| 25 | * |
||
| 26 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 27 | */ |
||
| 28 | protected $input; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The output interface implementation. |
||
| 32 | * |
||
| 33 | * @var \OutputStyle |
||
| 34 | */ |
||
| 35 | protected $output; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The name and signature of the console command. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $signature; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The console command name. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $name; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The console command description. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $description; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The default verbosity of output commands. |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $verbosity = OutputInterface::VERBOSITY_NORMAL; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The mapping between human readable verbosity levels and Symfony's OutputInterface. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $verbosityMap = [ |
||
| 71 | 'v' => OutputInterface::VERBOSITY_VERBOSE, |
||
| 72 | 'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE, |
||
| 73 | 'vvv' => OutputInterface::VERBOSITY_DEBUG, |
||
| 74 | 'quiet' => OutputInterface::VERBOSITY_QUIET, |
||
| 75 | 'normal' => OutputInterface::VERBOSITY_NORMAL, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Create a new console command instance. |
||
| 80 | * |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function __construct() |
||
| 84 | { |
||
| 85 | // We will go ahead and set the name, description, and parameters on console |
||
| 86 | // commands just to make things a little easier on the developer. This is |
||
| 87 | // so they don't have to all be manually specified in the constructors. |
||
| 88 | if (isset($this->signature)) { |
||
| 89 | $this->configureUsingFluentDefinition(); |
||
| 90 | } else { |
||
| 91 | parent::__construct($this->name); |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->setDescription($this->description); |
||
| 95 | |||
| 96 | if (!isset($this->signature)) { |
||
| 97 | $this->specifyParameters(); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Configure the console command using a fluent definition. |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | protected function configureUsingFluentDefinition() |
||
| 107 | { |
||
| 108 | list($name, $arguments, $options) = \CommandParser::parse($this->signature); |
||
| 109 | |||
| 110 | parent::__construct($name); |
||
| 111 | |||
| 112 | foreach ($arguments as $argument) { |
||
| 113 | $this->getDefinition()->addArgument($argument); |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach ($options as $option) { |
||
| 117 | $this->getDefinition()->addOption($option); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Specify the arguments and options on the command. |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | protected function specifyParameters() |
||
| 127 | { |
||
| 128 | // We will loop through all of the arguments and options for the command and |
||
| 129 | // set them all on the base command instance. This specifies what can get |
||
| 130 | // passed into these commands as "parameters" to control the execution. |
||
| 131 | foreach ($this->getArguments() as $arguments) { |
||
| 132 | call_user_func_array([$this, 'addArgument'], $arguments); |
||
| 133 | } |
||
| 134 | |||
| 135 | foreach ($this->getOptions() as $options) { |
||
| 136 | call_user_func_array([$this, 'addOption'], $options); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Run the console command. |
||
| 142 | * |
||
| 143 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 144 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
| 145 | * |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | public function run(InputInterface $input, OutputInterface $output) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Execute the console command. |
||
| 159 | * |
||
| 160 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 161 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
| 162 | * |
||
| 163 | * @return mixed |
||
| 164 | */ |
||
| 165 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 176 | |||
| 177 | abstract public function fire(); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Call another console command. |
||
| 181 | * |
||
| 182 | * @param string $command |
||
| 183 | * @param array $arguments |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function call($command, array $arguments = []) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Call another console command silently. |
||
| 198 | * |
||
| 199 | * @param string $command |
||
| 200 | * @param array $arguments |
||
| 201 | * |
||
| 202 | * @return int |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function callSilent($command, array $arguments = []) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get the value of a command argument. |
||
| 215 | * |
||
| 216 | * @param string $key |
||
| 217 | * |
||
| 218 | * @return string|array |
||
| 219 | */ |
||
| 220 | public function argument($key = null) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the value of a command option. |
||
| 231 | * |
||
| 232 | * @param string $key |
||
| 233 | * |
||
| 234 | * @return string|array |
||
| 235 | */ |
||
| 236 | public function option($key = null) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Confirm a question with the user. |
||
| 247 | * |
||
| 248 | * @param string $question |
||
| 249 | * @param bool $default |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function confirm($question, $default = false) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Prompt the user for input. |
||
| 260 | * |
||
| 261 | * @param string $question |
||
| 262 | * @param string $default |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function ask($question, $default = null) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Prompt the user for input with auto completion. |
||
| 273 | * |
||
| 274 | * @param string $question |
||
| 275 | * @param array $choices |
||
| 276 | * @param string $default |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function anticipate($question, array $choices, $default = null) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Prompt the user for input with auto completion. |
||
| 287 | * |
||
| 288 | * @param string $question |
||
| 289 | * @param array $choices |
||
| 290 | * @param string $default |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | public function askWithCompletion($question, array $choices, $default = null) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Prompt the user for input but hide the answer from the console. |
||
| 305 | * |
||
| 306 | * @param string $question |
||
| 307 | * @param bool $fallback |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function secret($question, $fallback = true) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Give the user a single choice from an array of answers. |
||
| 322 | * |
||
| 323 | * @param string $question |
||
| 324 | * @param array $choices |
||
| 325 | * @param string $default |
||
| 326 | * @param mixed $attempts |
||
| 327 | * @param bool $multiple |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | public function choice($question, array $choices, $default = null, $attempts = null, $multiple = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Format input to textual table. |
||
| 342 | * |
||
| 343 | * @param array $headers |
||
| 344 | * @param array $rows |
||
| 345 | * @param string $style |
||
| 346 | * |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function table(array $headers, $rows, $style = 'default') |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Write a string as information output. |
||
| 358 | * |
||
| 359 | * @param string $string |
||
| 360 | * @param null|int|string $verbosity |
||
| 361 | * |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | public function info($string, $verbosity = null) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Write a string as standard output. |
||
| 371 | * |
||
| 372 | * @param string $string |
||
| 373 | * @param string $style |
||
| 374 | * @param null|int|string $verbosity |
||
| 375 | * |
||
| 376 | * @return void |
||
| 377 | */ |
||
| 378 | public function line($string, $style = null, $verbosity = null) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Write a string as comment output. |
||
| 387 | * |
||
| 388 | * @param string $string |
||
| 389 | * @param null|int|string $verbosity |
||
| 390 | * |
||
| 391 | * @return void |
||
| 392 | */ |
||
| 393 | public function comment($string, $verbosity = null) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Write a string as question output. |
||
| 400 | * |
||
| 401 | * @param string $string |
||
| 402 | * @param null|int|string $verbosity |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | public function question($string, $verbosity = null) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Write a string as error output. |
||
| 413 | * |
||
| 414 | * @param string $string |
||
| 415 | * @param null|int|string $verbosity |
||
| 416 | * |
||
| 417 | * @return void |
||
| 418 | */ |
||
| 419 | public function error($string, $verbosity = null) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Write a string as warning output. |
||
| 426 | * |
||
| 427 | * @param string $string |
||
| 428 | * @param null|int|string $verbosity |
||
| 429 | * |
||
| 430 | * @return void |
||
| 431 | */ |
||
| 432 | public function warn($string, $verbosity = null) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Get the verbosity level in terms of Symfony's OutputInterface level. |
||
| 445 | * |
||
| 446 | * @param string|int $level |
||
| 447 | * |
||
| 448 | * @return int |
||
| 449 | */ |
||
| 450 | protected function parseVerbosity($level = null) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Set the verbosity level. |
||
| 463 | * |
||
| 464 | * @param string|int $level |
||
| 465 | * |
||
| 466 | * @return void |
||
| 467 | */ |
||
| 468 | protected function setVerbosity($level) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get the console command arguments. |
||
| 475 | * |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | protected function getArguments() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Get the console command options. |
||
| 485 | * |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | protected function getOptions() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Get the output implementation. |
||
| 495 | * |
||
| 496 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
| 497 | */ |
||
| 498 | public function getOutput() |
||
| 502 | } |
||
| 503 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.