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 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $fetchURL = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * push url of named remote |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $pushURL = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * HEAD branch of named remote |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $remoteHEAD = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $branches; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Class constructor |
||
| 75 | * |
||
| 76 | * @param \GitElephant\Repository $repository repository instance |
||
| 77 | * @param string $name remote name |
||
| 78 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
| 79 | * |
||
| 80 | * @throws \RuntimeException |
||
| 81 | * @throws \InvalidArgumentException |
||
| 82 | * @throws \UnexpectedValueException |
||
| 83 | * @return \GitElephant\Objects\Remote |
||
|
|
|||
| 84 | */ |
||
| 85 | 14 | public function __construct(Repository $repository, string $name = null, bool $queryRemotes = true) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Static constructor |
||
| 98 | * |
||
| 99 | * @param \GitElephant\Repository $repository repository instance |
||
| 100 | * @param string $name remote name |
||
| 101 | * @param bool $queryRemotes Fetch new information from remotes |
||
| 102 | * |
||
| 103 | * @return \GitElephant\Objects\Remote |
||
| 104 | */ |
||
| 105 | 1 | public static function pick(Repository $repository, string $name = null, bool $queryRemotes = true) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * get output lines from git-remote --verbose |
||
| 112 | * |
||
| 113 | * @param RemoteCommand $remoteCmd Optionally provide RemoteCommand object |
||
| 114 | * |
||
| 115 | * @throws \RuntimeException |
||
| 116 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
| 117 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
| 118 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | 2 | public function getVerboseOutput(RemoteCommand $remoteCmd = null) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * get output lines from git-remote show [name] |
||
| 133 | * |
||
| 134 | * NOTE: for technical reasons $name is optional, however under normal |
||
| 135 | * implementation it SHOULD be passed! |
||
| 136 | * |
||
| 137 | * @param string $name Name of remote to show details |
||
| 138 | * @param RemoteCommand $remoteCmd Optionally provide RemoteCommand object |
||
| 139 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
| 140 | * |
||
| 141 | * @throws \RuntimeException |
||
| 142 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
| 143 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
| 144 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | 2 | public function getShowOutput(string $name = null, RemoteCommand $remoteCmd = null, bool $queryRemotes = true) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * get/store the properties from git-remote command |
||
| 159 | * |
||
| 160 | * NOTE: the name property should be set if this is to do anything, |
||
| 161 | * otherwise it's likely to throw |
||
| 162 | * |
||
| 163 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
| 164 | * |
||
| 165 | * @throws \RuntimeException |
||
| 166 | * @throws \UnexpectedValueException |
||
| 167 | * @throws \InvalidArgumentException |
||
| 168 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 169 | * @return \GitElephant\Objects\Remote |
||
| 170 | */ |
||
| 171 | 3 | private function createFromCommand(bool $queryRemotes = true) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * parse details from remote show |
||
| 194 | * |
||
| 195 | * @param array|string $remoteDetails Output lines for a remote show |
||
| 196 | * |
||
| 197 | * @throws \UnexpectedValueException |
||
| 198 | */ |
||
| 199 | 8 | public function parseOutputLines(array $remoteDetails) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * provided with the start points of the branch details, parse out the |
||
| 250 | * branch details and return a structured representation of said details |
||
| 251 | * |
||
| 252 | * @param array $groupLines Associative array whose values are line numbers |
||
| 253 | * are respective of the "group" detail present in $remoteDetails |
||
| 254 | * @param array $remoteDetails Output of git-remote show [name] |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 8 | protected function aggregateBranchDetails($groupLines, $remoteDetails) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * parse the details related to remote branch references |
||
| 294 | * |
||
| 295 | * @param array $lines |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 8 | public function parseRemoteBranches(array $lines) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * parse the details related to local branches and the remotes that they |
||
| 317 | * merge with |
||
| 318 | * |
||
| 319 | * @param array $lines |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 8 | public function parseLocalPullBranches($lines) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * parse the details related to local branches and the remotes that they |
||
| 341 | * push to |
||
| 342 | * |
||
| 343 | * @param array $lines |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | 8 | public function parseLocalPushRefs($lines) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * parse remote name from git-remote show [name] output line |
||
| 366 | * |
||
| 367 | * @param string $line |
||
| 368 | * |
||
| 369 | * @return string remote name or blank if invalid |
||
| 370 | */ |
||
| 371 | 8 | public function parseName($line) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * get the matches from an output line |
||
| 385 | * |
||
| 386 | * @param string $remoteString remote line output |
||
| 387 | * |
||
| 388 | * @throws \InvalidArgumentException |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | 3 | public static function getMatches($remoteString) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * toString magic method |
||
| 404 | * |
||
| 405 | * @return string the named remote |
||
| 406 | */ |
||
| 407 | 1 | public function __toString() |
|
| 411 | |||
| 412 | /** |
||
| 413 | * name setter |
||
| 414 | * |
||
| 415 | * @param string $name the remote name |
||
| 416 | */ |
||
| 417 | 1 | public function setName($name) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * name getter |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | 4 | public function getName() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * fetchURL getter |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 2 | public function getFetchURL() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * fetchURL setter |
||
| 444 | * |
||
| 445 | * @param string $url the fetch url |
||
| 446 | */ |
||
| 447 | 1 | public function setFetchURL($url) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * pushURL getter |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | 2 | public function getPushURL() |
|
| 461 | |||
| 462 | /** |
||
| 463 | * pushURL setter |
||
| 464 | * |
||
| 465 | * @param string $url the push url |
||
| 466 | */ |
||
| 467 | 1 | public function setPushURL($url) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * remote HEAD branch getter |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | 2 | public function getRemoteHEAD() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * remote HEAD branch setter |
||
| 484 | * |
||
| 485 | * @param string $branchName |
||
| 486 | */ |
||
| 487 | 1 | public function setRemoteHEAD($branchName) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * get structured representation of branches |
||
| 494 | * |
||
| 495 | * @return array |
||
| 496 | */ |
||
| 497 | 1 | public function getBranches() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * set structured representation of branches |
||
| 504 | * |
||
| 505 | * @param array $branches |
||
| 506 | */ |
||
| 507 | 8 | public function setBranches(Array $branches) |
|
| 511 | } |
||
| 512 |
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.