Complex classes like MergeCommand 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 MergeCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class MergeCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand |
||
| 30 | { |
||
| 31 | |||
| 32 | const SETTING_MERGE_SOURCE_URL = 'merge.source-url'; |
||
| 33 | |||
| 34 | const SETTING_MERGE_RECENT_CONFLICTS = 'merge.recent-conflicts'; |
||
| 35 | |||
| 36 | const REVISION_ALL = 'all'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Merge source detector. |
||
| 40 | * |
||
| 41 | * @var AbstractMergeSourceDetector |
||
| 42 | */ |
||
| 43 | private $_mergeSourceDetector; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Revision list parser. |
||
| 47 | * |
||
| 48 | * @var RevisionListParser |
||
| 49 | */ |
||
| 50 | private $_revisionListParser; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Unmerged revisions. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $_unmergedRevisions = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Url resolver. |
||
| 61 | * |
||
| 62 | * @var UrlResolver |
||
| 63 | */ |
||
| 64 | private $_urlResolver; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Prepare dependencies. |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | protected function prepareDependencies() |
||
| 72 | { |
||
| 73 | parent::prepareDependencies(); |
||
| 74 | |||
| 75 | $container = $this->getContainer(); |
||
| 76 | |||
| 77 | $this->_mergeSourceDetector = $container['merge_source_detector']; |
||
| 78 | $this->_revisionListParser = $container['revision_list_parser']; |
||
| 79 | $this->_urlResolver = $container['repository_url_resolver']; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
|
|
|||
| 84 | */ |
||
| 85 | protected function configure() |
||
| 86 | { |
||
| 87 | $this |
||
| 88 | ->setName('merge') |
||
| 89 | ->setDescription('Merge changes from another project or ref within same project into a working copy') |
||
| 90 | ->addArgument( |
||
| 91 | 'path', |
||
| 92 | InputArgument::OPTIONAL, |
||
| 93 | 'Working copy path', |
||
| 94 | '.' |
||
| 95 | ) |
||
| 96 | ->addOption( |
||
| 97 | 'source-url', |
||
| 98 | null, |
||
| 99 | InputOption::VALUE_REQUIRED, |
||
| 100 | 'Merge source url (absolute or relative) or ref name, e.g. <comment>branches/branch-name</comment>' |
||
| 101 | ) |
||
| 102 | ->addOption( |
||
| 103 | 'revisions', |
||
| 104 | 'r', |
||
| 105 | InputOption::VALUE_REQUIRED, |
||
| 106 | 'List of revision(-s) and/or revision range(-s) to merge, e.g. <comment>53324</comment>, <comment>1224-4433</comment> or <comment>all</comment>' |
||
| 107 | ) |
||
| 108 | ->addOption( |
||
| 109 | 'bugs', |
||
| 110 | 'b', |
||
| 111 | InputOption::VALUE_REQUIRED, |
||
| 112 | 'List of bug(-s) to merge, e.g. <comment>JRA-1234</comment>, <comment>43644</comment>' |
||
| 113 | ) |
||
| 114 | ->addOption( |
||
| 115 | 'with-details', |
||
| 116 | 'd', |
||
| 117 | InputOption::VALUE_NONE, |
||
| 118 | 'Shows detailed revision information, e.g. paths affected' |
||
| 119 | ) |
||
| 120 | ->addOption( |
||
| 121 | 'with-summary', |
||
| 122 | 's', |
||
| 123 | InputOption::VALUE_NONE, |
||
| 124 | 'Shows number of added/changed/removed paths in the revision' |
||
| 125 | ) |
||
| 126 | /*->addOption( |
||
| 127 | 'rollback', |
||
| 128 | null, |
||
| 129 | InputOption::VALUE_NONE, |
||
| 130 | 'Do a rollback merge' |
||
| 131 | ) |
||
| 132 | ->addOption( |
||
| 133 | 'record-only', |
||
| 134 | null, |
||
| 135 | InputOption::VALUE_NONE, |
||
| 136 | 'Only mark revisions as merged' |
||
| 137 | )*/; |
||
| 138 | |||
| 139 | parent::configure(); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return possible values for the named option |
||
| 144 | * |
||
| 145 | * @param string $optionName Option name. |
||
| 146 | * @param CompletionContext $context Completion context. |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function completeOptionValues($optionName, CompletionContext $context) |
||
| 151 | { |
||
| 152 | $ret = parent::completeOptionValues($optionName, $context); |
||
| 153 | |||
| 154 | if ( $optionName === 'revisions' ) { |
||
| 155 | return array('all'); |
||
| 156 | } |
||
| 157 | |||
| 158 | if ( $optionName == 'source-url' ) { |
||
| 159 | return $this->getAllRefs(); |
||
| 160 | } |
||
| 161 | |||
| 162 | return $ret; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Determines if all unmerged revisions should be merged. |
||
| 227 | * |
||
| 228 | * @param array $revisions Revisions. |
||
| 229 | * |
||
| 230 | * @return boolean |
||
| 231 | */ |
||
| 232 | protected function shouldMergeAll(array $revisions) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Ensures, that working copy is up to date. |
||
| 239 | * |
||
| 240 | * @param string $wc_path Working copy path. |
||
| 241 | * |
||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | protected function ensureLatestWorkingCopy($wc_path) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Updates working copy. |
||
| 272 | * |
||
| 273 | * @param string $wc_path Working copy path. |
||
| 274 | * |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | protected function updateWorkingCopy($wc_path) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns source url for merge. |
||
| 284 | * |
||
| 285 | * @param string $wc_path Working copy path. |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | * @throws CommandException When source path is invalid. |
||
| 289 | */ |
||
| 290 | protected function getSourceUrl($wc_path) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Prints information about merge source & target. |
||
| 322 | * |
||
| 323 | * @param string $source_url Merge source: url. |
||
| 324 | * @param string $wc_path Merge target: working copy path. |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | protected function printSourceAndTarget($source_url, $wc_path) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Ensures, that there are some unmerged revisions. |
||
| 339 | * |
||
| 340 | * @param string $source_url Merge source: url. |
||
| 341 | * @param string $wc_path Merge target: working copy path. |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | protected function getUnmergedRevisions($source_url, $wc_path) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Returns not merged revisions. |
||
| 367 | * |
||
| 368 | * @param string $source_url Merge source: url. |
||
| 369 | * @param string $wc_path Merge target: working copy path. |
||
| 370 | * |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | protected function calculateUnmergedRevisions($source_url, $wc_path) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Parses information from "svn:mergeinfo" property. |
||
| 400 | * |
||
| 401 | * @param string $source_path Merge source: path in repository. |
||
| 402 | * @param string $wc_path Merge target: working copy path. |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | protected function getMergedRevisions($source_path, $wc_path) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Validates revisions to actually exist. |
||
| 424 | * |
||
| 425 | * @param array $revisions Revisions. |
||
| 426 | * @param string $repository_url Repository url. |
||
| 427 | * |
||
| 428 | * @return array |
||
| 429 | * @throws CommandException When revision doesn't exist. |
||
| 430 | */ |
||
| 431 | protected function getDirectRevisions(array $revisions, $repository_url) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Performs merge. |
||
| 448 | * |
||
| 449 | * @param string $source_url Merge source: url. |
||
| 450 | * @param string $wc_path Merge target: working copy path. |
||
| 451 | * @param array $revisions Revisions to merge. |
||
| 452 | * |
||
| 453 | * @return void |
||
| 454 | */ |
||
| 455 | protected function performMerge($source_url, $wc_path, array $revisions) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Ensures, that there are no unresolved conflicts in working copy. |
||
| 478 | * |
||
| 479 | * @param string $source_url Source url. |
||
| 480 | * @param string $wc_path Working copy path. |
||
| 481 | * @param integer $largest_suggested_revision Largest revision, that is suggested in error message. |
||
| 482 | * |
||
| 483 | * @return void |
||
| 484 | * @throws CommandException When merge conflicts detected. |
||
| 485 | */ |
||
| 486 | protected function ensureWorkingCopyWithoutConflicts($source_url, $wc_path, $largest_suggested_revision = null) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Adds new conflicts to already remembered ones. |
||
| 543 | * |
||
| 544 | * @param array $conflicts Conflicts. |
||
| 545 | * |
||
| 546 | * @return void |
||
| 547 | */ |
||
| 548 | protected function rememberConflicts(array $conflicts) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Returns revisions not larger, then given one. |
||
| 558 | * |
||
| 559 | * @param array $revisions Revisions. |
||
| 560 | * @param integer $max_revision Maximal revision. |
||
| 561 | * |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | protected function limitRevisions(array $revisions, $max_revision) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Returns list of config settings. |
||
| 579 | * |
||
| 580 | * @return AbstractConfigSetting[] |
||
| 581 | */ |
||
| 582 | public function getConfigSettings() |
||
| 589 | |||
| 590 | } |
||
| 591 |