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 ) { |
||
| 10 | if ( $user_id ) { |
||
| 11 | if ( !$tokens = Jetpack_Options::get_option( 'user_tokens' ) ) { |
||
| 12 | return false; |
||
| 13 | } |
||
| 14 | if ( $user_id === JETPACK_MASTER_USER ) { |
||
| 15 | if ( !$user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
| 16 | return false; |
||
| 17 | } |
||
| 18 | } |
||
| 19 | if ( !isset( $tokens[$user_id] ) || !$token = $tokens[$user_id] ) { |
||
| 20 | return false; |
||
| 21 | } |
||
| 22 | $token_chunks = explode( '.', $token ); |
||
| 23 | if ( empty( $token_chunks[1] ) || empty( $token_chunks[2] ) ) { |
||
| 24 | return false; |
||
| 25 | } |
||
| 26 | if ( $user_id != $token_chunks[2] ) { |
||
| 27 | return false; |
||
| 28 | } |
||
| 29 | $token = "{$token_chunks[0]}.{$token_chunks[1]}"; |
||
| 30 | } else { |
||
| 31 | $token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 32 | if ( empty( $token ) ) { |
||
| 33 | return false; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | return (object) array( |
||
| 38 | 'secret' => $token, |
||
| 39 | 'external_user_id' => (int) $user_id, |
||
| 40 | ); |
||
| 41 | } |
||
| 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() ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns true if the IP address passed in should not be in a reserved range, even if PHP says that it is. |
||
| 104 | * See: https://bugs.php.net/bug.php?id=66229 and https://github.com/php/php-src/commit/d1314893fd1325ca6aa0831101896e31135a2658 |
||
| 105 | * |
||
| 106 | * This function mirrors Jetpack_Data::php_bug_66229_check() in the WPCOM codebase. |
||
| 107 | */ |
||
| 108 | public static function php_bug_66229_check( $ip ) { |
||
| 125 | } |
||
| 126 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.