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) |
|
192 | |||
193 | /** |
||
194 | * parse details from remote show |
||
195 | * |
||
196 | * @param array|string $remoteDetails Output lines for a remote show |
||
197 | * |
||
198 | * @throws \UnexpectedValueException |
||
199 | */ |
||
200 | 8 | public function parseOutputLines(array $remoteDetails) |
|
253 | |||
254 | /** |
||
255 | * provided with the start points of the branch details, parse out the |
||
256 | * branch details and return a structured representation of said details |
||
257 | * |
||
258 | * @param array $groupLines Associative array whose values are line numbers |
||
259 | * are respective of the "group" detail present in $remoteDetails |
||
260 | * @param array $remoteDetails Output of git-remote show [name] |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | 8 | protected function aggregateBranchDetails($groupLines, $remoteDetails) |
|
297 | |||
298 | /** |
||
299 | * parse the details related to remote branch references |
||
300 | * |
||
301 | * @param array $lines |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | 8 | public function parseRemoteBranches(array $lines) |
|
320 | |||
321 | /** |
||
322 | * parse the details related to local branches and the remotes that they |
||
323 | * merge with |
||
324 | * |
||
325 | * @param array $lines |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | 8 | public function parseLocalPullBranches($lines) |
|
344 | |||
345 | /** |
||
346 | * parse the details related to local branches and the remotes that they |
||
347 | * push to |
||
348 | * |
||
349 | * @param array $lines |
||
350 | * |
||
351 | * @return array |
||
352 | */ |
||
353 | 8 | public function parseLocalPushRefs($lines) |
|
369 | |||
370 | /** |
||
371 | * parse remote name from git-remote show [name] output line |
||
372 | * |
||
373 | * @param string $line |
||
374 | * |
||
375 | * @return string remote name or blank if invalid |
||
376 | */ |
||
377 | 8 | public function parseName($line) |
|
388 | |||
389 | /** |
||
390 | * get the matches from an output line |
||
391 | * |
||
392 | * @param string $remoteString remote line output |
||
393 | * |
||
394 | * @throws \InvalidArgumentException |
||
395 | * @return array |
||
396 | */ |
||
397 | 3 | public static function getMatches($remoteString) |
|
407 | |||
408 | /** |
||
409 | * toString magic method |
||
410 | * |
||
411 | * @return string the named remote |
||
412 | */ |
||
413 | 1 | public function __toString() |
|
417 | |||
418 | /** |
||
419 | * name setter |
||
420 | * |
||
421 | * @param string $name the remote name |
||
422 | */ |
||
423 | 1 | public function setName($name) |
|
427 | |||
428 | /** |
||
429 | * name getter |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | 4 | public function getName() |
|
437 | |||
438 | /** |
||
439 | * fetchURL getter |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 2 | public function getFetchURL() |
|
447 | |||
448 | /** |
||
449 | * fetchURL setter |
||
450 | * |
||
451 | * @param string $url the fetch url |
||
452 | */ |
||
453 | 1 | public function setFetchURL($url) |
|
457 | |||
458 | /** |
||
459 | * pushURL getter |
||
460 | * |
||
461 | * @return string |
||
462 | */ |
||
463 | 2 | public function getPushURL() |
|
467 | |||
468 | /** |
||
469 | * pushURL setter |
||
470 | * |
||
471 | * @param string $url the push url |
||
472 | */ |
||
473 | 1 | public function setPushURL($url) |
|
477 | |||
478 | /** |
||
479 | * remote HEAD branch getter |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | 2 | public function getRemoteHEAD() |
|
487 | |||
488 | /** |
||
489 | * remote HEAD branch setter |
||
490 | * |
||
491 | * @param string $branchName |
||
492 | */ |
||
493 | 1 | public function setRemoteHEAD($branchName) |
|
497 | |||
498 | /** |
||
499 | * get structured representation of branches |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | 1 | public function getBranches() |
|
507 | |||
508 | /** |
||
509 | * set structured representation of branches |
||
510 | * |
||
511 | * @param array $branches |
||
512 | */ |
||
513 | 8 | public function setBranches(Array $branches) |
|
517 | } |
||
518 |
Adding a
@return
annotation 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.