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 |
||
| 30 | class LogCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand |
||
| 31 | { |
||
| 32 | |||
| 33 | const SETTING_LOG_LIMIT = 'log.limit'; |
||
| 34 | |||
| 35 | const SETTING_LOG_MERGE_CONFLICT_REGEXPS = 'log.merge-conflict-regexps'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Revision list parser. |
||
| 39 | * |
||
| 40 | * @var RevisionListParser |
||
| 41 | */ |
||
| 42 | private $_revisionListParser; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Revision log |
||
| 46 | * |
||
| 47 | * @var RevisionLog |
||
| 48 | */ |
||
| 49 | private $_revisionLog; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Prepare dependencies. |
||
| 53 | * |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | protected function prepareDependencies() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
|
|
|||
| 67 | */ |
||
| 68 | protected function configure() |
||
| 69 | { |
||
| 70 | $this->pathAcceptsUrl = true; |
||
| 71 | |||
| 72 | $description = <<<TEXT |
||
| 73 | TODO |
||
| 74 | TEXT; |
||
| 75 | |||
| 76 | $this |
||
| 77 | ->setName('log') |
||
| 78 | ->setDescription( |
||
| 79 | 'Show the log messages for revisions/bugs/path' |
||
| 80 | ) |
||
| 81 | ->setHelp($description) |
||
| 82 | ->addArgument( |
||
| 83 | 'path', |
||
| 84 | InputArgument::OPTIONAL, |
||
| 85 | 'Working copy path or URL', |
||
| 86 | '.' |
||
| 87 | ) |
||
| 88 | ->addOption( |
||
| 89 | 'revisions', |
||
| 90 | 'r', |
||
| 91 | InputOption::VALUE_REQUIRED, |
||
| 92 | 'Revision or revision range (e.g. "53324,34342,1224-4433,232")' |
||
| 93 | ) |
||
| 94 | ->addOption( |
||
| 95 | 'bugs', |
||
| 96 | 'b', |
||
| 97 | InputOption::VALUE_REQUIRED, |
||
| 98 | 'Bugs to merge (e.g. "JRA-1234,43644")' |
||
| 99 | ) |
||
| 100 | ->addOption( |
||
| 101 | 'refs', |
||
| 102 | null, |
||
| 103 | InputOption::VALUE_REQUIRED, |
||
| 104 | 'Refs (e.g. "trunk", "branches/branch-name", "tags/tag-name")' |
||
| 105 | ) |
||
| 106 | ->addOption( |
||
| 107 | 'details', |
||
| 108 | 'd', |
||
| 109 | InputOption::VALUE_NONE, |
||
| 110 | 'Shows paths affected in each revision' |
||
| 111 | ) |
||
| 112 | ->addOption( |
||
| 113 | 'summary', |
||
| 114 | 's', |
||
| 115 | InputOption::VALUE_NONE, |
||
| 116 | 'Shows summary of paths affected in each revision' |
||
| 117 | ) |
||
| 118 | ->addOption( |
||
| 119 | 'merge-oracle', |
||
| 120 | null, |
||
| 121 | InputOption::VALUE_NONE, |
||
| 122 | 'Detects commits with possible merge conflicts' |
||
| 123 | ) |
||
| 124 | ->addOption( |
||
| 125 | 'merges', |
||
| 126 | null, |
||
| 127 | InputOption::VALUE_NONE, |
||
| 128 | 'Print only merge commits' |
||
| 129 | ) |
||
| 130 | ->addOption( |
||
| 131 | 'no-merges', |
||
| 132 | null, |
||
| 133 | InputOption::VALUE_NONE, |
||
| 134 | 'Do not print merge commits' |
||
| 135 | ) |
||
| 136 | ->addOption( |
||
| 137 | 'merged', |
||
| 138 | null, |
||
| 139 | InputOption::VALUE_NONE, |
||
| 140 | 'Print only merged commits' |
||
| 141 | ) |
||
| 142 | ->addOption( |
||
| 143 | 'merged-by', |
||
| 144 | null, |
||
| 145 | InputOption::VALUE_REQUIRED, |
||
| 146 | 'Show revisions merged via given revision(-s)' |
||
| 147 | ) |
||
| 148 | ->addOption( |
||
| 149 | 'not-merged', |
||
| 150 | null, |
||
| 151 | InputOption::VALUE_NONE, |
||
| 152 | 'Print only not merged commits' |
||
| 153 | ) |
||
| 154 | ->addOption( |
||
| 155 | 'merge-status', |
||
| 156 | null, |
||
| 157 | InputOption::VALUE_NONE, |
||
| 158 | 'Show merge revisions (if any) for each revisions' |
||
| 159 | ) |
||
| 160 | ->addOption( |
||
| 161 | 'max-count', |
||
| 162 | null, |
||
| 163 | InputOption::VALUE_REQUIRED, |
||
| 164 | 'Limit the number of revisions to output' |
||
| 165 | ); |
||
| 166 | |||
| 167 | parent::configure(); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Return possible values for the named option |
||
| 172 | * |
||
| 173 | * @param string $optionName Option name. |
||
| 174 | * @param CompletionContext $context Completion context. |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function completeOptionValues($optionName, CompletionContext $context) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function initialize(InputInterface $input, OutputInterface $output) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Shows error about missing revisions. |
||
| 285 | * |
||
| 286 | * @param array $missing_revisions Missing revisions. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | protected function getMissingRevisionsErrorMessage(array $missing_revisions) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns list of revisions by path. |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | * @throws CommandException When given refs doesn't exist. |
||
| 310 | */ |
||
| 311 | protected function getRevisionsByPath() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns displayed revision limit. |
||
| 342 | * |
||
| 343 | * @return integer |
||
| 344 | */ |
||
| 345 | protected function getMaxCount() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Prints revisions. |
||
| 358 | * |
||
| 359 | * @param array $revisions Revisions. |
||
| 360 | * @param boolean $with_details Print extended revision details (e.g. paths changed). |
||
| 361 | * |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | protected function printRevisions(array $revisions, $with_details = false) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Generates change summary for a revision. |
||
| 502 | * |
||
| 503 | * @param array $revision_paths Revision paths. |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | protected function generateChangeSummary(array $revision_paths) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Returns merge conflict path predictions. |
||
| 538 | * |
||
| 539 | * @param array $revision_paths Revision paths. |
||
| 540 | * @param array $merge_conflict_regexps Merge conflict paths. |
||
| 541 | * |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | protected function getMergeConflictPrediction(array $revision_paths, array $merge_conflict_regexps) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Returns merge conflict regexps. |
||
| 565 | * |
||
| 566 | * @return array |
||
| 567 | */ |
||
| 568 | protected function getMergeConflictRegExps() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns relative path to "svn log" returned path. |
||
| 575 | * |
||
| 576 | * @param array $path_data Path data. |
||
| 577 | * @param string $path_key Path key. |
||
| 578 | * @param string $repository_path Repository path. |
||
| 579 | * |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | private function _getRelativeLogPath(array $path_data, $path_key, $repository_path) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Returns URL to the working copy. |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | protected function getWorkingCopyUrl() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Returns list of config settings. |
||
| 617 | * |
||
| 618 | * @return AbstractConfigSetting[] |
||
| 619 | */ |
||
| 620 | public function getConfigSettings() |
||
| 627 | |||
| 628 | } |
||
| 629 |