| Total Complexity | 57 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DescribeCommand 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.
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 DescribeCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | final class DescribeCommand extends Command |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected static $defaultName = 'describe'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string[] |
||
| 57 | */ |
||
| 58 | private $setNames; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var FixerFactory |
||
| 62 | */ |
||
| 63 | private $fixerFactory; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array<string, FixerInterface> |
||
| 67 | */ |
||
| 68 | private $fixers; |
||
| 69 | |||
| 70 | public function __construct(?FixerFactory $fixerFactory = null) |
||
| 71 | { |
||
| 72 | parent::__construct(); |
||
| 73 | |||
| 74 | if (null === $fixerFactory) { |
||
| 75 | $fixerFactory = new FixerFactory(); |
||
| 76 | $fixerFactory->registerBuiltInFixers(); |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->fixerFactory = $fixerFactory; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | protected function configure(): void |
||
| 86 | { |
||
| 87 | $this |
||
| 88 | ->setDefinition( |
||
| 89 | [ |
||
| 90 | new InputArgument('name', InputArgument::REQUIRED, 'Name of rule / set.'), |
||
| 91 | ] |
||
| 92 | ) |
||
| 93 | ->setDescription('Describe rule / ruleset.') |
||
| 94 | ; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 101 | { |
||
| 102 | if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity() && $output instanceof ConsoleOutputInterface) { |
||
| 103 | $stdErr = $output->getErrorOutput(); |
||
| 104 | $stdErr->writeln($this->getApplication()->getLongVersion()); |
||
| 105 | $stdErr->writeln(sprintf('Runtime: <info>PHP %s</info>', PHP_VERSION)); |
||
| 106 | } |
||
| 107 | |||
| 108 | $name = $input->getArgument('name'); |
||
| 109 | |||
| 110 | try { |
||
| 111 | if ('@' === $name[0]) { |
||
| 112 | $this->describeSet($output, $name); |
||
| 113 | |||
| 114 | return 0; |
||
| 115 | } |
||
| 116 | |||
| 117 | $this->describeRule($output, $name); |
||
| 118 | } catch (DescribeNameNotFoundException $e) { |
||
| 119 | $matcher = new WordMatcher( |
||
| 120 | 'set' === $e->getType() ? $this->getSetNames() : array_keys($this->getFixers()) |
||
| 121 | ); |
||
| 122 | |||
| 123 | $alternative = $matcher->match($name); |
||
| 124 | |||
| 125 | $this->describeList($output, $e->getType()); |
||
| 126 | |||
| 127 | throw new \InvalidArgumentException(sprintf( |
||
| 128 | '%s "%s" not found.%s', |
||
| 129 | ucfirst($e->getType()), |
||
| 130 | $name, |
||
| 131 | null === $alternative ? '' : ' Did you mean "'.$alternative.'"?' |
||
| 132 | )); |
||
| 133 | } |
||
| 134 | |||
| 135 | return 0; |
||
| 136 | } |
||
| 137 | |||
| 138 | private function describeRule(OutputInterface $output, string $name): void |
||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | private function describeSet(OutputInterface $output, string $name): void |
||
| 308 | { |
||
| 309 | if (!\in_array($name, $this->getSetNames(), true)) { |
||
| 310 | throw new DescribeNameNotFoundException($name, 'set'); |
||
| 311 | } |
||
| 312 | |||
| 313 | $ruleSetDefinitions = RuleSets::getSetDefinitions(); |
||
| 314 | $fixers = $this->getFixers(); |
||
| 315 | |||
| 316 | $output->writeln(sprintf('<info>Description of the</info> %s <info>set.</info>', $ruleSetDefinitions[$name]->getName())); |
||
| 317 | $output->writeln($this->replaceRstLinks($ruleSetDefinitions[$name]->getDescription())); |
||
| 318 | |||
| 319 | if ($ruleSetDefinitions[$name]->isRisky()) { |
||
| 320 | $output->writeln('This set contains <error>risky</error> rules.'); |
||
| 321 | } |
||
| 322 | |||
| 323 | $output->writeln(''); |
||
| 324 | |||
| 325 | $help = ''; |
||
| 326 | |||
| 327 | foreach ($ruleSetDefinitions[$name]->getRules() as $rule => $config) { |
||
| 328 | if ('@' === $rule[0]) { |
||
| 329 | $set = $ruleSetDefinitions[$rule]; |
||
| 330 | $help .= sprintf( |
||
| 331 | " * <info>%s</info>%s\n | %s\n\n", |
||
| 332 | $rule, |
||
| 333 | $set->isRisky() ? ' <error>risky</error>' : '', |
||
| 334 | $this->replaceRstLinks($set->getDescription()) |
||
| 335 | ); |
||
| 336 | |||
| 337 | continue; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** @var FixerInterface $fixer */ |
||
| 341 | $fixer = $fixers[$rule]; |
||
| 342 | |||
| 343 | $definition = $fixer->getDefinition(); |
||
| 344 | $help .= sprintf( |
||
| 345 | " * <info>%s</info>%s\n | %s\n%s\n", |
||
| 346 | $rule, |
||
| 347 | $fixer->isRisky() ? ' <error>risky</error>' : '', |
||
| 348 | $definition->getSummary(), |
||
| 349 | true !== $config ? sprintf(" <comment>| Configuration: %s</comment>\n", HelpCommand::toString($config)) : '' |
||
| 350 | ); |
||
| 351 | } |
||
| 352 | |||
| 353 | $output->write($help); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return array<string, FixerInterface> |
||
| 358 | */ |
||
| 359 | private function getFixers(): array |
||
| 360 | { |
||
| 361 | if (null !== $this->fixers) { |
||
| 362 | return $this->fixers; |
||
| 363 | } |
||
| 364 | |||
| 365 | $fixers = []; |
||
| 366 | |||
| 367 | foreach ($this->fixerFactory->getFixers() as $fixer) { |
||
| 368 | $fixers[$fixer->getName()] = $fixer; |
||
| 369 | } |
||
| 370 | |||
| 371 | $this->fixers = $fixers; |
||
| 372 | ksort($this->fixers); |
||
| 373 | |||
| 374 | return $this->fixers; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return string[] |
||
| 379 | */ |
||
| 380 | private function getSetNames(): array |
||
| 381 | { |
||
| 382 | if (null !== $this->setNames) { |
||
| 383 | return $this->setNames; |
||
| 384 | } |
||
| 385 | |||
| 386 | $this->setNames = RuleSets::getSetDefinitionNames(); |
||
| 387 | |||
| 388 | return $this->setNames; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $type 'rule'|'set' |
||
| 393 | */ |
||
| 394 | private function describeList(OutputInterface $output, string $type): void |
||
| 395 | { |
||
| 396 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { |
||
| 397 | $describe = [ |
||
| 398 | 'sets' => $this->getSetNames(), |
||
| 399 | 'rules' => $this->getFixers(), |
||
| 400 | ]; |
||
| 401 | } elseif ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
||
| 402 | $describe = 'set' === $type ? ['sets' => $this->getSetNames()] : ['rules' => $this->getFixers()]; |
||
| 403 | } else { |
||
| 404 | return; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** @var string[] $items */ |
||
| 408 | foreach ($describe as $list => $items) { |
||
| 409 | $output->writeln(sprintf('<comment>Defined %s:</comment>', $list)); |
||
| 410 | |||
| 411 | foreach ($items as $name => $item) { |
||
| 412 | $output->writeln(sprintf('* <info>%s</info>', \is_string($name) ? $name : $item)); |
||
| 413 | } |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | private function replaceRstLinks(string $content): string |
||
| 431 | ); |
||
| 432 | } |
||
| 433 | } |
||
| 434 |