Complex classes like Host 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 Host, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Host |
||
15 | { |
||
16 | private $config; |
||
17 | private $sshArguments; |
||
18 | |||
19 | 21 | public function __construct(string $hostname) |
|
20 | { |
||
21 | 21 | $parent = null; |
|
22 | 21 | if (Deployer::get()) { |
|
23 | 19 | $parent = Deployer::get()->config; |
|
24 | } |
||
25 | 21 | $this->config = new Configuration($parent); |
|
|
|||
26 | 21 | $this->set('alias', $hostname); |
|
27 | 21 | $this->set('hostname', preg_replace('/\/.+$/', '', $hostname)); |
|
28 | 21 | $this->set('remote_user', ''); |
|
29 | 21 | $this->set('port', ''); |
|
30 | 21 | $this->set('config_file', ''); |
|
31 | 21 | $this->set('identity_file', ''); |
|
32 | 21 | $this->set('forward_agent', true); |
|
33 | 21 | $this->set('shell', 'bash -s'); |
|
34 | 21 | $this->sshArguments = new Arguments(); |
|
35 | 21 | } |
|
36 | |||
37 | 9 | public function getConfig() |
|
38 | { |
||
39 | 9 | return $this->config; |
|
40 | } |
||
41 | |||
42 | 21 | public function set(string $name, $value) |
|
43 | { |
||
44 | 21 | $this->config->set($name, $value); |
|
45 | 21 | return $this; |
|
46 | } |
||
47 | |||
48 | 1 | public function add(string $name, array $value) |
|
53 | |||
54 | public function has(string $name): bool |
||
55 | { |
||
58 | |||
59 | 14 | public function get(string $name, $default = null) |
|
63 | |||
64 | 17 | public function getAlias() |
|
68 | |||
69 | public function setTag(string $tag) |
||
74 | |||
75 | 9 | public function getTag(): string |
|
79 | |||
80 | 1 | public function setHostname(string $hostname) |
|
85 | |||
86 | 3 | public function getHostname() |
|
90 | |||
91 | 1 | public function setRemoteUser($user) |
|
96 | |||
97 | 2 | public function getRemoteUser() |
|
101 | |||
102 | 1 | public function setPort(int $port) |
|
107 | |||
108 | 5 | public function getPort() |
|
112 | |||
113 | 1 | public function setConfigFile(string $file) |
|
118 | |||
119 | 3 | public function getConfigFile() |
|
123 | |||
124 | 1 | public function setIdentityFile($file) |
|
129 | |||
130 | 4 | public function getIdentityFile() |
|
134 | |||
135 | 1 | public function setForwardAgent(bool $on) |
|
140 | |||
141 | 3 | public function getForwardAgent() |
|
145 | |||
146 | 1 | public function setSshMultiplexing(bool $on) |
|
151 | |||
152 | 2 | public function getSshMultiplexing() |
|
156 | |||
157 | public function setShell(string $command) |
||
162 | |||
163 | public function getShell(): string |
||
167 | |||
168 | public function getConnectionString(): string |
||
175 | |||
176 | 3 | public function getSshArguments() |
|
181 | |||
182 | // TODO: Migrate to configuration. |
||
183 | |||
184 | 2 | public function setSshOptions(array $options) |
|
189 | |||
190 | // TODO: Migrate to configuration. |
||
191 | |||
192 | 2 | public function setSshFlags(array $flags) |
|
197 | |||
198 | 3 | private function initOptions() |
|
216 | |||
217 | 9 | private function generateTag() |
|
304 | } |
||
305 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.