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 = ';stored;'; |
||
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 | * Blog Tokens can be "Normal" or "Special". |
||
21 | * * Normal: The result of a normal connection flow. They look like |
||
22 | * "{$random_string_1}.{$random_string_2}" |
||
23 | * Sites only have one Normal Blog Token. Normal Tokens are found in either |
||
24 | * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN |
||
25 | * constant (rare). |
||
26 | * * Special: A connection token for sites that have gone through an alternative |
||
27 | * connection flow. They look like: |
||
28 | * ";{$special_id};.{$random_string}" |
||
29 | * Most sites have zero Special Blog Tokens. Special tokens are only found in the |
||
30 | * JETPACK_BLOG_TOKEN constant. |
||
31 | * |
||
32 | * In particular, note that Normal Blog Tokens never start with ";" and that |
||
33 | * Special Blog Tokens always do. |
||
34 | * |
||
35 | * When searching for a matching Blog Tokens, Blog Tokens are examined in the following |
||
36 | * order: |
||
37 | * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
38 | * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' )) |
||
39 | * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant) |
||
40 | * |
||
41 | * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token. |
||
42 | * @param string|false $token_key If provided, check that the token matches the provided input. |
||
43 | * false : Use first token. Default. |
||
44 | * Jetpack_Data::MAGIC_NORMAL_TOKEN_KEY : Use first Normal Token. |
||
45 | * non-empty string : Use matching token |
||
46 | * @return object|false |
||
47 | */ |
||
48 | public static function get_access_token( $user_id = false, $token_key = false ) { |
||
131 | |||
132 | /** |
||
133 | * This function mirrors Jetpack_Data::is_usable_domain() in the WPCOM codebase. |
||
134 | * |
||
135 | * @param $domain |
||
136 | * @param array $extra |
||
137 | * |
||
138 | * @return bool|WP_Error |
||
139 | */ |
||
140 | public static function is_usable_domain( $domain, $extra = array() ) { |
||
193 | |||
194 | /** |
||
195 | * Returns true if the IP address passed in should not be in a reserved range, even if PHP says that it is. |
||
196 | * See: https://bugs.php.net/bug.php?id=66229 and https://github.com/php/php-src/commit/d1314893fd1325ca6aa0831101896e31135a2658 |
||
197 | * |
||
198 | * This function mirrors Jetpack_Data::php_bug_66229_check() in the WPCOM codebase. |
||
199 | */ |
||
200 | public static function php_bug_66229_check( $ip ) { |
||
217 | } |
||
218 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: