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) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns all actions. |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | protected function getAllActions() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Returns all actions. |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | protected function getAllKinds() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Returns revision log identifier. |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | protected function getRevisionLogIdentifier() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Shows error about missing revisions. |
||
| 385 | * |
||
| 386 | * @param array $missing_revisions Missing revisions. |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | protected function getMissingRevisionsErrorMessage(array $missing_revisions) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Returns list of revisions by path. |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | * @throws CommandException When given refs doesn't exist. |
||
| 410 | */ |
||
| 411 | protected function getRevisionsByPath() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Returns difference between 2 paths. |
||
| 463 | * |
||
| 464 | * @param string $main_path Main path. |
||
| 465 | * @param string $sub_path Sub path. |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | private function _getPathDifference($main_path, $sub_path) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns displayed revision limit. |
||
| 497 | * |
||
| 498 | * @return integer |
||
| 499 | */ |
||
| 500 | protected function getMaxCount() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Prints revisions. |
||
| 513 | * |
||
| 514 | * @param array $revisions Revisions. |
||
| 515 | * |
||
| 516 | * @return void |
||
| 517 | */ |
||
| 518 | protected function printRevisions(array $revisions) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Returns list of config settings. |
||
| 542 | * |
||
| 543 | * @return AbstractConfigSetting[] |
||
| 544 | */ |
||
| 545 | public function getConfigSettings() |
||
| 553 | |||
| 554 | } |
||
| 555 |
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.