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 | private $output; |
||
34 | |||
35 | public function openConnection( $host, |
||
70 | |||
71 | public function closeConnection($uid) { |
||
82 | |||
83 | public function getConnection($uid) { |
||
86 | |||
87 | protected function __isValidConnnection($uid) { |
||
98 | |||
99 | protected function __authenticate($connection, $method, ...$args) |
||
116 | |||
117 | protected function __checkFingerprint($connection) |
||
144 | |||
145 | protected function __disconnect() |
||
153 | |||
154 | /** Remote Commands methods **/ |
||
155 | |||
156 | public function runRemoteCommand($session, $command) |
||
166 | |||
167 | public function seeInRemoteOutput($text) |
||
171 | |||
172 | public function dontSeeInRemoteOutput($text) |
||
176 | |||
177 | /** Remote Files methods **/ |
||
178 | |||
179 | public function seeRemoteFile() |
||
183 | |||
184 | public function dontSeeRemoteFile() |
||
188 | |||
189 | public function grabRemoteFile() |
||
193 | |||
194 | public function copyRemoteFile() |
||
198 | |||
199 | public function deleteRemoteFile() |
||
203 | |||
204 | /** Remote Dir methods **/ |
||
205 | |||
206 | public function seeRemoteDir() |
||
210 | |||
211 | public function dontSeeRemoteDir() |
||
215 | |||
216 | public function copyRemoteDir() |
||
220 | |||
221 | public function deleteRemoteDir() |
||
225 | |||
226 | public function readRemoteDir() |
||
230 | |||
231 | /** Tunnel methods **/ |
||
232 | |||
233 | public function openRemoteTunnel() |
||
237 | |||
238 | public function closeRemoteTunnel() |
||
242 | |||
243 | } |
||
244 |
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.