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 | * Used internally when we want to look for the Normal Blog Token |
||
| 6 | * without knowing its token key ahead of time. |
||
| 7 | */ |
||
| 8 | const MAGIC_NORMAL_TOKEN_KEY = ';normal;'; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Gets the requested token. |
||
| 12 | * |
||
| 13 | * Tokens are one of two types: |
||
| 14 | * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token, |
||
| 15 | * though some sites can have multiple "Special" Blog Tokens (see below). These tokens |
||
| 16 | * are not associated with a user account. They represent the site's connection with |
||
| 17 | * the Jetpack servers. |
||
| 18 | * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token. |
||
| 19 | * |
||
| 20 | * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the |
||
| 21 | * token, and $private is a secret that should never be displayed anywhere or sent |
||
| 22 | * over the network; it's used only for signing things. |
||
| 23 | * |
||
| 24 | * Blog Tokens can be "Normal" or "Special". |
||
| 25 | * * Normal: The result of a normal connection flow. They look like |
||
| 26 | * "{$random_string_1}.{$random_string_2}" |
||
| 27 | * That is, $token_key and $private are both random strings. |
||
| 28 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
| 29 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
| 30 | * constant (rare). |
||
| 31 | * * Special: A connection token for sites that have gone through an alternative |
||
| 32 | * connection flow. They look like: |
||
| 33 | * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}" |
||
| 34 | * That is, $private is a random string and $token_key has a special structure with |
||
| 35 | * lots of semicolons. |
||
| 36 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
| 37 | * JETPACK_BLOG_TOKEN constant. |
||
| 38 | * |
||
| 39 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
| 40 | * Special Blog Tokens always do. |
||
| 41 | * |
||
| 42 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
| 43 | * order: |
||
| 44 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 45 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
| 46 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
| 47 | * |
||
| 48 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
| 49 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
| 50 | * false : Use first token. Default. |
||
| 51 | * Jetpack_Data::MAGIC_NORMAL_TOKEN_KEY : Use first Normal Token. |
||
| 52 | * non-empty string : Use matching token |
||
| 53 | * @return object|false |
||
| 54 | */ |
||
| 55 | public static function get_access_token( $user_id = false, $token_key = false ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
| 141 | * |
||
| 142 | * @param $domain |
||
| 143 | * @param array $extra |
||
| 144 | * |
||
| 145 | * @return bool|WP_Error |
||
| 146 | */ |
||
| 147 | public static function is_usable_domain( $domain, $extra = array() ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns true if the IP address passed in should not be in a reserved range, even if PHP says that it is. |
||
| 203 | * See: https://bugs.php.net/bug.php?id=66229 and https://github.com/php/php-src/commit/d1314893fd1325ca6aa0831101896e31135a2658 |
||
| 204 | * |
||
| 205 | * This function mirrors Jetpack_Data::php_bug_66229_check() in the WPCOM codebase. |
||
| 206 | */ |
||
| 207 | public static function php_bug_66229_check( $ip ) { |
||
| 224 | } |
||
| 225 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: