| Conditions | 23 |
| Paths | 10368 |
| Total Lines | 51 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 169 | private function get_ftp_creds( $type ) { |
||
| 170 | $credentials = get_option( 'ftp_credentials', array( |
||
| 171 | 'hostname' => '', |
||
| 172 | 'username' => '', |
||
| 173 | ) ); |
||
| 174 | |||
| 175 | $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : $credentials['hostname']; |
||
| 176 | $credentials['username'] = defined('FTP_USER') ? FTP_USER : $credentials['username']; |
||
| 177 | $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : ''; |
||
| 178 | |||
| 179 | // Check to see if we are setting the public/private keys for ssh |
||
| 180 | $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : ''; |
||
| 181 | $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : ''; |
||
| 182 | |||
| 183 | // Sanitize the hostname, Some people might pass in odd-data: |
||
| 184 | $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); //Strip any schemes off |
||
| 185 | |||
| 186 | if ( strpos( $credentials['hostname'], ':' ) ) { |
||
| 187 | list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); |
||
| 188 | if ( ! is_numeric( $credentials['port'] ) ) { |
||
| 189 | unset( $credentials['port'] ); |
||
| 190 | } |
||
| 191 | } else { |
||
| 192 | unset( $credentials['port'] ); |
||
| 193 | } |
||
| 194 | |||
| 195 | if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) { |
||
| 196 | $credentials['connection_type'] = 'ssh'; |
||
| 197 | } else if ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { |
||
| 198 | //Only the FTP Extension understands SSL |
||
| 199 | $credentials['connection_type'] = 'ftps'; |
||
| 200 | } else if ( ! isset( $credentials['connection_type'] ) ) { |
||
| 201 | //All else fails (And it's not defaulted to something else saved), Default to FTP |
||
| 202 | $credentials['connection_type'] = 'ftp'; |
||
| 203 | } |
||
| 204 | |||
| 205 | $has_creds = ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) ); |
||
| 206 | $can_ssh = ( 'ssh' == $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) ); |
||
| 207 | if ( $has_creds || $can_ssh ) { |
||
| 208 | $stored_credentials = $credentials; |
||
| 209 | if ( ! empty( $stored_credentials['port'] ) ) { |
||
| 210 | //save port as part of hostname to simplify above code. |
||
| 211 | $stored_credentials['hostname'] .= ':' . $stored_credentials['port']; |
||
| 212 | } |
||
| 213 | |||
| 214 | unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] ); |
||
| 215 | |||
| 216 | return $credentials; |
||
| 217 | } |
||
| 218 | |||
| 219 | return false; |
||
| 220 | } |
||
| 228 |