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 |
||
15 | class Host |
||
16 | { |
||
17 | private $config; |
||
18 | private $sshArguments; |
||
19 | |||
20 | 25 | public function __construct(string $hostname) |
|
21 | { |
||
22 | 25 | $parent = null; |
|
23 | 25 | if (Deployer::get()) { |
|
24 | 23 | $parent = Deployer::get()->config; |
|
25 | } |
||
26 | 25 | $this->config = new Configuration($parent); |
|
27 | 25 | $this->set('alias', $hostname); |
|
28 | 25 | $this->set('hostname', preg_replace('/\/.+$/', '', $hostname)); |
|
29 | 25 | $this->sshArguments = new Arguments(); |
|
30 | 25 | } |
|
31 | |||
32 | 12 | public function config() |
|
33 | { |
||
34 | 12 | return $this->config; |
|
35 | } |
||
36 | |||
37 | 25 | public function set(string $name, $value) |
|
38 | { |
||
39 | 25 | $this->config->set($name, $value); |
|
40 | 25 | return $this; |
|
41 | } |
||
42 | |||
43 | 1 | public function add(string $name, array $value) |
|
44 | { |
||
45 | 1 | $this->config->add($name, $value); |
|
46 | 1 | return $this; |
|
47 | } |
||
48 | |||
49 | public function has(string $name): bool |
||
50 | { |
||
51 | return $this->config->has($name); |
||
52 | } |
||
53 | |||
54 | 19 | public function get(string $name, $default = null) |
|
55 | { |
||
56 | 19 | return $this->config->get($name, $default); |
|
57 | } |
||
58 | |||
59 | 19 | public function getAlias() |
|
60 | { |
||
61 | 19 | return $this->config->get('alias'); |
|
62 | } |
||
63 | |||
64 | public function setTag(string $tag) |
||
65 | { |
||
66 | $this->config->set('tag', $tag); |
||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | 11 | public function getTag(): string |
|
71 | { |
||
72 | 11 | return $this->config->get('tag', $this->generateTag()); |
|
73 | } |
||
74 | |||
75 | 1 | public function setHostname(string $hostname) |
|
76 | { |
||
77 | 1 | $this->config->set('hostname', $hostname); |
|
78 | 1 | return $this; |
|
79 | } |
||
80 | |||
81 | 3 | public function getHostname() |
|
82 | { |
||
83 | 3 | return $this->config->get('hostname'); |
|
84 | } |
||
85 | |||
86 | 1 | public function setRemoteUser($user) |
|
87 | { |
||
88 | 1 | $this->config->set('remote_user', $user); |
|
89 | 1 | return $this; |
|
90 | } |
||
91 | |||
92 | 2 | public function getRemoteUser() |
|
93 | { |
||
94 | 2 | return $this->config->get('remote_user'); |
|
95 | } |
||
96 | |||
97 | 1 | public function setPort(int $port) |
|
98 | { |
||
99 | 1 | $this->config->set('port', $port); |
|
100 | 1 | return $this; |
|
101 | } |
||
102 | |||
103 | 3 | public function getPort() |
|
104 | { |
||
105 | 3 | return $this->config->get('port'); |
|
106 | } |
||
107 | |||
108 | 1 | public function setConfigFile(string $file) |
|
109 | { |
||
110 | 1 | $this->config->set('config_file', $file); |
|
111 | 1 | return $this; |
|
112 | } |
||
113 | |||
114 | 3 | public function getConfigFile() |
|
115 | { |
||
116 | 3 | return $this->config->get('config_file'); |
|
117 | } |
||
118 | |||
119 | 1 | public function setIdentityFile($file) |
|
120 | { |
||
121 | 1 | $this->config->set('identity_file', $file); |
|
122 | 1 | return $this; |
|
123 | } |
||
124 | |||
125 | 4 | public function getIdentityFile() |
|
126 | { |
||
127 | 4 | return $this->config->get('identity_file'); |
|
128 | } |
||
129 | |||
130 | 1 | public function setForwardAgent(bool $on) |
|
131 | { |
||
132 | 1 | $this->config->set('forward_agent', $on); |
|
133 | 1 | return $this; |
|
134 | } |
||
135 | |||
136 | 3 | public function getForwardAgent() |
|
137 | { |
||
138 | 3 | return $this->config->get('forward_agent'); |
|
139 | } |
||
140 | |||
141 | 1 | public function setSshMultiplexing(bool $on) |
|
142 | { |
||
143 | 1 | $this->config->set('ssh_multiplexing', $on); |
|
144 | 1 | return $this; |
|
145 | } |
||
146 | |||
147 | 2 | public function getSshMultiplexing() |
|
148 | { |
||
149 | 2 | return $this->config->get('ssh_multiplexing'); |
|
150 | } |
||
151 | |||
152 | public function setShell(string $command) |
||
153 | { |
||
154 | $this->config->set('shell', $command); |
||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | public function getShell(): string |
||
159 | { |
||
160 | return $this->config->get('shell'); |
||
161 | } |
||
162 | |||
163 | public function setDeployPath(string $path) |
||
164 | { |
||
165 | $this->config->set('deploy_path', $path); |
||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | public function getDeployPath() |
||
170 | { |
||
171 | return $this->config->get('deploy_path'); |
||
172 | } |
||
173 | |||
174 | 2 | public function getConnectionString(): string |
|
175 | { |
||
176 | 2 | if ($this->get('remote_user', '') !== '') { |
|
177 | return $this->get('remote_user') . '@' . $this->get('hostname'); |
||
178 | } |
||
179 | 2 | return $this->get('hostname'); |
|
180 | } |
||
181 | |||
182 | 3 | public function getSshArguments() |
|
187 | |||
188 | // TODO: Migrate to configuration. |
||
189 | |||
190 | 2 | public function setSshOptions(array $options) |
|
191 | { |
||
192 | 2 | $this->sshArguments = $this->sshArguments->withOptions($options); |
|
193 | 2 | return $this; |
|
194 | } |
||
195 | |||
196 | // TODO: Migrate to configuration. |
||
197 | |||
198 | 2 | public function setSshFlags(array $flags) |
|
199 | { |
||
200 | 2 | $this->sshArguments = $this->sshArguments->withFlags($flags); |
|
201 | 2 | return $this; |
|
202 | } |
||
203 | |||
204 | 3 | private function initOptions() |
|
205 | { |
||
206 | 3 | if ($this->getPort()) { |
|
207 | 3 | $this->sshArguments = $this->sshArguments->withFlag('-p', $this->getPort()); |
|
208 | } |
||
209 | |||
210 | 3 | if ($this->getConfigFile()) { |
|
211 | 2 | $this->sshArguments = $this->sshArguments->withFlag('-F', $this->getConfigFile()); |
|
212 | } |
||
213 | |||
214 | 3 | if ($this->getIdentityFile()) { |
|
215 | 2 | $this->sshArguments = $this->sshArguments->withFlag('-i', $this->getIdentityFile()); |
|
216 | } |
||
217 | |||
222 | |||
223 | 11 | private function generateTag() |
|
310 | } |
||
311 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.