Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
12 | class SecureShell extends Module |
||
13 | { |
||
14 | |||
15 | const DEFAULT_PORT = 22; |
||
16 | const AUTH_PASSWORD = 1; |
||
17 | const AUTH_PUBKEY = 2; |
||
18 | const AUTH_HOSTKEY = 3; |
||
19 | const AUTH_AGENT = 4; |
||
20 | const AUTH_NONE = 0; |
||
21 | |||
22 | protected $config = ['StrictHostKeyChecking', 'KnownHostsFile']; |
||
23 | |||
24 | protected $requiredFields = []; |
||
25 | |||
26 | /** |
||
27 | * @var string $knownHostsFile |
||
28 | */ |
||
29 | protected static $knownHostsFile = ''; |
||
30 | |||
31 | /** |
||
32 | * @var bool $strictHostKeyChecking |
||
33 | */ |
||
34 | protected static $strictHostKeyChecking = false; |
||
35 | |||
36 | /** |
||
37 | * @var mixed|null $connection |
||
38 | */ |
||
39 | protected $connection; |
||
40 | |||
41 | /** |
||
42 | * @var string[] $output |
||
43 | */ |
||
44 | private $output; |
||
45 | |||
46 | /** |
||
47 | * @codeCoverageIgnore |
||
48 | * @ignore Codeception specific |
||
49 | */ |
||
50 | public function _initialize() |
||
66 | |||
67 | /** |
||
68 | * Open an SSH connection to the host |
||
69 | * |
||
70 | * @param string $host |
||
71 | * @param int $port |
||
72 | * @param int $auth |
||
73 | * @param mixed ...$args |
||
74 | * |
||
75 | * @return resource|null Returns the SSH connection |
||
76 | */ |
||
77 | public function openConnection($host, |
||
104 | |||
105 | /** |
||
106 | * Close current SSH connection |
||
107 | * |
||
108 | * @return boolean Return true once executed |
||
109 | */ |
||
110 | public function closeConnection() { |
||
114 | |||
115 | /** |
||
116 | * Get current SSH connection |
||
117 | * |
||
118 | * @return resource|null Return current SSH connection |
||
119 | */ |
||
120 | public function getConnection() { |
||
123 | |||
124 | /** |
||
125 | * Run SSH authentication based on the selected method |
||
126 | * |
||
127 | * @param resource $connection |
||
128 | * @param int $method |
||
129 | * @param mixed ...$args |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | protected function __authenticate($connection, $method, ...$args) |
||
150 | |||
151 | /** |
||
152 | * Check if the SSH server public key fingerprint is valid |
||
153 | * |
||
154 | * @param resource $connection |
||
155 | * |
||
156 | * @return string Server public key fingerprint |
||
157 | */ |
||
158 | protected function __checkFingerprint($connection) |
||
188 | |||
189 | /** |
||
190 | * Run a command on the remote host and return the output |
||
191 | * |
||
192 | * @param string $command |
||
193 | * |
||
194 | * @return string[] Return command output 'STDOUT' and 'STDERR' |
||
195 | */ |
||
196 | public function runRemoteCommand($command) |
||
209 | |||
210 | /** |
||
211 | * Verify that the last remote command output contains a specific text |
||
212 | * |
||
213 | * @param string $text |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | public function seeInRemoteOutput($text) |
||
221 | |||
222 | /** |
||
223 | * Verify that the last remote command output does not contain a specific text |
||
224 | * |
||
225 | * @param string $text |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | public function dontSeeInRemoteOutput($text) |
||
233 | |||
234 | |||
235 | /** |
||
236 | * Verify that a file exists in the current remote folder |
||
237 | * |
||
238 | * @param string $filename |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | View Code Duplication | public function seeRemoteFile($filename) |
|
252 | |||
253 | /** |
||
254 | * Verify that a file does not exist in the current remote folder |
||
255 | * |
||
256 | * @param string $filename |
||
257 | * |
||
258 | * @return void |
||
259 | */ |
||
260 | View Code Duplication | public function dontSeeRemoteFile($filename) |
|
270 | |||
271 | /** |
||
272 | * Get the content of a remote file over SFTP |
||
273 | * |
||
274 | * @param string $filename |
||
275 | * |
||
276 | * @return string File content |
||
277 | */ |
||
278 | public function grabRemoteFile($filename) |
||
287 | |||
288 | /** |
||
289 | * Verify that a remote folder exists in the current remote path |
||
290 | * |
||
291 | * @param string $dirname |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | View Code Duplication | public function seeRemoteDir($dirname) |
|
304 | |||
305 | /** |
||
306 | * Verify that a remote folder does not exist in the current remote path |
||
307 | * |
||
308 | * @param string $dirname |
||
309 | * |
||
310 | * @return void |
||
311 | */ |
||
312 | View Code Duplication | public function dontSeeRemoteDir($dirname) |
|
321 | |||
322 | /** |
||
323 | * Get the remode folder content |
||
324 | * |
||
325 | * @param string $dirname |
||
326 | * |
||
327 | * @return string[] Array of file names and folder names |
||
328 | */ |
||
329 | public function grabRemoteDir($dirname) |
||
340 | |||
341 | } |
||
342 |