| Conditions | 23 |
| Paths | 10368 |
| Total Lines | 49 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 85 | private function get_ftp_creds( $type ) { |
||
| 86 | $credentials = get_option( 'ftp_credentials', array( 'hostname' => '', 'username' => '' ) ); |
||
| 87 | |||
| 88 | $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : $credentials['hostname']; |
||
| 89 | $credentials['username'] = defined('FTP_USER') ? FTP_USER : $credentials['username']; |
||
| 90 | $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : ''; |
||
| 91 | |||
| 92 | // Check to see if we are setting the public/private keys for ssh |
||
| 93 | $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : ''; |
||
| 94 | $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : ''; |
||
| 95 | |||
| 96 | // Sanitize the hostname, Some people might pass in odd-data: |
||
| 97 | $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); //Strip any schemes off |
||
| 98 | |||
| 99 | if ( strpos( $credentials['hostname'], ':' ) ) { |
||
| 100 | list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); |
||
| 101 | if ( ! is_numeric( $credentials['port'] ) ) { |
||
| 102 | unset( $credentials['port'] ); |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | unset( $credentials['port'] ); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) { |
||
| 109 | $credentials['connection_type'] = 'ssh'; |
||
| 110 | } else if ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { |
||
| 111 | //Only the FTP Extension understands SSL |
||
| 112 | $credentials['connection_type'] = 'ftps'; |
||
| 113 | } else if ( ! isset( $credentials['connection_type'] ) ) { |
||
| 114 | //All else fails (And it's not defaulted to something else saved), Default to FTP |
||
| 115 | $credentials['connection_type'] = 'ftp'; |
||
| 116 | } |
||
| 117 | |||
| 118 | $has_creds = ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) ); |
||
| 119 | $can_ssh = ( 'ssh' == $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) ); |
||
| 120 | if ( $has_creds || $can_ssh ) { |
||
| 121 | $stored_credentials = $credentials; |
||
| 122 | if ( ! empty( $stored_credentials['port'] ) ) { |
||
| 123 | //save port as part of hostname to simplify above code. |
||
| 124 | $stored_credentials['hostname'] .= ':' . $stored_credentials['port']; |
||
| 125 | } |
||
| 126 | |||
| 127 | unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] ); |
||
| 128 | |||
| 129 | return $credentials; |
||
| 130 | } |
||
| 131 | |||
| 132 | return false; |
||
| 133 | } |
||
| 134 | |||
| 141 |