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 | * Reference to configuration. |
||
| 49 | * |
||
| 50 | * @var ConfigEditor |
||
| 51 | */ |
||
| 52 | private $_configEditor; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Process factory. |
||
| 56 | * |
||
| 57 | * @var IProcessFactory |
||
| 58 | */ |
||
| 59 | private $_processFactory; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Console IO. |
||
| 63 | * |
||
| 64 | * @var ConsoleIO |
||
| 65 | */ |
||
| 66 | private $_io; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Cache manager. |
||
| 70 | * |
||
| 71 | * @var CacheManager |
||
| 72 | */ |
||
| 73 | private $_cacheManager; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Revision list parser. |
||
| 77 | * |
||
| 78 | * @var RevisionListParser |
||
| 79 | */ |
||
| 80 | private $_revisionListParser; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Path to an svn command. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $_svnCommand = 'svn'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Cache duration for next invoked command. |
||
| 91 | * |
||
| 92 | * @var mixed |
||
| 93 | */ |
||
| 94 | private $_nextCommandCacheDuration = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Whatever to cache last repository revision or not. |
||
| 98 | * |
||
| 99 | * @var mixed |
||
| 100 | */ |
||
| 101 | private $_lastRevisionCacheDuration = null; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Creates repository connector. |
||
| 105 | * |
||
| 106 | * @param ConfigEditor $config_editor ConfigEditor. |
||
| 107 | * @param IProcessFactory $process_factory Process factory. |
||
| 108 | * @param ConsoleIO $io Console IO. |
||
| 109 | * @param CacheManager $cache_manager Cache manager. |
||
| 110 | * @param RevisionListParser $revision_list_parser Revision list parser. |
||
| 111 | */ |
||
| 112 | 90 | public function __construct( |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Prepares static part of svn command to be used across the script. |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | 90 | protected function prepareSvnCommand() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Builds a command. |
||
| 159 | * |
||
| 160 | * @param string $sub_command Sub command. |
||
| 161 | * @param string|null $param_string Parameter string. |
||
| 162 | * |
||
| 163 | * @return Command |
||
| 164 | */ |
||
| 165 | 46 | public function getCommand($sub_command, $param_string = null) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Builds command from given arguments. |
||
| 186 | * |
||
| 187 | * @param string $sub_command Command. |
||
| 188 | * @param string $param_string Parameter string. |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | * @throws \InvalidArgumentException When command contains spaces. |
||
| 192 | */ |
||
| 193 | 46 | protected function buildCommand($sub_command, $param_string = null) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Sets cache configuration for next created command. |
||
| 222 | * |
||
| 223 | * @param mixed $cache_duration Cache duration. |
||
| 224 | * |
||
| 225 | * @return self |
||
| 226 | */ |
||
| 227 | 21 | public function withCache($cache_duration) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Returns property value. |
||
| 236 | * |
||
| 237 | * @param string $name Property name. |
||
| 238 | * @param string $path_or_url Path to get property from. |
||
| 239 | * @param mixed $revision Revision. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | * @throws RepositoryCommandException When other, then missing property exception happens. |
||
| 243 | */ |
||
| 244 | 6 | public function getProperty($name, $path_or_url, $revision = null) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Returns relative path of given path/url to the root of the repository. |
||
| 270 | * |
||
| 271 | * @param string $path_or_url Path or url. |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 3 | public function getRelativePath($path_or_url) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Returns repository root url from given path/url. |
||
| 289 | * |
||
| 290 | * @param string $path_or_url Path or url. |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 3 | public function getRootUrl($path_or_url) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Determines if path is a root of the ref. |
||
| 301 | * |
||
| 302 | * @param string $path Path to a file. |
||
| 303 | * |
||
| 304 | * @return boolean |
||
| 305 | */ |
||
| 306 | 13 | public function isRefRoot($path) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Detects ref from given path. |
||
| 319 | * |
||
| 320 | * @param string $path Path to a file. |
||
| 321 | * |
||
| 322 | * @return string|boolean |
||
| 323 | * @see getProjectUrl |
||
| 324 | */ |
||
| 325 | 22 | public function getRefByPath($path) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Returns URL of the working copy. |
||
| 336 | * |
||
| 337 | * @param string $wc_path Working copy path. |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 341 | */ |
||
| 342 | 8 | public function getWorkingCopyUrl($wc_path) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Returns last changed revision on path/url. |
||
| 372 | * |
||
| 373 | * @param string $path_or_url Path or url. |
||
| 374 | * |
||
| 375 | * @return integer |
||
| 376 | */ |
||
| 377 | 7 | public function getLastRevision($path_or_url) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Determines if given path is in fact an url. |
||
| 387 | * |
||
| 388 | * @param string $path Path. |
||
| 389 | * |
||
| 390 | * @return boolean |
||
| 391 | */ |
||
| 392 | 24 | public function isUrl($path) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Removes credentials from url. |
||
| 399 | * |
||
| 400 | * @param string $url URL. |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | * @throws \InvalidArgumentException When non-url given. |
||
| 404 | */ |
||
| 405 | 15 | public function removeCredentials($url) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
| 416 | * |
||
| 417 | * @param string $repository_url Repository url. |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | * @see getRefByPath |
||
| 421 | */ |
||
| 422 | 9 | public function getProjectUrl($repository_url) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Returns "svn info" entry for path or url. |
||
| 434 | * |
||
| 435 | * @param string $path_or_url Path or url. |
||
| 436 | * @param mixed $cache_duration Cache duration. |
||
| 437 | * |
||
| 438 | * @return \SimpleXMLElement |
||
| 439 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
| 440 | */ |
||
| 441 | 19 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Returns path of "svn info" entry. |
||
| 473 | * |
||
| 474 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | 17 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Returns revision, when path was added to repository. |
||
| 493 | * |
||
| 494 | * @param string $url Url. |
||
| 495 | * |
||
| 496 | * @return integer |
||
| 497 | * @throws \InvalidArgumentException When not an url was given. |
||
| 498 | */ |
||
| 499 | public function getFirstRevision($url) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Returns conflicts in working copy. |
||
| 512 | * |
||
| 513 | * @param string $wc_path Working copy path. |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Returns missing paths in working copy. |
||
| 532 | * |
||
| 533 | * @param string $wc_path Working copy path. |
||
| 534 | * |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | 2 | public function getWorkingCopyMissing($wc_path) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Returns compact working copy status. |
||
| 552 | * |
||
| 553 | * @param string $wc_path Working copy path. |
||
| 554 | * @param string|null $changelist Changelist. |
||
| 555 | * @param boolean $with_unversioned With unversioned. |
||
| 556 | * |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | public function getCompactWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns short item status. |
||
| 575 | * |
||
| 576 | * @param string $status Status. |
||
| 577 | * |
||
| 578 | * @return string |
||
| 579 | * @throws \InvalidArgumentException When unknown status given. |
||
| 580 | */ |
||
| 581 | protected function getShortItemStatus($status) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Returns short item status. |
||
| 609 | * |
||
| 610 | * @param string $status Status. |
||
| 611 | * |
||
| 612 | * @return string |
||
| 613 | * @throws \InvalidArgumentException When unknown status given. |
||
| 614 | */ |
||
| 615 | protected function getShortPropertiesStatus($status) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Returns working copy status. |
||
| 633 | * |
||
| 634 | * @param string $wc_path Working copy path. |
||
| 635 | * @param string|null $changelist Changelist. |
||
| 636 | * @param boolean $with_unversioned With unversioned. |
||
| 637 | * |
||
| 638 | * @return array |
||
| 639 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
| 640 | */ |
||
| 641 | 7 | public function getWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Processes "entry" nodes from "svn status" command. |
||
| 702 | * |
||
| 703 | * @param string $wc_path Working copy path. |
||
| 704 | * @param \SimpleXMLElement $entries Entries. |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | 5 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
| 725 | |||
| 726 | /** |
||
| 727 | * Detects specific path status. |
||
| 728 | * |
||
| 729 | * @param array $status Path status. |
||
| 730 | * @param string $path_status Expected path status. |
||
| 731 | * |
||
| 732 | * @return boolean |
||
| 733 | */ |
||
| 734 | 5 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
| 752 | |||
| 753 | /** |
||
| 754 | * Returns parent paths from given paths. |
||
| 755 | * |
||
| 756 | * @param array $paths Paths. |
||
| 757 | * |
||
| 758 | * @return array |
||
| 759 | */ |
||
| 760 | 1 | protected function getParentPaths(array $paths) |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Returns working copy changelists. |
||
| 776 | * |
||
| 777 | * @param string $wc_path Working copy path. |
||
| 778 | * |
||
| 779 | * @return array |
||
| 780 | */ |
||
| 781 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
| 794 | |||
| 795 | /** |
||
| 796 | * Returns revisions of paths in a working copy. |
||
| 797 | * |
||
| 798 | * @param string $wc_path Working copy path. |
||
| 799 | * |
||
| 800 | * @return array |
||
| 801 | */ |
||
| 802 | public function getWorkingCopyRevisions($wc_path) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Determines if there is a working copy on a given path. |
||
| 827 | * |
||
| 828 | * @param string $path Path. |
||
| 829 | * |
||
| 830 | * @return boolean |
||
| 831 | * @throws \InvalidArgumentException When path isn't found. |
||
| 832 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 833 | */ |
||
| 834 | public function isWorkingCopy($path) |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Returns list of just merged revisions. |
||
| 856 | * |
||
| 857 | * @param string $wc_path Working copy path, where merge happens. |
||
| 858 | * |
||
| 859 | * @return array |
||
| 860 | */ |
||
| 861 | 2 | public function getFreshMergedRevisions($wc_path) |
|
| 894 | |||
| 895 | /** |
||
| 896 | * Returns list of merged revisions per path. |
||
| 897 | * |
||
| 898 | * @param string $wc_path Merge target: working copy path. |
||
| 899 | * @param integer $revision Revision. |
||
| 900 | * |
||
| 901 | * @return array |
||
| 902 | */ |
||
| 903 | 2 | protected function getMergedRevisions($wc_path, $revision = null) |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Returns file contents at given revision. |
||
| 920 | * |
||
| 921 | * @param string $path_or_url Path or url. |
||
| 922 | * @param integer $revision Revision. |
||
| 923 | * |
||
| 924 | * @return string |
||
| 925 | */ |
||
| 926 | 1 | public function getFileContent($path_or_url, $revision) |
|
| 933 | |||
| 934 | } |
||
| 935 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.