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 | 94 | public function __construct( |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Prepares static part of svn command to be used across the script. |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | 94 | 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 | 50 | 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 | 50 | 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 | 25 | 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 | 10 | 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 | 9 | 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 | 28 | 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 | 17 | 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 | 23 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Returns revision, when path was added to repository. |
||
| 473 | * |
||
| 474 | * @param string $url Url. |
||
| 475 | * |
||
| 476 | * @return integer |
||
| 477 | * @throws \InvalidArgumentException When not an url was given. |
||
| 478 | */ |
||
| 479 | public function getFirstRevision($url) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Returns conflicts in working copy. |
||
| 492 | * |
||
| 493 | * @param string $wc_path Working copy path. |
||
| 494 | * |
||
| 495 | * @return array |
||
| 496 | */ |
||
| 497 | 2 | public function getWorkingCopyConflicts($wc_path) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Returns missing paths in working copy. |
||
| 512 | * |
||
| 513 | * @param string $wc_path Working copy path. |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | 2 | public function getWorkingCopyMissing($wc_path) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Returns compact working copy status. |
||
| 532 | * |
||
| 533 | * @param string $wc_path Working copy path. |
||
| 534 | * @param string|null $changelist Changelist. |
||
| 535 | * @param boolean $with_unversioned With unversioned. |
||
| 536 | * |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | public function getCompactWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Returns short item status. |
||
| 555 | * |
||
| 556 | * @param string $status Status. |
||
| 557 | * |
||
| 558 | * @return string |
||
| 559 | * @throws \InvalidArgumentException When unknown status given. |
||
| 560 | */ |
||
| 561 | protected function getShortItemStatus($status) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Returns short item status. |
||
| 589 | * |
||
| 590 | * @param string $status Status. |
||
| 591 | * |
||
| 592 | * @return string |
||
| 593 | * @throws \InvalidArgumentException When unknown status given. |
||
| 594 | */ |
||
| 595 | protected function getShortPropertiesStatus($status) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Returns working copy status. |
||
| 613 | * |
||
| 614 | * @param string $wc_path Working copy path. |
||
| 615 | * @param string|null $changelist Changelist. |
||
| 616 | * @param boolean $with_unversioned With unversioned. |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | * @throws \InvalidArgumentException When changelist doens't exist. |
||
| 620 | */ |
||
| 621 | 7 | public function getWorkingCopyStatus($wc_path, $changelist = null, $with_unversioned = false) |
|
| 679 | |||
| 680 | /** |
||
| 681 | * Processes "entry" nodes from "svn status" command. |
||
| 682 | * |
||
| 683 | * @param string $wc_path Working copy path. |
||
| 684 | * @param \SimpleXMLElement $entries Entries. |
||
| 685 | * |
||
| 686 | * @return array |
||
| 687 | */ |
||
| 688 | 5 | protected function processStatusEntryNodes($wc_path, \SimpleXMLElement $entries) |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Detects specific path status. |
||
| 708 | * |
||
| 709 | * @param array $status Path status. |
||
| 710 | * @param string $path_status Expected path status. |
||
| 711 | * |
||
| 712 | * @return boolean |
||
| 713 | */ |
||
| 714 | 5 | protected function isWorkingCopyPathStatus(array $status, $path_status) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Returns parent paths from given paths. |
||
| 735 | * |
||
| 736 | * @param array $paths Paths. |
||
| 737 | * |
||
| 738 | * @return array |
||
| 739 | */ |
||
| 740 | 1 | protected function getParentPaths(array $paths) |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Returns working copy changelists. |
||
| 756 | * |
||
| 757 | * @param string $wc_path Working copy path. |
||
| 758 | * |
||
| 759 | * @return array |
||
| 760 | */ |
||
| 761 | 2 | public function getWorkingCopyChangelists($wc_path) |
|
| 774 | |||
| 775 | /** |
||
| 776 | * Returns revisions of paths in a working copy. |
||
| 777 | * |
||
| 778 | * @param string $wc_path Working copy path. |
||
| 779 | * |
||
| 780 | * @return array |
||
| 781 | */ |
||
| 782 | public function getWorkingCopyRevisions($wc_path) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Determines if there is a working copy on a given path. |
||
| 807 | * |
||
| 808 | * @param string $path Path. |
||
| 809 | * |
||
| 810 | * @return boolean |
||
| 811 | * @throws \InvalidArgumentException When path isn't found. |
||
| 812 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 813 | */ |
||
| 814 | public function isWorkingCopy($path) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Returns list of just merged revisions. |
||
| 836 | * |
||
| 837 | * @param string $wc_path Working copy path, where merge happens. |
||
| 838 | * |
||
| 839 | * @return array |
||
| 840 | */ |
||
| 841 | 2 | public function getFreshMergedRevisions($wc_path) |
|
| 874 | |||
| 875 | /** |
||
| 876 | * Returns list of merged revisions per path. |
||
| 877 | * |
||
| 878 | * @param string $wc_path Merge target: working copy path. |
||
| 879 | * @param integer $revision Revision. |
||
| 880 | * |
||
| 881 | * @return array |
||
| 882 | */ |
||
| 883 | 2 | protected function getMergedRevisions($wc_path, $revision = null) |
|
| 897 | |||
| 898 | /** |
||
| 899 | * Returns file contents at given revision. |
||
| 900 | * |
||
| 901 | * @param string $path_or_url Path or url. |
||
| 902 | * @param integer $revision Revision. |
||
| 903 | * |
||
| 904 | * @return string |
||
| 905 | */ |
||
| 906 | 1 | public function getFileContent($path_or_url, $revision) |
|
| 913 | |||
| 914 | } |
||
| 915 |
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.