| Conditions | 10 |
| Paths | 7 |
| Total Lines | 65 |
| Code Lines | 28 |
| Lines | 9 |
| Ratio | 13.85 % |
| 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 |
||
| 51 | public static function is_usable_domain( $domain, $extra = array() ) { |
||
| 52 | |||
| 53 | // If it's empty, just fail out. |
||
| 54 | if ( ! $domain ) { |
||
| 55 | return new WP_Error( 'fail_domain_empty', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is empty.', 'jetpack' ), $domain ) ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Skips the usuable domain check when connecting a site. |
||
| 60 | * |
||
| 61 | * Allows site administrators with domains that fail gethostname-based checks to pass the request to WP.com |
||
| 62 | * |
||
| 63 | * @since 4.1.0 |
||
| 64 | * |
||
| 65 | * @param bool If the check should be skipped. Default false. |
||
| 66 | */ |
||
| 67 | if ( apply_filters( 'jetpack_skip_usuable_domain_check', false ) ) { |
||
| 68 | return true; |
||
| 69 | } |
||
| 70 | |||
| 71 | // None of the explicit localhosts. |
||
| 72 | $forbidden_domains = array( |
||
| 73 | 'wordpress.com', |
||
| 74 | 'localhost', |
||
| 75 | 'localhost.localdomain', |
||
| 76 | '127.0.0.1', |
||
| 77 | 'local.wordpress.dev', // VVV |
||
| 78 | 'local.wordpress-trunk.dev', // VVV |
||
| 79 | 'src.wordpress-develop.dev', // VVV |
||
| 80 | 'build.wordpress-develop.dev', // VVV |
||
| 81 | ); |
||
| 82 | View Code Duplication | if ( in_array( $domain, $forbidden_domains ) ) { |
|
| 83 | return new WP_Error( 'fail_domain_forbidden', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is in the forbidden array.', 'jetpack' ), $domain ) ); |
||
| 84 | } |
||
| 85 | |||
| 86 | // No .dev or .local domains |
||
| 87 | View Code Duplication | if ( preg_match( '#\.(dev|local)$#i', $domain ) ) { |
|
| 88 | return new WP_Error( 'fail_domain_tld', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it uses an invalid top level domain.', 'jetpack' ), $domain ) ); |
||
| 89 | } |
||
| 90 | |||
| 91 | // No WPCOM subdomains |
||
| 92 | View Code Duplication | if ( preg_match( '#\.wordpress\.com$#i', $domain ) ) { |
|
| 93 | return new WP_Error( 'fail_subdomain_wpcom', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as it is a subdomain of WordPress.com.', 'jetpack' ), $domain ) ); |
||
| 94 | } |
||
| 95 | |||
| 96 | // If PHP was compiled without support for the Filter module (very edge case) |
||
| 97 | if ( ! function_exists( 'filter_var' ) ) { |
||
| 98 | // Just pass back true for now, and let wpcom sort it out. |
||
| 99 | return true; |
||
| 100 | } |
||
| 101 | |||
| 102 | return true; |
||
| 103 | |||
| 104 | // Check the IP to make sure it's pingable. |
||
| 105 | $ip = gethostbyname( $domain ); |
||
|
|
|||
| 106 | |||
| 107 | // Doing this again as I was getting some false positives when gethostbyname() flaked out and returned the domain. |
||
| 108 | $ip = filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ? $ip : gethostbyname( $ip ); |
||
| 109 | |||
| 110 | if ( ! filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_IPV4 ) && ! self::php_bug_66229_check( $ip ) ) { |
||
| 111 | return new WP_Error( 'fail_domain_bad_ip_range', sprintf( __( 'Domain `%1$s` just failed is_usable_domain check as its IP `%2$s` is either invalid, or in a reserved or private range.', 'jetpack' ), $domain, $ip ) ); |
||
| 112 | } |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 141 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.