Conditions | 17 |
Paths | 292 |
Total Lines | 91 |
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 |
||
141 | public static function json_api( $args = array() ) { |
||
142 | $json_api_args = $args[0]; |
||
143 | $verify_api_user_args = $args[1]; |
||
144 | |||
145 | $method = (string) $json_api_args[0]; |
||
146 | $url = (string) $json_api_args[1]; |
||
147 | $post_body = is_null( $json_api_args[2] ) ? null : (string) $json_api_args[2]; |
||
148 | $user_details = (array) $json_api_args[4]; |
||
149 | $locale = (string) $json_api_args[5]; |
||
150 | |||
151 | if ( ! $verify_api_user_args ) { |
||
152 | $user_id = 0; |
||
153 | } elseif ( 'internal' === $verify_api_user_args[0] ) { |
||
154 | $user_id = (int) $verify_api_user_args[1]; |
||
155 | if ( $user_id ) { |
||
156 | $user = get_user_by( 'id', $user_id ); |
||
157 | if ( ! $user || is_wp_error( $user ) ) { |
||
158 | return false; |
||
159 | } |
||
160 | } |
||
161 | } else { |
||
162 | $user_id = call_user_func( array( new Jetpack_XMLRPC_Server(), 'test_api_user_code' ), $verify_api_user_args ); |
||
163 | if ( ! $user_id ) { |
||
164 | return false; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | if ( 'en' !== $locale ) { |
||
169 | // .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them. |
||
170 | $new_locale = $locale; |
||
171 | if ( strpos( $locale, '-' ) !== false ) { |
||
172 | $locale_pieces = explode( '-', $locale ); |
||
173 | $new_locale = $locale_pieces[0]; |
||
174 | $new_locale .= ( ! empty( $locale_pieces[1] ) ) ? '_' . strtoupper( $locale_pieces[1] ) : ''; |
||
175 | } else { |
||
176 | // .com might pass 'fr' because thats what our language files are named as, where core seems |
||
177 | // to do fr_FR - so try that if we don't think we can load the file. |
||
178 | if ( ! file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) { |
||
179 | $new_locale = $locale . '_' . strtoupper( $locale ); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | if ( file_exists( WP_LANG_DIR . '/' . $new_locale . '.mo' ) ) { |
||
184 | unload_textdomain( 'default' ); |
||
185 | load_textdomain( 'default', WP_LANG_DIR . '/' . $new_locale . '.mo' ); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | $old_user = wp_get_current_user(); |
||
190 | wp_set_current_user( $user_id ); |
||
191 | |||
192 | if ( $user_id ) { |
||
193 | $token_key = false; |
||
194 | } else { |
||
195 | $verified = ( new Connection_Manager() )->verify_xml_rpc_signature(); |
||
196 | $token_key = $verified['token_key']; |
||
197 | } |
||
198 | |||
199 | $token = ( new Tokens() )->get_access_token( $user_id, $token_key ); |
||
200 | if ( ! $token || is_wp_error( $token ) ) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | define( 'REST_API_REQUEST', true ); |
||
205 | define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' ); |
||
206 | |||
207 | // needed? |
||
208 | require_once ABSPATH . 'wp-admin/includes/admin.php'; |
||
209 | |||
210 | require_once JETPACK__PLUGIN_DIR . 'class.json-api.php'; |
||
211 | $api = WPCOM_JSON_API::init( $method, $url, $post_body ); |
||
212 | $api->token_details['user'] = $user_details; |
||
213 | require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php'; |
||
214 | |||
215 | $display_errors = ini_set( 'display_errors', 0 ); // phpcs:ignore WordPress.PHP.IniSet |
||
216 | ob_start(); |
||
217 | $api->serve( false ); |
||
218 | $output = ob_get_clean(); |
||
219 | ini_set( 'display_errors', $display_errors ); // phpcs:ignore WordPress.PHP.IniSet |
||
220 | |||
221 | $nonce = wp_generate_password( 10, false ); |
||
222 | $hmac = hash_hmac( 'md5', $nonce . $output, $token->secret ); |
||
223 | |||
224 | wp_set_current_user( isset( $old_user->ID ) ? $old_user->ID : 0 ); |
||
225 | |||
226 | return array( |
||
227 | (string) $output, |
||
228 | (string) $nonce, |
||
229 | (string) $hmac, |
||
230 | ); |
||
231 | } |
||
232 | } |
||
233 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.