Complex classes like Remote 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 Remote, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Remote |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var \GitElephant\Repository |
||
| 37 | */ |
||
| 38 | private $repository; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * remote name |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $name; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * fetch url of named remote |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $fetchURL = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * push url of named remote |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $pushURL = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * HEAD branch of named remote |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $remoteHEAD = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $branches; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Class constructor |
||
| 72 | * |
||
| 73 | * @param \GitElephant\Repository $repository repository instance |
||
| 74 | * @param string $name remote name |
||
| 75 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
| 76 | * |
||
| 77 | * @throws \RuntimeException |
||
| 78 | * @throws \InvalidArgumentException |
||
| 79 | * @throws \UnexpectedValueException |
||
| 80 | * @return \GitElephant\Objects\Remote |
||
|
|
|||
| 81 | */ |
||
| 82 | 14 | public function __construct(Repository $repository, $name = null, $queryRemotes = true) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Static constructor |
||
| 95 | * |
||
| 96 | * @param \GitElephant\Repository $repository repository instance |
||
| 97 | * @param string $name remote name |
||
| 98 | * @param bool $queryRemotes Fetch new information from remotes |
||
| 99 | * |
||
| 100 | * @return \GitElephant\Objects\Remote |
||
| 101 | */ |
||
| 102 | 1 | public static function pick(Repository $repository, $name = null, $queryRemotes = true) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * get output lines from git-remote --verbose |
||
| 109 | * |
||
| 110 | * @param RemoteCommand $remoteCmd Optionally provide RemoteCommand object |
||
| 111 | * |
||
| 112 | * @throws \RuntimeException |
||
| 113 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
| 114 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
| 115 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | 2 | public function getVerboseOutput(RemoteCommand $remoteCmd = null) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * get output lines from git-remote show [name] |
||
| 130 | * |
||
| 131 | * NOTE: for technical reasons $name is optional, however under normal |
||
| 132 | * implementation it SHOULD be passed! |
||
| 133 | * |
||
| 134 | * @param string $name Name of remote to show details |
||
| 135 | * @param RemoteCommand $remoteCmd Optionally provide RemoteCommand object |
||
| 136 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
| 137 | * |
||
| 138 | * @throws \RuntimeException |
||
| 139 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
| 140 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
| 141 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | 2 | public function getShowOutput($name = null, RemoteCommand $remoteCmd = null, $queryRemotes = true) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * get/store the properties from git-remote command |
||
| 156 | * |
||
| 157 | * NOTE: the name property should be set if this is to do anything, |
||
| 158 | * otherwise it's likely to throw |
||
| 159 | * |
||
| 160 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
| 161 | * |
||
| 162 | * @throws \RuntimeException |
||
| 163 | * @throws \UnexpectedValueException |
||
| 164 | * @throws \InvalidArgumentException |
||
| 165 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 166 | * @return \GitElephant\Objects\Remote |
||
| 167 | */ |
||
| 168 | 3 | private function createFromCommand($queryRemotes = true) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * parse details from remote show |
||
| 191 | * |
||
| 192 | * @param array|string $remoteDetails Output lines for a remote show |
||
| 193 | * |
||
| 194 | * @throws \UnexpectedValueException |
||
| 195 | */ |
||
| 196 | 8 | public function parseOutputLines(Array $remoteDetails) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * provided with the start points of the branch details, parse out the |
||
| 247 | * branch details and return a structured representation of said details |
||
| 248 | * |
||
| 249 | * @param array $groupLines Associative array whose values are line numbers |
||
| 250 | * are respective of the "group" detail present in $remoteDetails |
||
| 251 | * @param array $remoteDetails Output of git-remote show [name] |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | 8 | protected function aggregateBranchDetails($groupLines, $remoteDetails) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * parse the details related to remote branch references |
||
| 284 | * |
||
| 285 | * @param array $lines |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | 8 | public function parseRemoteBranches($lines) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * parse the details related to local branches and the remotes that they |
||
| 307 | * merge with |
||
| 308 | * |
||
| 309 | * @param array $lines |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | 8 | public function parseLocalPullBranches($lines) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * parse the details related to local branches and the remotes that they |
||
| 331 | * push to |
||
| 332 | * |
||
| 333 | * @param array $lines |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | 8 | public function parseLocalPushRefs($lines) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * parse remote name from git-remote show [name] output line |
||
| 356 | * |
||
| 357 | * @param string $line |
||
| 358 | * |
||
| 359 | * @return string remote name or blank if invalid |
||
| 360 | */ |
||
| 361 | 8 | public function parseName($line) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * get the matches from an output line |
||
| 375 | * |
||
| 376 | * @param string $remoteString remote line output |
||
| 377 | * |
||
| 378 | * @throws \InvalidArgumentException |
||
| 379 | * @return array |
||
| 380 | */ |
||
| 381 | 3 | public static function getMatches($remoteString) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * toString magic method |
||
| 394 | * |
||
| 395 | * @return string the named remote |
||
| 396 | */ |
||
| 397 | 1 | public function __toString() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * name setter |
||
| 404 | * |
||
| 405 | * @param string $name the remote name |
||
| 406 | */ |
||
| 407 | 1 | public function setName($name) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * name getter |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | 4 | public function getName() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * fetchURL getter |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | 2 | public function getFetchURL() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * fetchURL setter |
||
| 434 | * |
||
| 435 | * @param string $url the fetch url |
||
| 436 | */ |
||
| 437 | 1 | public function setFetchURL($url) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * pushURL getter |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 2 | public function getPushURL() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * pushURL setter |
||
| 454 | * |
||
| 455 | * @param string $url the push url |
||
| 456 | */ |
||
| 457 | 1 | public function setPushURL($url) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * remote HEAD branch getter |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | 2 | public function getRemoteHEAD() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * remote HEAD branch setter |
||
| 474 | * |
||
| 475 | * @param string $branchName |
||
| 476 | */ |
||
| 477 | 1 | public function setRemoteHEAD($branchName) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * get structured representation of branches |
||
| 484 | * |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | 1 | public function getBranches() |
|
| 491 | |||
| 492 | /** |
||
| 493 | * set structured representation of branches |
||
| 494 | * |
||
| 495 | * @param array $branches |
||
| 496 | */ |
||
| 497 | 8 | public function setBranches(Array $branches) |
|
| 501 | } |
||
| 502 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.