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 |
||
| 23 | class Connector |
||
| 24 | { |
||
| 25 | |||
| 26 | const STATUS_UNVERSIONED = 'unversioned'; |
||
| 27 | |||
| 28 | const URL_REGEXP = '#([\w]*)://([^/@\s\']+@)?([^/@:\s\']+)(:\d+)?([^@\s\']*)?#'; |
||
| 29 | |||
| 30 | const SVN_INFO_CACHE_DURATION = '1 year'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Reference to configuration. |
||
| 34 | * |
||
| 35 | * @var ConfigEditor |
||
| 36 | */ |
||
| 37 | private $_configEditor; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Process factory. |
||
| 41 | * |
||
| 42 | * @var IProcessFactory |
||
| 43 | */ |
||
| 44 | private $_processFactory; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Console IO. |
||
| 48 | * |
||
| 49 | * @var ConsoleIO |
||
| 50 | */ |
||
| 51 | private $_io; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Cache manager. |
||
| 55 | * |
||
| 56 | * @var CacheManager |
||
| 57 | */ |
||
| 58 | private $_cacheManager; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Path to an svn command. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $_svnCommand = 'svn'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Cache duration for next invoked command. |
||
| 69 | * |
||
| 70 | * @var mixed |
||
| 71 | */ |
||
| 72 | private $_nextCommandCacheDuration = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Whatever to cache last repository revision or not. |
||
| 76 | * |
||
| 77 | * @var mixed |
||
| 78 | */ |
||
| 79 | private $_lastRevisionCacheDuration = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Creates repository connector. |
||
| 83 | * |
||
| 84 | * @param ConfigEditor $config_editor ConfigEditor. |
||
| 85 | * @param IProcessFactory $process_factory Process factory. |
||
| 86 | * @param ConsoleIO $io Console IO. |
||
| 87 | * @param CacheManager $cache_manager Cache manager. |
||
| 88 | */ |
||
| 89 | 71 | public function __construct( |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Prepares static part of svn command to be used across the script. |
||
| 113 | * |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | 71 | protected function prepareSvnCommand() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Builds a command. |
||
| 134 | * |
||
| 135 | * @param string $sub_command Sub command. |
||
| 136 | * @param string|null $param_string Parameter string. |
||
| 137 | * |
||
| 138 | * @return Command |
||
| 139 | */ |
||
| 140 | 32 | public function getCommand($sub_command, $param_string = null) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Builds command from given arguments. |
||
| 160 | * |
||
| 161 | * @param string $sub_command Command. |
||
| 162 | * @param string $param_string Parameter string. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | * @throws \InvalidArgumentException When command contains spaces. |
||
| 166 | */ |
||
| 167 | 32 | protected function buildCommand($sub_command, $param_string = null) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Sets cache configuration for next created command. |
||
| 196 | * |
||
| 197 | * @param mixed $cache_duration Cache duration. |
||
| 198 | * |
||
| 199 | * @return self |
||
| 200 | */ |
||
| 201 | 20 | public function withCache($cache_duration) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Returns property value. |
||
| 210 | * |
||
| 211 | * @param string $name Property name. |
||
| 212 | * @param string $path_or_url Path to get property from. |
||
| 213 | * @param mixed $revision Revision. |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 2 | public function getProperty($name, $path_or_url, $revision = null) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Returns relative path of given path/url to the root of the repository. |
||
| 230 | * |
||
| 231 | * @param string $path_or_url Path or url. |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | 3 | public function getRelativePath($path_or_url) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Returns repository root url from given path/url. |
||
| 249 | * |
||
| 250 | * @param string $path_or_url Path or url. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 3 | public function getRootUrl($path_or_url) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Determines if path is a root of the ref. |
||
| 261 | * |
||
| 262 | * @param string $path Path to a file. |
||
| 263 | * |
||
| 264 | * @return boolean |
||
| 265 | */ |
||
| 266 | 13 | public function isRefRoot($path) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Detects ref from given path. |
||
| 279 | * |
||
| 280 | * @param string $path Path to a file. |
||
| 281 | * |
||
| 282 | * @return string|boolean |
||
| 283 | * @see getProjectUrl |
||
| 284 | */ |
||
| 285 | 22 | public function getRefByPath($path) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Returns URL of the working copy. |
||
| 296 | * |
||
| 297 | * @param string $wc_path Working copy path. |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 301 | */ |
||
| 302 | 8 | public function getWorkingCopyUrl($wc_path) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Returns last changed revision on path/url. |
||
| 332 | * |
||
| 333 | * @param string $path_or_url Path or url. |
||
| 334 | * |
||
| 335 | * @return integer |
||
| 336 | */ |
||
| 337 | 7 | public function getLastRevision($path_or_url) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Determines if given path is in fact an url. |
||
| 347 | * |
||
| 348 | * @param string $path Path. |
||
| 349 | * |
||
| 350 | * @return boolean |
||
| 351 | */ |
||
| 352 | 24 | public function isUrl($path) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Removes credentials from url. |
||
| 359 | * |
||
| 360 | * @param string $url URL. |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | * @throws \InvalidArgumentException When non-url given. |
||
| 364 | */ |
||
| 365 | 15 | public function removeCredentials($url) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
| 376 | * |
||
| 377 | * @param string $repository_url Repository url. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | * @see getRefByPath |
||
| 381 | */ |
||
| 382 | 9 | public function getProjectUrl($repository_url) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Returns "svn info" entry for path or url. |
||
| 394 | * |
||
| 395 | * @param string $path_or_url Path or url. |
||
| 396 | * @param mixed $cache_duration Cache duration. |
||
| 397 | * |
||
| 398 | * @return \SimpleXMLElement |
||
| 399 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
| 400 | */ |
||
| 401 | 19 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Returns path of "svn info" entry. |
||
| 433 | * |
||
| 434 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | 17 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Returns revision, when path was added to repository. |
||
| 453 | * |
||
| 454 | * @param string $url Url. |
||
| 455 | * |
||
| 456 | * @return integer |
||
| 457 | * @throws \InvalidArgumentException When not an url was given. |
||
| 458 | */ |
||
| 459 | public function getFirstRevision($url) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Returns conflicts in working copy. |
||
| 472 | * |
||
| 473 | * @param string $wc_path Working copy path. |
||
| 474 | * |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | public function getWorkingCopyConflicts($wc_path) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Returns compact working copy status. |
||
| 492 | * |
||
| 493 | * @param string $wc_path Working copy path. |
||
| 494 | * @param boolean $with_unversioned With unversioned. |
||
| 495 | * |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | public function getCompactWorkingCopyStatus($wc_path, $with_unversioned = true) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Returns short item status. |
||
| 518 | * |
||
| 519 | * @param string $status Status. |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | * @throws \InvalidArgumentException When unknown status given. |
||
| 523 | */ |
||
| 524 | protected function getShortItemStatus($status) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Returns short item status. |
||
| 552 | * |
||
| 553 | * @param string $status Status. |
||
| 554 | * |
||
| 555 | * @return string |
||
| 556 | * @throws \InvalidArgumentException When unknown status given. |
||
| 557 | */ |
||
| 558 | protected function getShortPropertiesStatus($status) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns working copy status. |
||
| 576 | * |
||
| 577 | * @param string $wc_path Working copy path. |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | public function getWorkingCopyStatus($wc_path) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Determines if working copy contains mixed revisions. |
||
| 614 | * |
||
| 615 | * @param string $wc_path Working copy path. |
||
| 616 | * |
||
| 617 | * @return array |
||
| 618 | */ |
||
| 619 | public function isMixedRevisionWorkingCopy($wc_path) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Determines if there is a working copy on a given path. |
||
| 644 | * |
||
| 645 | * @param string $path Path. |
||
| 646 | * |
||
| 647 | * @return boolean |
||
| 648 | * @throws \InvalidArgumentException When path isn't found. |
||
| 649 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 650 | */ |
||
| 651 | public function isWorkingCopy($path) |
||
| 670 | |||
| 671 | } |
||
| 672 |