Complex classes like Connector 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 Connector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Connector |
||
| 25 | { |
||
| 26 | |||
| 27 | const STATUS_NORMAL = 'normal'; |
||
| 28 | |||
| 29 | const STATUS_ADDED = 'added'; |
||
| 30 | |||
| 31 | const STATUS_CONFLICTED = 'conflicted'; |
||
| 32 | |||
| 33 | const STATUS_UNVERSIONED = 'unversioned'; |
||
| 34 | |||
| 35 | const STATUS_EXTERNAL = 'external'; |
||
| 36 | |||
| 37 | const STATUS_MISSING = 'missing'; |
||
| 38 | |||
| 39 | const STATUS_NONE = 'none'; |
||
| 40 | |||
| 41 | const URL_REGEXP = '#([\w]*)://([^/@\s\']+@)?([^/@:\s\']+)(:\d+)?([^@\s\']*)?#'; |
||
| 42 | |||
| 43 | const SVN_INFO_CACHE_DURATION = '1 year'; |
||
| 44 | |||
| 45 | const SVN_CAT_CACHE_DURATION = '1 month'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Command factory. |
||
| 49 | * |
||
| 50 | * @var CommandFactory |
||
| 51 | */ |
||
| 52 | private $_commandFactory; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Console IO. |
||
| 56 | * |
||
| 57 | * @var ConsoleIO |
||
| 58 | */ |
||
| 59 | private $_io; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Revision list parser. |
||
| 63 | * |
||
| 64 | * @var RevisionListParser |
||
| 65 | */ |
||
| 66 | private $_revisionListParser; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Cache duration for next invoked command. |
||
| 70 | * |
||
| 71 | * @var mixed |
||
| 72 | */ |
||
| 73 | private $_nextCommandCacheDuration = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Cache overwrite for next invoked command. |
||
| 77 | * |
||
| 78 | * @var mixed |
||
| 79 | */ |
||
| 80 | private $_nextCommandCacheOverwrite = null; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Whatever to cache last repository revision or not. |
||
| 84 | * |
||
| 85 | * @var mixed |
||
| 86 | */ |
||
| 87 | private $_lastRevisionCacheDuration = null; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Creates repository connector. |
||
| 91 | * |
||
| 92 | * @param ConfigEditor $config_editor ConfigEditor. |
||
| 93 | * @param CommandFactory $command_factory Command factory. |
||
| 94 | * @param ConsoleIO $io Console IO. |
||
| 95 | * @param RevisionListParser $revision_list_parser Revision list parser. |
||
| 96 | */ |
||
| 97 | 97 | public function __construct( |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Builds a command. |
||
| 118 | * |
||
| 119 | * @param string $sub_command Sub command. |
||
| 120 | * @param string|null $param_string Parameter string. |
||
| 121 | * |
||
| 122 | * @return Command |
||
| 123 | */ |
||
| 124 | 51 | public function getCommand($sub_command, $param_string = null) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Sets cache configuration for next created command. |
||
| 143 | * |
||
| 144 | * @param mixed $cache_duration Cache duration. |
||
| 145 | * @param boolean|null $cache_overwrite Cache overwrite. |
||
| 146 | * |
||
| 147 | * @return self |
||
| 148 | */ |
||
| 149 | 31 | public function withCache($cache_duration, $cache_overwrite = null) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Returns property value. |
||
| 159 | * |
||
| 160 | * @param string $name Property name. |
||
| 161 | * @param string $path_or_url Path to get property from. |
||
| 162 | * @param mixed $revision Revision. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | * @throws RepositoryCommandException When other, then missing property exception happens. |
||
| 166 | */ |
||
| 167 | 8 | public function getProperty($name, $path_or_url, $revision = null) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Returns relative path of given path/url to the root of the repository. |
||
| 193 | * |
||
| 194 | * @param string $path_or_url Path or url. |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | 3 | public function getRelativePath($path_or_url) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Returns repository root url from given path/url. |
||
| 212 | * |
||
| 213 | * @param string $path_or_url Path or url. |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 3 | public function getRootUrl($path_or_url) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Determines if path is a root of the ref. |
||
| 224 | * |
||
| 225 | * @param string $path Path to a file. |
||
| 226 | * |
||
| 227 | * @return boolean |
||
| 228 | */ |
||
| 229 | 13 | public function isRefRoot($path) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Detects ref from given path. |
||
| 242 | * |
||
| 243 | * @param string $path Path to a file. |
||
| 244 | * |
||
| 245 | * @return string|boolean |
||
| 246 | * @see getProjectUrl |
||
| 247 | */ |
||
| 248 | 22 | public function getRefByPath($path) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Returns URL of the working copy. |
||
| 259 | * |
||
| 260 | * @param string $wc_path Working copy path. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 264 | */ |
||
| 265 | 11 | public function getWorkingCopyUrl($wc_path) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Returns last changed revision on path/url. |
||
| 296 | * |
||
| 297 | * @param string $path_or_url Path or url. |
||
| 298 | * |
||
| 299 | * @return integer |
||
| 300 | */ |
||
| 301 | 9 | public function getLastRevision($path_or_url) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Determines if given path is in fact an url. |
||
| 311 | * |
||
| 312 | * @param string $path Path. |
||
| 313 | * |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | 29 | public function isUrl($path) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Removes credentials from url. |
||
| 323 | * |
||
| 324 | * @param string $url URL. |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | * @throws \InvalidArgumentException When non-url given. |
||
| 328 | */ |
||
| 329 | 17 | public function removeCredentials($url) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
| 340 | * |
||
| 341 | * @param string $repository_url Repository url. |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | * @see getRefByPath |
||
| 345 | */ |
||
| 346 | 9 | public function getProjectUrl($repository_url) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Returns "svn info" entry for path or url. |
||
| 358 | * |
||
| 359 | * @param string $path_or_url Path or url. |
||
| 360 | * @param mixed $cache_duration Cache duration. |
||
| 361 | * |
||
| 362 | * @return \SimpleXMLElement |
||
| 363 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
| 364 | */ |
||
| 365 | 24 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Returns revision, when path was added to repository. |
||
| 407 | * |
||
| 408 | * @param string $url Url. |
||
| 409 | * |
||
| 410 | * @return integer |
||
| 411 | * @throws \InvalidArgumentException When not an url was given. |
||
| 412 | */ |
||
| 413 | public function getFirstRevision($url) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Returns conflicts in working copy. |
||
| 426 | * |
||
| 427 | * @param string $wc_path Working copy path. |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Returns missing paths in working copy. |
||
| 446 | * |
||
| 447 | * @param string $wc_path Working copy path. |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | 2 | public function getWorkingCopyMissing($wc_path) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Returns compact working copy status. |
||
| 466 | * |
||
| 467 | * @param string $wc_path Working copy path. |
||
| 468 | * @param string|null $changelist Changelist. |
||
| 469 | * @param array $except_statuses Except statuses. |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public function getCompactWorkingCopyStatus( |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Returns short item status. |
||
| 495 | * |
||
| 496 | * @param string $status Status. |
||
| 497 | * |
||
| 498 | * @return string |
||
| 499 | * @throws \InvalidArgumentException When unknown status given. |
||
| 500 | */ |
||
| 501 | protected function getShortItemStatus($status) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns short item status. |
||
| 529 | * |
||
| 530 | * @param string $status Status. |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | * @throws \InvalidArgumentException When unknown status given. |
||
| 534 | */ |
||
| 535 | protected function getShortPropertiesStatus($status) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Returns working copy status. |
||
| 553 | * |
||
| 554 | * @param string $wc_path Working copy path. |
||
| 555 | * @param string|null $changelist Changelist. |
||
| 556 | * @param array $except_statuses Except statuses. |
||
| 557 | * |
||
| 558 | * @return array |
||
| 559 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
| 560 | */ |
||
| 561 | 10 | public function getWorkingCopyStatus( |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Processes "entry" nodes from "svn status" command. |
||
| 630 | * |
||
| 631 | * @param string $wc_path Working copy path. |
||
| 632 | * @param \SimpleXMLElement $entries Entries. |
||
| 633 | * |
||
| 634 | * @return array |
||
| 635 | */ |
||
| 636 | 8 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
| 654 | |||
| 655 | /** |
||
| 656 | * Detects specific path status. |
||
| 657 | * |
||
| 658 | * @param array $status Path status. |
||
| 659 | * @param string $path_status Expected path status. |
||
| 660 | * |
||
| 661 | * @return boolean |
||
| 662 | */ |
||
| 663 | 8 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
| 683 | |||
| 684 | /** |
||
| 685 | * Returns parent paths from given paths. |
||
| 686 | * |
||
| 687 | * @param array $paths Paths. |
||
| 688 | * |
||
| 689 | * @return array |
||
| 690 | */ |
||
| 691 | 1 | protected function getParentPaths(array $paths) |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Returns working copy changelists. |
||
| 707 | * |
||
| 708 | * @param string $wc_path Working copy path. |
||
| 709 | * |
||
| 710 | * @return array |
||
| 711 | */ |
||
| 712 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
| 725 | |||
| 726 | /** |
||
| 727 | * Returns revisions of paths in a working copy. |
||
| 728 | * |
||
| 729 | * @param string $wc_path Working copy path. |
||
| 730 | * |
||
| 731 | * @return array |
||
| 732 | */ |
||
| 733 | public function getWorkingCopyRevisions($wc_path) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Determines if there is a working copy on a given path. |
||
| 758 | * |
||
| 759 | * @param string $path Path. |
||
| 760 | * |
||
| 761 | * @return boolean |
||
| 762 | * @throws \InvalidArgumentException When path isn't found. |
||
| 763 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 764 | */ |
||
| 765 | public function isWorkingCopy($path) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Returns list of add/removed revisions from last merge operation. |
||
| 787 | * |
||
| 788 | * @param string $wc_path Working copy path, where merge happens. |
||
| 789 | * @param boolean $regular_or_reverse Merge direction ("regular" or "reverse"). |
||
| 790 | * |
||
| 791 | * @return array |
||
| 792 | */ |
||
| 793 | 4 | public function getMergedRevisionChanges($wc_path, $regular_or_reverse) |
|
| 833 | |||
| 834 | /** |
||
| 835 | * Returns list of merged revisions per path. |
||
| 836 | * |
||
| 837 | * @param string $wc_path Merge target: working copy path. |
||
| 838 | * @param integer $revision Revision. |
||
| 839 | * |
||
| 840 | * @return array |
||
| 841 | */ |
||
| 842 | 4 | protected function getMergedRevisions($wc_path, $revision = null) |
|
| 856 | |||
| 857 | /** |
||
| 858 | * Returns file contents at given revision. |
||
| 859 | * |
||
| 860 | * @param string $path_or_url Path or url. |
||
| 861 | * @param integer $revision Revision. |
||
| 862 | * |
||
| 863 | * @return string |
||
| 864 | */ |
||
| 865 | 1 | public function getFileContent($path_or_url, $revision) |
|
| 872 | |||
| 873 | } |
||
| 874 |