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 |
||
34 | class Remote |
||
35 | { |
||
36 | /** |
||
37 | * @var \GitElephant\Repository |
||
38 | */ |
||
39 | private $repository; |
||
40 | |||
41 | /** |
||
42 | * remote name |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $name; |
||
47 | |||
48 | /** |
||
49 | * fetch url of named remote |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $fetchURL = ''; |
||
54 | |||
55 | /** |
||
56 | * push url of named remote |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $pushURL = ''; |
||
61 | |||
62 | /** |
||
63 | * HEAD branch of named remote |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $remoteHEAD = null; |
||
68 | |||
69 | /** |
||
70 | * @var array<Branch> |
||
71 | */ |
||
72 | private $branches; |
||
73 | |||
74 | /** |
||
75 | * Class constructor |
||
76 | * |
||
77 | * @param \GitElephant\Repository $repository repository instance |
||
78 | * @param string|null $name remote name |
||
79 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
80 | * |
||
81 | * @throws \RuntimeException |
||
82 | * @throws \InvalidArgumentException |
||
83 | * @throws \UnexpectedValueException |
||
84 | */ |
||
85 | 14 | public function __construct(Repository $repository, string $name = null, bool $queryRemotes = true) |
|
93 | |||
94 | /** |
||
95 | * Static constructor |
||
96 | * |
||
97 | * @param \GitElephant\Repository $repository repository instance |
||
98 | * @param string $name remote name |
||
99 | * @param bool $queryRemotes Fetch new information from remotes |
||
100 | * |
||
101 | * @return \GitElephant\Objects\Remote |
||
102 | */ |
||
103 | 1 | public static function pick( |
|
110 | |||
111 | /** |
||
112 | * get output lines from git-remote --verbose |
||
113 | * |
||
114 | * @param RemoteCommand $remoteCmd Optionally provide RemoteCommand object |
||
115 | * |
||
116 | * @throws \RuntimeException |
||
117 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
118 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
119 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
120 | * @return array<string> |
||
121 | */ |
||
122 | 2 | public function getVerboseOutput(RemoteCommand $remoteCmd = null): array |
|
131 | |||
132 | /** |
||
133 | * get output lines from git-remote show [name] |
||
134 | * |
||
135 | * NOTE: for technical reasons $name is optional, however under normal |
||
136 | * implementation it SHOULD be passed! |
||
137 | * |
||
138 | * @param string $name Name of remote to show details |
||
139 | * @param RemoteCommand $remoteCmd Optionally provide RemoteCommand object |
||
140 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
141 | * |
||
142 | * @throws \RuntimeException |
||
143 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
144 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
145 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
146 | * @return array<string> |
||
147 | */ |
||
148 | 2 | public function getShowOutput( |
|
160 | |||
161 | /** |
||
162 | * get/store the properties from git-remote command |
||
163 | * |
||
164 | * NOTE: the name property should be set if this is to do anything, |
||
165 | * otherwise it's likely to throw |
||
166 | * |
||
167 | * @param bool $queryRemotes Do not fetch new information from remotes |
||
168 | * |
||
169 | * @throws \RuntimeException |
||
170 | * @throws \UnexpectedValueException |
||
171 | * @throws \InvalidArgumentException |
||
172 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
173 | * @return \GitElephant\Objects\Remote |
||
174 | */ |
||
175 | 3 | private function createFromCommand(bool $queryRemotes = true): self |
|
195 | |||
196 | /** |
||
197 | * parse details from remote show |
||
198 | * |
||
199 | * @param array $remoteDetails Output lines for a remote show |
||
200 | * |
||
201 | * @throws \UnexpectedValueException |
||
202 | */ |
||
203 | 8 | public function parseOutputLines(array $remoteDetails): void |
|
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<array<string>> |
||
263 | */ |
||
264 | 8 | protected function aggregateBranchDetails($groupLines, $remoteDetails): array |
|
297 | |||
298 | /** |
||
299 | * parse the details related to remote branch references |
||
300 | * |
||
301 | * @param array<string> $lines |
||
302 | * |
||
303 | * @return array // <string, array<string, string>> |
||
304 | */ |
||
305 | 8 | public function parseRemoteBranches(array $lines): array |
|
320 | |||
321 | /** |
||
322 | * parse the details related to local branches and the remotes that they |
||
323 | * merge with |
||
324 | * |
||
325 | * @param array<string> $lines |
||
326 | * |
||
327 | * @return array // <array<string>> |
||
328 | */ |
||
329 | 8 | public function parseLocalPullBranches($lines): array |
|
344 | |||
345 | /** |
||
346 | * parse the details related to local branches and the remotes that they |
||
347 | * push to |
||
348 | * |
||
349 | * @param array<string> $lines |
||
350 | * |
||
351 | * @return array // <array<string>> |
||
352 | */ |
||
353 | 8 | public function parseLocalPushRefs($lines): array |
|
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(string $remoteString): array |
|
407 | |||
408 | /** |
||
409 | * toString magic method |
||
410 | * |
||
411 | * @return string the named remote |
||
412 | */ |
||
413 | 1 | public function __toString(): string |
|
417 | |||
418 | /** |
||
419 | * name setter |
||
420 | * |
||
421 | * @param string $name the remote name |
||
422 | */ |
||
423 | 1 | public function setName($name): void |
|
427 | |||
428 | /** |
||
429 | * name getter |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | 4 | public function getName(): string |
|
437 | |||
438 | /** |
||
439 | * fetchURL getter |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 2 | public function getFetchURL(): string |
|
447 | |||
448 | /** |
||
449 | * fetchURL setter |
||
450 | * |
||
451 | * @param string $url the fetch url |
||
452 | */ |
||
453 | 1 | public function setFetchURL($url): void |
|
457 | |||
458 | /** |
||
459 | * pushURL getter |
||
460 | * |
||
461 | * @return string |
||
462 | */ |
||
463 | 2 | public function getPushURL(): string |
|
467 | |||
468 | /** |
||
469 | * pushURL setter |
||
470 | * |
||
471 | * @param string $url the push url |
||
472 | */ |
||
473 | 1 | public function setPushURL($url): void |
|
477 | |||
478 | /** |
||
479 | * remote HEAD branch getter |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | 2 | public function getRemoteHEAD(): string |
|
487 | |||
488 | /** |
||
489 | * remote HEAD branch setter |
||
490 | * |
||
491 | * @param string $branchName |
||
492 | */ |
||
493 | 1 | public function setRemoteHEAD($branchName): void |
|
497 | |||
498 | /** |
||
499 | * get structured representation of branches |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | 1 | public function getBranches(): array |
|
507 | |||
508 | /** |
||
509 | * set structured representation of branches |
||
510 | * |
||
511 | * @param array $branches |
||
512 | */ |
||
513 | 8 | public function setBranches(array $branches): void |
|
517 | } |
||
518 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: