Complex classes like SecureShell 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 SecureShell, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class SecureShell extends Module |
||
12 | { |
||
13 | |||
14 | const DEFAULT_PORT = 22; |
||
15 | const AUTH_PASSWORD = 1; |
||
16 | const AUTH_PUBKEY = 2; |
||
17 | const AUTH_HOSTKEY = 3; |
||
18 | const AUTH_AGENT = 4; |
||
19 | const AUTH_NONE = 0; |
||
20 | |||
21 | protected $config = []; |
||
22 | |||
23 | protected $requiredFields = []; |
||
24 | |||
25 | protected static $knownHostsFile = '~/.ssh/known_hosts'; // configuration |
||
26 | |||
27 | protected static $acceptUnknownHost = true; // configuration |
||
28 | |||
29 | protected $tunnels = []; |
||
30 | |||
31 | protected $connections = []; |
||
32 | |||
33 | public function openConnection( $host, |
||
68 | |||
69 | public function closeConnection($uid) { |
||
80 | |||
81 | public function getConnection($uid) { |
||
84 | |||
85 | protected function __isValidConnnection($uid) { |
||
96 | |||
97 | protected function __authenticate($connection, $method, ...$args) |
||
114 | |||
115 | protected function __checkFingerprint($connection) |
||
142 | |||
143 | protected function __disconnect() |
||
151 | |||
152 | /** Remote Commands methods **/ |
||
153 | |||
154 | public function runRemoteCommand() |
||
158 | |||
159 | public function seeRemoteOutput() |
||
163 | |||
164 | public function dontSeeRemoteOutput() |
||
168 | |||
169 | /** Remote Files methods **/ |
||
170 | |||
171 | public function seeRemoteFile() |
||
175 | |||
176 | public function dontSeeRemoteFile() |
||
180 | |||
181 | public function grabRemoteFile() |
||
185 | |||
186 | public function copyRemoteFile() |
||
190 | |||
191 | public function deleteRemoteFile() |
||
195 | |||
196 | /** Remote Dir methods **/ |
||
197 | |||
198 | public function seeRemoteDir() |
||
202 | |||
203 | public function dontSeeRemoteDir() |
||
207 | |||
208 | public function copyRemoteDir() |
||
212 | |||
213 | public function deleteRemoteDir() |
||
217 | |||
218 | public function readRemoteDir() |
||
222 | |||
223 | /** Tunnel methods **/ |
||
224 | |||
225 | public function openRemoteTunnel() |
||
229 | |||
230 | public function closeRemoteTunnel() |
||
234 | |||
235 | } |
||
236 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.