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 | /** |
||
| 29 | * Reference to configuration. |
||
| 30 | * |
||
| 31 | * @var ConfigEditor |
||
| 32 | */ |
||
| 33 | private $_configEditor; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Process factory. |
||
| 37 | * |
||
| 38 | * @var IProcessFactory |
||
| 39 | */ |
||
| 40 | private $_processFactory; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Console IO. |
||
| 44 | * |
||
| 45 | * @var ConsoleIO |
||
| 46 | */ |
||
| 47 | private $_io; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Cache manager. |
||
| 51 | * |
||
| 52 | * @var CacheManager |
||
| 53 | */ |
||
| 54 | private $_cacheManager; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Path to an svn command. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $_svnCommand = 'svn'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Cache duration for next invoked command. |
||
| 65 | * |
||
| 66 | * @var mixed |
||
| 67 | */ |
||
| 68 | private $_nextCommandCacheDuration = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Whatever to cache last repository revision or not. |
||
| 72 | * |
||
| 73 | * @var mixed |
||
| 74 | */ |
||
| 75 | private $_lastRevisionCacheDuration = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Creates repository connector. |
||
| 79 | * |
||
| 80 | * @param ConfigEditor $config_editor ConfigEditor. |
||
| 81 | * @param IProcessFactory $process_factory Process factory. |
||
| 82 | * @param ConsoleIO $io Console IO. |
||
| 83 | * @param CacheManager $cache_manager Cache manager. |
||
| 84 | */ |
||
| 85 | 41 | public function __construct( |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Prepares static part of svn command to be used across the script. |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | 41 | protected function prepareSvnCommand() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Builds a command. |
||
| 130 | * |
||
| 131 | * @param string $sub_command Sub command. |
||
| 132 | * @param string|null $param_string Parameter string. |
||
| 133 | * |
||
| 134 | * @return Command |
||
| 135 | */ |
||
| 136 | 29 | public function getCommand($sub_command, $param_string = null) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Builds command from given arguments. |
||
| 156 | * |
||
| 157 | * @param string $sub_command Command. |
||
| 158 | * @param string $param_string Parameter string. |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | * @throws \InvalidArgumentException When command contains spaces. |
||
| 162 | */ |
||
| 163 | 29 | protected function buildCommand($sub_command, $param_string = null) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Sets cache configuration for next created command. |
||
| 192 | * |
||
| 193 | * @param mixed $cache_duration Cache duration. |
||
| 194 | * |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | 11 | public function withCache($cache_duration) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Returns property value. |
||
| 206 | * |
||
| 207 | * @param string $name Property name. |
||
| 208 | * @param string $path_or_url Path to get property from. |
||
| 209 | * @param mixed $revision Revision. |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 2 | public function getProperty($name, $path_or_url, $revision = null) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Returns relative path of given path/url to the root of the repository. |
||
| 226 | * |
||
| 227 | * @param string $path_or_url Path or url. |
||
| 228 | * @param mixed $cache_duration Cache duration. |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 3 | public function getRelativePath($path_or_url, $cache_duration = null) |
|
| 233 | { |
||
| 234 | // Cache "svn info" commands to remote urls, not the working copy. |
||
| 235 | 3 | if ( !isset($cache_duration) && $this->isUrl($path_or_url) ) { |
|
| 236 | 1 | $cache_duration = '1 year'; |
|
| 237 | 1 | } |
|
| 238 | |||
| 239 | 3 | $svn_info = $this->withCache($cache_duration)->_getSvnInfoEntry($path_or_url); |
|
| 240 | |||
| 241 | 3 | $wc_url = (string)$svn_info->url; |
|
| 242 | 3 | $repository_root_url = (string)$svn_info->repository->root; |
|
| 243 | |||
| 244 | 3 | return preg_replace('/^' . preg_quote($repository_root_url, '/') . '/', '', $wc_url, 1); |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns URL of the working copy. |
||
| 249 | * |
||
| 250 | * @param string $wc_path Working copy path. |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 254 | */ |
||
| 255 | 7 | public function getWorkingCopyUrl($wc_path) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Returns last changed revision on path/url. |
||
| 285 | * |
||
| 286 | * @param string $path_or_url Path or url. |
||
| 287 | * @param mixed $cache_duration Cache duration. |
||
| 288 | * |
||
| 289 | * @return integer |
||
| 290 | */ |
||
| 291 | 7 | public function getLastRevision($path_or_url, $cache_duration = null) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Determines if given path is in fact an url. |
||
| 305 | * |
||
| 306 | * @param string $path Path. |
||
| 307 | * |
||
| 308 | * @return boolean |
||
| 309 | */ |
||
| 310 | 15 | public function isUrl($path) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Returns project url (container for "trunk/branches/tags/releases" folders). |
||
| 317 | * |
||
| 318 | * @param string $repository_url Repository url. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 9 | public function getProjectUrl($repository_url) |
|
| 323 | { |
||
| 324 | 9 | if ( preg_match('#^(.*?)/(trunk|branches|tags|releases).*$#', $repository_url, $regs) ) { |
|
| 325 | 8 | return $regs[1]; |
|
| 326 | } |
||
| 327 | |||
| 328 | 1 | return $repository_url; |
|
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns "svn info" entry for path or url. |
||
| 333 | * |
||
| 334 | * @param string $path_or_url Path or url. |
||
| 335 | * |
||
| 336 | * @return \SimpleXMLElement |
||
| 337 | * @throws \LogicException When unexpected 'svn info' results retrieved. |
||
| 338 | */ |
||
| 339 | 16 | private function _getSvnInfoEntry($path_or_url) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Returns path of "svn info" entry. |
||
| 353 | * |
||
| 354 | * @param \SimpleXMLElement $svn_info_entry The "entry" node of "svn info" command. |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 14 | private function _getSvnInfoEntryPath(\SimpleXMLElement $svn_info_entry) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Returns revision, when path was added to repository. |
||
| 373 | * |
||
| 374 | * @param string $url Url. |
||
| 375 | * |
||
| 376 | * @return integer |
||
| 377 | * @throws \InvalidArgumentException When not an url was given. |
||
| 378 | */ |
||
| 379 | public function getFirstRevision($url) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns conflicts in working copy. |
||
| 392 | * |
||
| 393 | * @param string $wc_path Working copy path. |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function getWorkingCopyConflicts($wc_path) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns compact working copy status. |
||
| 412 | * |
||
| 413 | * @param string $wc_path Working copy path. |
||
| 414 | * @param boolean $with_unversioned With unversioned. |
||
| 415 | * |
||
| 416 | * @return string |
||
| 417 | */ |
||
| 418 | public function getCompactWorkingCopyStatus($wc_path, $with_unversioned = true) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns short item status. |
||
| 438 | * |
||
| 439 | * @param string $status Status. |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | * @throws \InvalidArgumentException When unknown status given. |
||
| 443 | */ |
||
| 444 | protected function getShortItemStatus($status) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Returns short item status. |
||
| 472 | * |
||
| 473 | * @param string $status Status. |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | * @throws \InvalidArgumentException When unknown status given. |
||
| 477 | */ |
||
| 478 | protected function getShortPropertiesStatus($status) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Returns working copy status. |
||
| 496 | * |
||
| 497 | * @param string $wc_path Working copy path. |
||
| 498 | * |
||
| 499 | * @return array |
||
| 500 | */ |
||
| 501 | protected function getWorkingCopyStatus($wc_path) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Determines if working copy contains mixed revisions. |
||
| 534 | * |
||
| 535 | * @param string $wc_path Working copy path. |
||
| 536 | * |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | public function isMixedRevisionWorkingCopy($wc_path) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Determines if there is a working copy on a given path. |
||
| 564 | * |
||
| 565 | * @param string $path Path. |
||
| 566 | * |
||
| 567 | * @return boolean |
||
| 568 | * @throws \InvalidArgumentException When path isn't found. |
||
| 569 | * @throws RepositoryCommandException When repository command failed to execute. |
||
| 570 | */ |
||
|
1 ignored issue
–
show
|
|||
| 571 | public function isWorkingCopy($path) |
||
| 590 | |||
| 591 | } |
||
| 592 |