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 | |||
127 | parent::configure(); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Return possible values for the named option |
||
132 | * |
||
133 | * @param string $optionName Option name. |
||
134 | * @param CompletionContext $context Completion context. |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function completeOptionValues($optionName, CompletionContext $context) |
||
139 | { |
||
140 | $ret = parent::completeOptionValues($optionName, $context); |
||
141 | |||
142 | if ( $optionName === 'revisions' ) { |
||
143 | return array('all'); |
||
144 | } |
||
145 | |||
146 | if ( $optionName == 'source-url' ) { |
||
147 | return $this->getAllRefs(); |
||
148 | } |
||
149 | |||
150 | return $ret; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | * |
||
156 | * @throws \RuntimeException When both "--bugs" and "--revisions" options were specified. |
||
157 | * @throws CommandException When everything is merged. |
||
158 | * @throws CommandException When manually specified revisions are already merged. |
||
159 | */ |
||
160 | protected function execute(InputInterface $input, OutputInterface $output) |
||
161 | { |
||
162 | $bugs = $this->getList($this->io->getOption('bugs')); |
||
163 | $revisions = $this->getList($this->io->getOption('revisions')); |
||
164 | |||
165 | if ( $bugs && $revisions ) { |
||
166 | throw new \RuntimeException('The "--bugs" and "--revisions" options are mutually exclusive.'); |
||
167 | } |
||
168 | |||
169 | $wc_path = $this->getWorkingCopyPath(); |
||
170 | |||
171 | $this->ensureLatestWorkingCopy($wc_path); |
||
172 | |||
173 | $source_url = $this->getSourceUrl($wc_path); |
||
174 | $this->printSourceAndTarget($source_url, $wc_path); |
||
175 | $this->_unmergedRevisions = $this->getUnmergedRevisions($source_url, $wc_path); |
||
176 | |||
177 | if ( ($bugs || $revisions) && !$this->_unmergedRevisions ) { |
||
178 | throw new CommandException('Nothing to merge.'); |
||
179 | } |
||
180 | |||
181 | $this->ensureWorkingCopyWithoutConflicts($source_url, $wc_path); |
||
182 | |||
183 | if ( $this->shouldMergeAll($revisions) ) { |
||
184 | $revisions = $this->_unmergedRevisions; |
||
185 | } |
||
186 | else { |
||
187 | if ( $revisions ) { |
||
188 | $revisions = $this->getDirectRevisions($revisions, $source_url); |
||
189 | } |
||
190 | elseif ( $bugs ) { |
||
191 | $revisions = $this->getRevisionLog($source_url)->find('bugs', $bugs); |
||
192 | } |
||
193 | |||
194 | if ( $revisions ) { |
||
195 | $revisions = array_intersect($revisions, $this->_unmergedRevisions); |
||
196 | |||
197 | if ( !$revisions ) { |
||
198 | throw new CommandException('Requested revisions are already merged'); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | if ( $revisions ) { |
||
204 | $this->performMerge($source_url, $wc_path, $revisions); |
||
205 | } |
||
206 | elseif ( $this->_unmergedRevisions ) { |
||
207 | $this->runOtherCommand('log', array( |
||
208 | 'path' => $this->repositoryConnector->getProjectUrl($source_url), |
||
209 | '--revisions' => implode(',', $this->_unmergedRevisions), |
||
210 | '--with-details' => $this->io->getOption('with-details'), |
||
211 | '--with-summary' => $this->io->getOption('with-summary'), |
||
212 | '--with-merge-oracle' => true, |
||
213 | )); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Determines if all unmerged revisions should be merged. |
||
219 | * |
||
220 | * @param array $revisions Revisions. |
||
221 | * |
||
222 | * @return boolean |
||
223 | */ |
||
224 | protected function shouldMergeAll(array $revisions) |
||
225 | { |
||
226 | return $revisions === array(self::REVISION_ALL); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Ensures, that working copy is up to date. |
||
231 | * |
||
232 | * @param string $wc_path Working copy path. |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | protected function ensureLatestWorkingCopy($wc_path) |
||
261 | |||
262 | /** |
||
263 | * Updates working copy. |
||
264 | * |
||
265 | * @param string $wc_path Working copy path. |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | protected function updateWorkingCopy($wc_path) |
||
273 | |||
274 | /** |
||
275 | * Returns source url for merge. |
||
276 | * |
||
277 | * @param string $wc_path Working copy path. |
||
278 | * |
||
279 | * @return string |
||
280 | * @throws CommandException When source path is invalid. |
||
281 | */ |
||
282 | protected function getSourceUrl($wc_path) |
||
311 | |||
312 | /** |
||
313 | * Prints information about merge source & target. |
||
314 | * |
||
315 | * @param string $source_url Merge source: url. |
||
316 | * @param string $wc_path Merge target: working copy path. |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | protected function printSourceAndTarget($source_url, $wc_path) |
||
328 | |||
329 | /** |
||
330 | * Ensures, that there are some unmerged revisions. |
||
331 | * |
||
332 | * @param string $source_url Merge source: url. |
||
333 | * @param string $wc_path Merge target: working copy path. |
||
334 | * |
||
335 | * @return array |
||
336 | */ |
||
337 | protected function getUnmergedRevisions($source_url, $wc_path) |
||
356 | |||
357 | /** |
||
358 | * Returns not merged revisions. |
||
359 | * |
||
360 | * @param string $source_url Merge source: url. |
||
361 | * @param string $wc_path Merge target: working copy path. |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | protected function calculateUnmergedRevisions($source_url, $wc_path) |
||
389 | |||
390 | /** |
||
391 | * Parses information from "svn:mergeinfo" property. |
||
392 | * |
||
393 | * @param string $source_path Merge source: path in repository. |
||
394 | * @param string $wc_path Merge target: working copy path. |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | protected function getMergedRevisions($source_path, $wc_path) |
||
413 | |||
414 | /** |
||
415 | * Validates revisions to actually exist. |
||
416 | * |
||
417 | * @param array $revisions Revisions. |
||
418 | * @param string $repository_url Repository url. |
||
419 | * |
||
420 | * @return array |
||
421 | * @throws CommandException When revision doesn't exist. |
||
422 | */ |
||
423 | protected function getDirectRevisions(array $revisions, $repository_url) |
||
437 | |||
438 | /** |
||
439 | * Performs merge. |
||
440 | * |
||
441 | * @param string $source_url Merge source: url. |
||
442 | * @param string $wc_path Merge target: working copy path. |
||
443 | * @param array $revisions Revisions to merge. |
||
444 | * |
||
445 | * @return void |
||
446 | */ |
||
447 | protected function performMerge($source_url, $wc_path, array $revisions) |
||
467 | |||
468 | /** |
||
469 | * Ensures, that there are no unresolved conflicts in working copy. |
||
470 | * |
||
471 | * @param string $source_url Source url. |
||
472 | * @param string $wc_path Working copy path. |
||
473 | * @param integer $largest_suggested_revision Largest revision, that is suggested in error message. |
||
474 | * |
||
475 | * @return void |
||
476 | * @throws CommandException When merge conflicts detected. |
||
477 | */ |
||
478 | protected function ensureWorkingCopyWithoutConflicts($source_url, $wc_path, $largest_suggested_revision = null) |
||
532 | |||
533 | /** |
||
534 | * Adds new conflicts to already remembered ones. |
||
535 | * |
||
536 | * @param array $conflicts Conflicts. |
||
537 | * |
||
538 | * @return void |
||
539 | */ |
||
540 | protected function rememberConflicts(array $conflicts) |
||
547 | |||
548 | /** |
||
549 | * Returns revisions not larger, then given one. |
||
550 | * |
||
551 | * @param array $revisions Revisions. |
||
552 | * @param integer $max_revision Maximal revision. |
||
553 | * |
||
554 | * @return array |
||
555 | */ |
||
556 | protected function limitRevisions(array $revisions, $max_revision) |
||
568 | |||
569 | /** |
||
570 | * Returns list of config settings. |
||
571 | * |
||
572 | * @return AbstractConfigSetting[] |
||
573 | */ |
||
574 | public function getConfigSettings() |
||
581 | |||
582 | } |
||
583 |
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.