Complex classes like LogCommand 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 LogCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class LogCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand |
||
| 28 | { |
||
| 29 | |||
| 30 | const SETTING_LOG_LIMIT = 'log.limit'; |
||
| 31 | |||
| 32 | const SETTING_LOG_MESSAGE_LIMIT = 'log.message-limit'; |
||
| 33 | |||
| 34 | const SETTING_LOG_MERGE_CONFLICT_REGEXPS = 'log.merge-conflict-regexps'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Revision list parser. |
||
| 38 | * |
||
| 39 | * @var RevisionListParser |
||
| 40 | */ |
||
| 41 | private $_revisionListParser; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Revision log |
||
| 45 | * |
||
| 46 | * @var RevisionLog |
||
| 47 | */ |
||
| 48 | private $_revisionLog; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Revision printer. |
||
| 52 | * |
||
| 53 | * @var RevisionPrinter |
||
| 54 | */ |
||
| 55 | private $_revisionPrinter; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Prepare dependencies. |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | protected function prepareDependencies() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | protected function configure() |
||
| 76 | { |
||
| 77 | $this->pathAcceptsUrl = true; |
||
| 78 | |||
| 79 | $this |
||
| 80 | ->setName('log') |
||
| 81 | ->setDescription( |
||
| 82 | 'Show the log messages for a set of revisions, bugs, paths, refs, etc.' |
||
| 83 | ) |
||
| 84 | ->addArgument( |
||
| 85 | 'path', |
||
| 86 | InputArgument::OPTIONAL, |
||
| 87 | 'Working copy path or URL', |
||
| 88 | '.' |
||
| 89 | ) |
||
| 90 | ->addOption( |
||
| 91 | 'revisions', |
||
| 92 | 'r', |
||
| 93 | InputOption::VALUE_REQUIRED, |
||
| 94 | 'List of revision(-s) and/or revision range(-s), e.g. <comment>53324</comment>, <comment>1224-4433</comment>' |
||
| 95 | ) |
||
| 96 | ->addOption( |
||
| 97 | 'bugs', |
||
| 98 | 'b', |
||
| 99 | InputOption::VALUE_REQUIRED, |
||
| 100 | 'List of bug(-s), e.g. <comment>JRA-1234</comment>, <comment>43644</comment>' |
||
| 101 | ) |
||
| 102 | ->addOption( |
||
| 103 | 'refs', |
||
| 104 | null, |
||
| 105 | InputOption::VALUE_REQUIRED, |
||
| 106 | 'List of refs, e.g. <comment>trunk</comment>, <comment>branches/branch-name</comment>, <comment>tags/tag-name</comment>' |
||
| 107 | ) |
||
| 108 | ->addOption( |
||
| 109 | 'merges', |
||
| 110 | null, |
||
| 111 | InputOption::VALUE_NONE, |
||
| 112 | 'Show merge revisions only' |
||
| 113 | ) |
||
| 114 | ->addOption( |
||
| 115 | 'no-merges', |
||
| 116 | null, |
||
| 117 | InputOption::VALUE_NONE, |
||
| 118 | 'Hide merge revisions' |
||
| 119 | ) |
||
| 120 | ->addOption( |
||
| 121 | 'merged', |
||
| 122 | null, |
||
| 123 | InputOption::VALUE_NONE, |
||
| 124 | 'Shows only revisions, that were merged at least once' |
||
| 125 | ) |
||
| 126 | ->addOption( |
||
| 127 | 'not-merged', |
||
| 128 | null, |
||
| 129 | InputOption::VALUE_NONE, |
||
| 130 | 'Shows only revisions, that were not merged' |
||
| 131 | ) |
||
| 132 | ->addOption( |
||
| 133 | 'merged-by', |
||
| 134 | null, |
||
| 135 | InputOption::VALUE_REQUIRED, |
||
| 136 | 'Show revisions merged by list of revision(-s) and/or revision range(-s)' |
||
| 137 | ) |
||
| 138 | ->addOption( |
||
| 139 | 'action', |
||
| 140 | null, |
||
| 141 | InputOption::VALUE_REQUIRED, |
||
| 142 | 'Show revisions, whose paths were affected by specified action, e.g. <comment>A</comment>, <comment>M</comment>, <comment>R</comment>, <comment>D</comment>' |
||
|
|
|||
| 143 | ) |
||
| 144 | ->addOption( |
||
| 145 | 'kind', |
||
| 146 | null, |
||
| 147 | InputOption::VALUE_REQUIRED, |
||
| 148 | 'Show revisions, whose paths match specified kind, e.g. <comment>dir</comment> or <comment>file</comment>' |
||
| 149 | ) |
||
| 150 | ->addOption( |
||
| 151 | 'with-details', |
||
| 152 | 'd', |
||
| 153 | InputOption::VALUE_NONE, |
||
| 154 | 'Shows detailed revision information, e.g. paths affected' |
||
| 155 | ) |
||
| 156 | ->addOption( |
||
| 157 | 'with-summary', |
||
| 158 | 's', |
||
| 159 | InputOption::VALUE_NONE, |
||
| 160 | 'Shows number of added/changed/removed paths in the revision' |
||
| 161 | ) |
||
| 162 | ->addOption( |
||
| 163 | 'with-refs', |
||
| 164 | null, |
||
| 165 | InputOption::VALUE_NONE, |
||
| 166 | 'Shows revision refs' |
||
| 167 | ) |
||
| 168 | ->addOption( |
||
| 169 | 'with-merge-oracle', |
||
| 170 | null, |
||
| 171 | InputOption::VALUE_NONE, |
||
| 172 | 'Shows number of paths in the revision, that can cause conflict upon merging' |
||
| 173 | ) |
||
| 174 | ->addOption( |
||
| 175 | 'with-merge-status', |
||
| 176 | null, |
||
| 177 | InputOption::VALUE_NONE, |
||
| 178 | 'Shows merge revisions affecting this revision' |
||
| 179 | ) |
||
| 180 | ->addOption( |
||
| 181 | 'max-count', |
||
| 182 | null, |
||
| 183 | InputOption::VALUE_REQUIRED, |
||
| 184 | 'Limit the number of revisions to output' |
||
| 185 | ); |
||
| 186 | |||
| 187 | parent::configure(); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Return possible values for the named option |
||
| 192 | * |
||
| 193 | * @param string $optionName Option name. |
||
| 194 | * @param CompletionContext $context Completion context. |
||
| 195 | * |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function completeOptionValues($optionName, CompletionContext $context) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | public function initialize(InputInterface $input, OutputInterface $output) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | * |
||
| 228 | * @throws \RuntimeException When both "--bugs" and "--revisions" options were specified. |
||
| 229 | * @throws CommandException When specified revisions are not present in current project. |
||
| 230 | * @throws CommandException When project contains no associated revisions. |
||
| 231 | */ |
||
| 232 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns all actions. |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | protected function getAllActions() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns all actions. |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | protected function getAllKinds() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns revision log identifier. |
||
| 369 | * |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | protected function getRevisionLogIdentifier() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Shows error about missing revisions. |
||
| 390 | * |
||
| 391 | * @param array $missing_revisions Missing revisions. |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | protected function getMissingRevisionsErrorMessage(array $missing_revisions) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns list of revisions by path. |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | * @throws CommandException When given path doesn't exist. |
||
| 415 | * @throws CommandException When given refs doesn't exist. |
||
| 416 | */ |
||
| 417 | protected function getRevisionsByPath() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Returns difference between 2 paths. |
||
| 455 | * |
||
| 456 | * @param string $main_path Main path. |
||
| 457 | * @param string $sub_path Sub path. |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | private function _getPathDifference($main_path, $sub_path) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Determines path kind from repository. |
||
| 489 | * |
||
| 490 | * @param string $path Path. |
||
| 491 | * |
||
| 492 | * @return string|null |
||
| 493 | */ |
||
| 494 | private function _crossReferencePathFromRepository($path) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Returns displayed revision limit. |
||
| 510 | * |
||
| 511 | * @return integer |
||
| 512 | */ |
||
| 513 | protected function getMaxCount() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Prints revisions. |
||
| 526 | * |
||
| 527 | * @param array $revisions Revisions. |
||
| 528 | * |
||
| 529 | * @return void |
||
| 530 | */ |
||
| 531 | protected function printRevisions(array $revisions) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Returns list of config settings. |
||
| 555 | * |
||
| 556 | * @return AbstractConfigSetting[] |
||
| 557 | */ |
||
| 558 | public function getConfigSettings() |
||
| 566 | |||
| 567 | } |
||
| 568 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.