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 |
||
| 8 | class SecureShell extends \Codeception\Platform\Extension |
||
| 9 | { |
||
| 10 | |||
| 11 | const DEFAULT_PORT = 22; |
||
| 12 | const AUTH_PASSWORD = 1; |
||
| 13 | const AUTH_PUBKEY = 2; |
||
| 14 | const AUTH_HOSTKEY = 3; |
||
| 15 | const AUTH_AGENT = 4; |
||
| 16 | const AUTH_NONE = 0; |
||
| 17 | |||
| 18 | // list events to listen to |
||
| 19 | public static $events = []; |
||
| 20 | |||
| 21 | protected static $knownHostsFile = '~/.ssh/known_hosts'; |
||
| 22 | |||
| 23 | protected static $tunnels = []; |
||
| 24 | |||
| 25 | protected static $connections = []; |
||
| 26 | |||
| 27 | public function openConnection($host, |
||
| 53 | |||
| 54 | public function closeConnection($uid) { |
||
| 64 | |||
| 65 | protected function __isValidConnnection($uid) { |
||
| 66 | if (isset($this->connections[$uid])) { |
||
| 67 | if (is_resource($this->connections[$uid]['resource'])) { |
||
| 68 | return 1; |
||
| 69 | } else { |
||
| 70 | return 0; |
||
| 71 | } |
||
| 72 | } else { |
||
| 73 | return -1; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | protected function __authenticate($connection, $method, ...$args) |
||
| 78 | { |
||
| 79 | switch ($method) { |
||
| 80 | case SecureShell::AUTH_PASSWORD: |
||
| 81 | return ssh2_auth_password($connection, ...$args); |
||
| 82 | case SecureShell::AUTH_PUBKEY: |
||
| 83 | return ssh2_auth_pubkey_file($connection, ...$args); |
||
| 84 | case SecureShell::AUTH_HOSTKEY: |
||
| 85 | return ssh2_auth_hostbased_file($connection, ...$args); |
||
| 86 | case SecureShell::AUTH_AGENT: |
||
| 87 | return ssh2_auth_agent($connection, ...$args); |
||
| 88 | case SecureShell::AUTH_NONE: |
||
| 89 | return ssh2_auth_none($connection, ...$args); |
||
| 90 | default: |
||
| 91 | throw new ModuleException('Unsupported authentication method'); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function __checkFingerprint($connection, $acceptUnknown = false) |
||
| 96 | { |
||
| 97 | $fingerprint = ssh2_fingerprint($connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX); |
||
| 98 | $knownHost = false; |
||
| 99 | try { |
||
| 100 | $file = new SplFileObject($this->knownHostsFile); |
||
| 101 | $file->setFlags(SplFileObject::READ_CSV); |
||
| 102 | $file->setCsvControl(' '); |
||
| 103 | foreach ($file as $entry) { |
||
| 104 | list($host, $method, $fp) = $entry; |
||
| 105 | $knownHost = (strcmp($fp, $fingerprint) !== 0); |
||
| 106 | if ($knownHost === true) { |
||
| 107 | break; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $knownHost = $knownHost || $acceptUnknown; |
||
| 111 | |||
| 112 | if ($knownHost === false) { |
||
| 113 | throw new ModuleException('Unable to verify server identity!'); |
||
| 114 | } |
||
| 115 | } catch (RuntimeException $e) { |
||
| 116 | if ($acceptUnknown === false) { |
||
| 117 | throw new ModuleException('Unable to verify server identity!'); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | protected function __disconnect() |
||
| 124 | { |
||
| 125 | foreach ($this->connections as $id => $connection) { |
||
| 126 | if (is_resource($connection['resource']) !== true) { |
||
| 127 | unset($this->connections[$id]); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** Remote Commands methods **/ |
||
| 133 | |||
| 134 | public function runRemoteCommand() |
||
| 138 | |||
| 139 | public function seeRemoteOutput() |
||
| 140 | { |
||
| 141 | |||
| 142 | } |
||
| 143 | |||
| 144 | public function dontSeeRemoteOutput() |
||
| 148 | |||
| 149 | /** Remote Files methods **/ |
||
| 150 | |||
| 151 | public function seeRemoteFile() |
||
| 155 | |||
| 156 | public function dontSeeRemoteFile() |
||
| 160 | |||
| 161 | public function grabRemoteFile() |
||
| 165 | |||
| 166 | public function copyRemoteFile() |
||
| 170 | |||
| 171 | public function deleteRemoteFile() |
||
| 175 | |||
| 176 | /** Remote Dir methods **/ |
||
| 177 | |||
| 178 | public function seeRemoteDir() |
||
| 182 | |||
| 183 | public function dontSeeRemoteDir() |
||
| 187 | |||
| 188 | public function copyRemoteDir() |
||
| 192 | |||
| 193 | public function deleteRemoteDir() |
||
| 197 | |||
| 198 | public function readRemoteDir() |
||
| 202 | |||
| 203 | /** Tunnel methods **/ |
||
| 204 | |||
| 205 | public function openRemoteTunnel() |
||
| 209 | |||
| 210 | public function closeRemoteTunnel() |
||
| 214 | |||
| 215 | } |
||
| 216 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.