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) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Builds command from given arguments. |
||
| 161 | * |
||
| 162 | * @param string $sub_command Command. |
||
| 163 | * @param string $param_string Parameter string. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | * @throws \InvalidArgumentException When command contains spaces. |
||
| 167 | */ |
||
| 168 | 32 | protected function buildCommand($sub_command, $param_string = null) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Sets cache configuration for next created command. |
||
| 197 | * |
||
| 198 | * @param mixed $cache_duration Cache duration. |
||
| 199 | * |
||
| 200 | * @return self |
||
| 201 | */ |
||
| 202 | 20 | public function withCache($cache_duration) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Returns property value. |
||
| 211 | * |
||
| 212 | * @param string $name Property name. |
||
| 213 | * @param string $path_or_url Path to get property from. |
||
| 214 | * @param mixed $revision Revision. |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 2 | public function getProperty($name, $path_or_url, $revision = null) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Returns relative path of given path/url to the root of the repository. |
||
| 231 | * |
||
| 232 | * @param string $path_or_url Path or url. |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | 3 | public function getRelativePath($path_or_url) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Returns repository root url from given path/url. |
||
| 250 | * |
||
| 251 | * @param string $path_or_url Path or url. |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 3 | public function getRootUrl($path_or_url) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Determines if path is a root of the ref. |
||
| 262 | * |
||
| 263 | * @param string $path Path to a file. |
||
| 264 | * |
||
| 265 | * @return boolean |
||
| 266 | */ |
||
| 267 | 13 | public function isRefRoot($path) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Detects ref from given path. |
||
| 280 | * |
||
| 281 | * @param string $path Path to a file. |
||
| 282 | * |
||
| 283 | * @return string|boolean |
||
| 284 | * @see getProjectUrl |
||
| 285 | */ |
||
| 286 | 22 | public function getRefByPath($path) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Returns URL of the working copy. |
||
| 297 | * |
||
| 298 | * @param string $wc_path Working copy path. |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 302 | */ |
||
| 303 | 8 | public function getWorkingCopyUrl($wc_path) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Returns last changed revision on path/url. |
||
| 333 | * |
||
| 334 | * @param string $path_or_url Path or url. |
||
| 335 | * |
||
| 336 | * @return integer |
||
| 337 | */ |
||
| 338 | 7 | public function getLastRevision($path_or_url) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Determines if given path is in fact an url. |
||
| 348 | * |
||
| 349 | * @param string $path Path. |
||
| 350 | * |
||
| 351 | * @return boolean |
||
| 352 | */ |
||
| 353 | 24 | public function isUrl($path) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Removes credentials from url. |
||
| 360 | * |
||
| 361 | * @param string $url URL. |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | * @throws \InvalidArgumentException When non-url given. |
||
| 365 | */ |
||
| 366 | 15 | public function removeCredentials($url) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
| 377 | * |
||
| 378 | * @param string $repository_url Repository url. |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | * @see getRefByPath |
||
| 382 | */ |
||
| 383 | 9 | public function getProjectUrl($repository_url) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Returns "svn info" entry for path or url. |
||
| 395 | * |
||
| 396 | * @param string $path_or_url Path or url. |
||
| 397 | * @param mixed $cache_duration Cache duration. |
||
| 398 | * |
||
| 399 | * @return \SimpleXMLElement |
||
| 400 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
| 401 | */ |
||
| 402 | 19 | private function _getSvnInfoEntry($path_or_url, $cache_duration = null) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Returns path of "svn info" entry. |
||
| 434 | * |
||
| 435 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | 17 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Returns revision, when path was added to repository. |
||
| 454 | * |
||
| 455 | * @param string $url Url. |
||
| 456 | * |
||
| 457 | * @return integer |
||
| 458 | * @throws \InvalidArgumentException When not an url was given. |
||
| 459 | */ |
||
| 460 | public function getFirstRevision($url) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Returns conflicts in working copy. |
||
| 473 | * |
||
| 474 | * @param string $wc_path Working copy path. |
||
| 475 | * |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | public function getWorkingCopyConflicts($wc_path) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns compact working copy status. |
||
| 493 | * |
||
| 494 | * @param string $wc_path Working copy path. |
||
| 495 | * @param boolean $with_unversioned With unversioned. |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public function getCompactWorkingCopyStatus($wc_path, $with_unversioned = true) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Returns short item status. |
||
| 519 | * |
||
| 520 | * @param string $status Status. |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | * @throws \InvalidArgumentException When unknown status given. |
||
| 524 | */ |
||
| 525 | protected function getShortItemStatus($status) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Returns short item status. |
||
| 553 | * |
||
| 554 | * @param string $status Status. |
||
| 555 | * |
||
| 556 | * @return string |
||
| 557 | * @throws \InvalidArgumentException When unknown status given. |
||
| 558 | */ |
||
| 559 | protected function getShortPropertiesStatus($status) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Returns working copy status. |
||
| 577 | * |
||
| 578 | * @param string $wc_path Working copy path. |
||
| 579 | * |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | public function getWorkingCopyStatus($wc_path) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Determines if working copy contains mixed revisions. |
||
| 615 | * |
||
| 616 | * @param string $wc_path Working copy path. |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | public function isMixedRevisionWorkingCopy($wc_path) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Determines if there is a working copy on a given path. |
||
| 645 | * |
||
| 646 | * @param string $path Path. |
||
| 647 | * |
||
| 648 | * @return boolean |
||
| 649 | * @throws \InvalidArgumentException When path isn't found. |
||
| 650 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 651 | */ |
||
| 652 | public function isWorkingCopy($path) |
||
| 671 | |||
| 672 | } |
||
| 673 |