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