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 |
||
31 | class MergeCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand |
||
32 | { |
||
33 | |||
34 | const SETTING_MERGE_SOURCE_URL = 'merge.source-url'; |
||
35 | |||
36 | const SETTING_MERGE_RECENT_CONFLICTS = 'merge.recent-conflicts'; |
||
37 | |||
38 | const SETTING_MERGE_AUTO_COMMIT = 'merge.auto-commit'; |
||
39 | |||
40 | const REVISION_ALL = 'all'; |
||
41 | |||
42 | /** |
||
43 | * Merge source detector. |
||
44 | * |
||
45 | * @var AbstractMergeSourceDetector |
||
46 | */ |
||
47 | private $_mergeSourceDetector; |
||
48 | |||
49 | /** |
||
50 | * Revision list parser. |
||
51 | * |
||
52 | * @var RevisionListParser |
||
53 | */ |
||
54 | private $_revisionListParser; |
||
55 | |||
56 | /** |
||
57 | * Unmerged revisions. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $_unmergedRevisions = array(); |
||
62 | |||
63 | /** |
||
64 | * Url resolver. |
||
65 | * |
||
66 | * @var UrlResolver |
||
67 | */ |
||
68 | private $_urlResolver; |
||
69 | |||
70 | /** |
||
71 | * Working copy conflict tracker. |
||
72 | * |
||
73 | * @var WorkingCopyConflictTracker |
||
74 | */ |
||
75 | private $_workingCopyConflictTracker; |
||
76 | |||
77 | /** |
||
78 | * Prepare dependencies. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | protected function prepareDependencies() |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | protected function configure() |
||
147 | |||
148 | /** |
||
149 | * Return possible values for the named option |
||
150 | * |
||
151 | * @param string $optionName Option name. |
||
152 | * @param CompletionContext $context Completion context. |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | public function completeOptionValues($optionName, CompletionContext $context) |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | * |
||
178 | * @throws \RuntimeException When both "--bugs" and "--revisions" options were specified. |
||
179 | * @throws CommandException When everything is merged. |
||
180 | * @throws CommandException When manually specified revisions are already merged. |
||
181 | */ |
||
182 | protected function execute(InputInterface $input, OutputInterface $output) |
||
238 | |||
239 | /** |
||
240 | * Determines if all unmerged revisions should be merged. |
||
241 | * |
||
242 | * @param array $revisions Revisions. |
||
243 | * |
||
244 | * @return boolean |
||
245 | */ |
||
246 | protected function shouldMergeAll(array $revisions) |
||
250 | |||
251 | /** |
||
252 | * Ensures, that working copy is up to date. |
||
253 | * |
||
254 | * @param string $wc_path Working copy path. |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | protected function ensureLatestWorkingCopy($wc_path) |
||
283 | |||
284 | /** |
||
285 | * Updates working copy. |
||
286 | * |
||
287 | * @param string $wc_path Working copy path. |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | protected function updateWorkingCopy($wc_path) |
||
295 | |||
296 | /** |
||
297 | * Returns source url for merge. |
||
298 | * |
||
299 | * @param string $wc_path Working copy path. |
||
300 | * |
||
301 | * @return string |
||
302 | * @throws CommandException When source path is invalid. |
||
303 | */ |
||
304 | protected function getSourceUrl($wc_path) |
||
334 | |||
335 | /** |
||
336 | * Prints information about merge source & target. |
||
337 | * |
||
338 | * @param string $source_url Merge source: url. |
||
339 | * @param string $wc_path Merge target: working copy path. |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | protected function printSourceAndTarget($source_url, $wc_path) |
||
351 | |||
352 | /** |
||
353 | * Ensures, that there are some unmerged revisions. |
||
354 | * |
||
355 | * @param string $source_url Merge source: url. |
||
356 | * @param string $wc_path Merge target: working copy path. |
||
357 | * |
||
358 | * @return array |
||
359 | */ |
||
360 | protected function getUnmergedRevisions($source_url, $wc_path) |
||
379 | |||
380 | /** |
||
381 | * Returns not merged revisions. |
||
382 | * |
||
383 | * @param string $source_url Merge source: url. |
||
384 | * @param string $wc_path Merge target: working copy path. |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | protected function calculateUnmergedRevisions($source_url, $wc_path) |
||
412 | |||
413 | /** |
||
414 | * Parses information from "svn:mergeinfo" property. |
||
415 | * |
||
416 | * @param string $source_path Merge source: path in repository. |
||
417 | * @param string $wc_path Merge target: working copy path. |
||
418 | * |
||
419 | * @return array |
||
420 | */ |
||
421 | protected function getMergedRevisions($source_path, $wc_path) |
||
436 | |||
437 | /** |
||
438 | * Validates revisions to actually exist. |
||
439 | * |
||
440 | * @param array $revisions Revisions. |
||
441 | * @param string $repository_url Repository url. |
||
442 | * |
||
443 | * @return array |
||
444 | * @throws CommandException When revision doesn't exist. |
||
445 | */ |
||
446 | protected function getDirectRevisions(array $revisions, $repository_url) |
||
460 | |||
461 | /** |
||
462 | * Performs merge. |
||
463 | * |
||
464 | * @param string $source_url Merge source: url. |
||
465 | * @param string $wc_path Merge target: working copy path. |
||
466 | * @param array $revisions Revisions to merge. |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | protected function performMerge($source_url, $wc_path, array $revisions) |
||
492 | |||
493 | /** |
||
494 | * Ensures, that there are no unresolved conflicts in working copy. |
||
495 | * |
||
496 | * @param string $source_url Source url. |
||
497 | * @param string $wc_path Working copy path. |
||
498 | * @param integer $largest_suggested_revision Largest revision, that is suggested in error message. |
||
499 | * |
||
500 | * @return void |
||
501 | * @throws CommandException When merge conflicts detected. |
||
502 | */ |
||
503 | protected function ensureWorkingCopyWithoutConflicts($source_url, $wc_path, $largest_suggested_revision = null) |
||
557 | |||
558 | /** |
||
559 | * Returns revisions not larger, then given one. |
||
560 | * |
||
561 | * @param array $revisions Revisions. |
||
562 | * @param integer $max_revision Maximal revision. |
||
563 | * |
||
564 | * @return array |
||
565 | */ |
||
566 | protected function limitRevisions(array $revisions, $max_revision) |
||
578 | |||
579 | /** |
||
580 | * Performs commit unless user doesn't want it. |
||
581 | * |
||
582 | * @return void |
||
583 | */ |
||
584 | protected function performCommit() |
||
600 | |||
601 | /** |
||
602 | * Returns list of config settings. |
||
603 | * |
||
604 | * @return AbstractConfigSetting[] |
||
605 | */ |
||
606 | public function getConfigSettings() |
||
618 | |||
619 | /** |
||
620 | * Returns option names, that makes sense to use in aggregation mode. |
||
621 | * |
||
622 | * @return array |
||
623 | */ |
||
624 | public function getAggregatedOptions() |
||
628 | |||
629 | } |
||
630 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.