Conditions | 21 |
Paths | 140 |
Total Lines | 83 |
Lines | 0 |
Ratio | 0 % |
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 |
||
55 | public static function get_access_token( $user_id = false, $token_key = false ) { |
||
56 | $possible_special_tokens = array(); |
||
57 | $possible_normal_tokens = array(); |
||
58 | |||
59 | if ( $user_id ) { |
||
|
|||
60 | if ( !$user_tokens = Jetpack_Options::get_option( 'user_tokens' ) ) { |
||
61 | return false; |
||
62 | } |
||
63 | if ( $user_id === JETPACK_MASTER_USER ) { |
||
64 | if ( !$user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
65 | return false; |
||
66 | } |
||
67 | } |
||
68 | if ( !isset( $user_tokens[$user_id] ) || ! $user_tokens[$user_id] ) { |
||
69 | return false; |
||
70 | } |
||
71 | $user_token_chunks = explode( '.', $user_tokens[$user_id] ); |
||
72 | if ( empty( $user_token_chunks[1] ) || empty( $user_token_chunks[2] ) ) { |
||
73 | return false; |
||
74 | } |
||
75 | if ( $user_id != $user_token_chunks[2] ) { |
||
76 | return false; |
||
77 | } |
||
78 | $possible_normal_tokens[] = "{$user_token_chunks[0]}.{$user_token_chunks[1]}"; |
||
79 | } else { |
||
80 | $stored_blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
81 | if ( $stored_blog_token ) { |
||
82 | $possible_normal_tokens[] = $stored_blog_token; |
||
83 | } |
||
84 | |||
85 | $defined_tokens = Jetpack_Constants::is_defined( 'JETPACK_BLOG_TOKEN' ) |
||
86 | ? explode( ',', Jetpack_Constants::get_constant( 'JETPACK_BLOG_TOKEN' ) ) |
||
87 | : array(); |
||
88 | |||
89 | foreach ( $defined_tokens as $defined_token ) { |
||
90 | if ( ';' === $defined_token[0] ) { |
||
91 | $possible_special_tokens[] = $defined_token; |
||
92 | } else { |
||
93 | $possible_normal_tokens[] = $defined_token; |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) { |
||
99 | $possible_tokens = $possible_normal_tokens; |
||
100 | } else { |
||
101 | $possible_tokens = array_merge( $possible_special_tokens, $possible_normal_tokens ); |
||
102 | } |
||
103 | |||
104 | if ( ! $possible_tokens ) { |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | $valid_token = false; |
||
109 | |||
110 | if ( false === $token_key ) { |
||
111 | // Use first token. |
||
112 | $valid_token = $possible_tokens[0]; |
||
113 | } elseif ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) { |
||
114 | // Use first normal token. |
||
115 | $valid_token = $possible_tokens[0]; // $possible_tokens only contains normal tokens because of earlier check. |
||
116 | } else { |
||
117 | // Use the token matching $token_key or false if none. |
||
118 | // Ensure we check the full key. |
||
119 | $token_check = rtrim( $token_key, '.' ) . '.'; |
||
120 | |||
121 | foreach ( $possible_tokens as $possible_token ) { |
||
122 | if ( hash_equals( substr( $possible_token, 0, strlen( $token_check ) ), $token_check ) ) { |
||
123 | $valid_token = $possible_token; |
||
124 | break; |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | if ( ! $valid_token ) { |
||
130 | return false; |
||
131 | } |
||
132 | |||
133 | return (object) array( |
||
134 | 'secret' => $valid_token, |
||
135 | 'external_user_id' => (int) $user_id, |
||
136 | ); |
||
137 | } |
||
138 | |||
225 |
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: