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:
1 | <?php |
||
3 | class Jetpack_Data { |
||
4 | /** |
||
5 | * Gets locally stored token |
||
6 | * |
||
7 | * @return object|false |
||
8 | */ |
||
9 | public static function get_access_token( $user_id = false ) { |
||
42 | |||
43 | /** |
||
44 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
45 | * |
||
46 | * @param $domain |
||
47 | * @param array $extra |
||
48 | * |
||
49 | * @return bool|WP_Error |
||
50 | */ |
||
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 | return true; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Returns true if the IP address passed in should not be in a reserved range, even if PHP says that it is. |
||
101 | * See: https://bugs.php.net/bug.php?id=66229 and https://github.com/php/php-src/commit/d1314893fd1325ca6aa0831101896e31135a2658 |
||
102 | * |
||
103 | * This function mirrors Jetpack_Data::php_bug_66229_check() in the WPCOM codebase. |
||
104 | */ |
||
105 | public static function php_bug_66229_check( $ip ) { |
||
122 | } |
||
123 |