| Conditions | 21 |
| Paths | > 20000 |
| Total Lines | 243 |
| Lines | 12 |
| Ratio | 4.94 % |
| 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 |
||
| 123 | public static function debug_data() { |
||
| 124 | $debug_info = array(); |
||
| 125 | |||
| 126 | /* Add various important Jetpack options */ |
||
| 127 | $debug_info['site_id'] = array( |
||
| 128 | 'label' => 'Jetpack Site ID', |
||
| 129 | 'value' => Jetpack_Options::get_option( 'id' ), |
||
| 130 | 'private' => false, |
||
| 131 | ); |
||
| 132 | $debug_info['ssl_cert'] = array( |
||
| 133 | 'label' => 'Jetpack SSL Verfication Bypass', |
||
| 134 | 'value' => ( Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) ? 'Yes' : 'No', |
||
| 135 | 'private' => false, |
||
| 136 | ); |
||
| 137 | $debug_info['time_diff'] = array( |
||
| 138 | 'label' => "Offset between Jetpack server's time and this server's time.", |
||
| 139 | 'value' => Jetpack_Options::get_option( 'time_diff' ), |
||
| 140 | 'private' => false, |
||
| 141 | ); |
||
| 142 | $debug_info['version_option'] = array( |
||
| 143 | 'label' => 'Current Jetpack Version Option', |
||
| 144 | 'value' => Jetpack_Options::get_option( 'version' ), |
||
| 145 | 'private' => false, |
||
| 146 | ); |
||
| 147 | $debug_info['old_version'] = array( |
||
| 148 | 'label' => 'Previous Jetpack Version', |
||
| 149 | 'value' => Jetpack_Options::get_option( 'old_version' ), |
||
| 150 | 'private' => false, |
||
| 151 | ); |
||
| 152 | $debug_info['public'] = array( |
||
| 153 | 'label' => 'Jetpack Site Public', |
||
| 154 | 'value' => ( Jetpack_Options::get_option( 'public' ) ) ? 'Public' : 'Private', |
||
| 155 | 'private' => false, |
||
| 156 | ); |
||
| 157 | $debug_info['master_user'] = array( |
||
| 158 | 'label' => 'Jetpack Master User', |
||
| 159 | 'value' => self::human_readable_master_user(), |
||
| 160 | 'private' => false, |
||
| 161 | ); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Token information is private, but awareness if there one is set is helpful. |
||
| 165 | * |
||
| 166 | * To balance out information vs privacy, we only display and include the "key", |
||
| 167 | * which is a segment of the token prior to a period within the token and is |
||
| 168 | * technically not private. |
||
| 169 | * |
||
| 170 | * If a token does not contain a period, then it is malformed and we report it as such. |
||
| 171 | */ |
||
| 172 | $user_id = get_current_user_id(); |
||
| 173 | $user_tokens = Jetpack_Options::get_option( 'user_tokens' ); |
||
| 174 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 175 | $user_token = null; |
||
| 176 | if ( is_array( $user_tokens ) && array_key_exists( $user_id, $user_tokens ) ) { |
||
| 177 | $user_token = $user_tokens[ $user_id ]; |
||
| 178 | } |
||
| 179 | unset( $user_tokens ); |
||
| 180 | |||
| 181 | $tokenset = ''; |
||
| 182 | View Code Duplication | if ( $blog_token ) { |
|
| 183 | $tokenset = 'Blog '; |
||
| 184 | $blog_key = substr( $blog_token, 0, strpos( $blog_token, '.' ) ); |
||
| 185 | // Intentionally not translated since this is helpful when sent to Happiness. |
||
| 186 | $blog_key = ( $blog_key ) ? $blog_key : 'Potentially Malformed Token.'; |
||
| 187 | } |
||
| 188 | View Code Duplication | if ( $user_token ) { |
|
| 189 | $tokenset .= 'User'; |
||
| 190 | $user_key = substr( $user_token, 0, strpos( $user_token, '.' ) ); |
||
| 191 | // Intentionally not translated since this is helpful when sent to Happiness. |
||
| 192 | $user_key = ( $user_key ) ? $user_key : 'Potentially Malformed Token.'; |
||
| 193 | } |
||
| 194 | if ( ! $tokenset ) { |
||
| 195 | $tokenset = 'None'; |
||
| 196 | } |
||
| 197 | |||
| 198 | $debug_info['current_user'] = array( |
||
| 199 | 'label' => 'Current User', |
||
| 200 | 'value' => self::human_readable_user( $user_id ), |
||
| 201 | 'private' => false, |
||
| 202 | ); |
||
| 203 | $debug_info['tokens_set'] = array( |
||
| 204 | 'label' => 'Tokens defined', |
||
| 205 | 'value' => $tokenset, |
||
| 206 | 'private' => false, |
||
| 207 | ); |
||
| 208 | $debug_info['blog_token'] = array( |
||
| 209 | 'label' => 'Blog Public Key', |
||
| 210 | 'value' => ( $blog_token ) ? $blog_key : 'Not set.', |
||
|
|
|||
| 211 | 'private' => false, |
||
| 212 | ); |
||
| 213 | $debug_info['user_token'] = array( |
||
| 214 | 'label' => 'User Public Key', |
||
| 215 | 'value' => ( $user_token ) ? $user_key : 'Not set.', |
||
| 216 | 'private' => false, |
||
| 217 | ); |
||
| 218 | |||
| 219 | /** Jetpack Environmental Information */ |
||
| 220 | $debug_info['version'] = array( |
||
| 221 | 'label' => 'Jetpack Version', |
||
| 222 | 'value' => JETPACK__VERSION, |
||
| 223 | 'private' => false, |
||
| 224 | ); |
||
| 225 | $debug_info['jp_plugin_dir'] = array( |
||
| 226 | 'label' => 'Jetpack Directory', |
||
| 227 | 'value' => JETPACK__PLUGIN_DIR, |
||
| 228 | 'private' => false, |
||
| 229 | ); |
||
| 230 | $debug_info['plan'] = array( |
||
| 231 | 'label' => 'Plan Type', |
||
| 232 | 'value' => self::what_jetpack_plan(), |
||
| 233 | 'private' => false, |
||
| 234 | ); |
||
| 235 | |||
| 236 | foreach ( array( |
||
| 237 | 'HTTP_HOST', |
||
| 238 | 'SERVER_PORT', |
||
| 239 | 'HTTPS', |
||
| 240 | 'GD_PHP_HANDLER', |
||
| 241 | 'HTTP_AKAMAI_ORIGIN_HOP', |
||
| 242 | 'HTTP_CF_CONNECTING_IP', |
||
| 243 | 'HTTP_CLIENT_IP', |
||
| 244 | 'HTTP_FASTLY_CLIENT_IP', |
||
| 245 | 'HTTP_FORWARDED', |
||
| 246 | 'HTTP_FORWARDED_FOR', |
||
| 247 | 'HTTP_INCAP_CLIENT_IP', |
||
| 248 | 'HTTP_TRUE_CLIENT_IP', |
||
| 249 | 'HTTP_X_CLIENTIP', |
||
| 250 | 'HTTP_X_CLUSTER_CLIENT_IP', |
||
| 251 | 'HTTP_X_FORWARDED', |
||
| 252 | 'HTTP_X_FORWARDED_FOR', |
||
| 253 | 'HTTP_X_IP_TRAIL', |
||
| 254 | 'HTTP_X_REAL_IP', |
||
| 255 | 'HTTP_X_VARNISH', |
||
| 256 | 'REMOTE_ADDR', |
||
| 257 | ) as $header ) { |
||
| 258 | if ( isset( $_SERVER[ $header ] ) ) { |
||
| 259 | $debug_info[ $header ] = array( |
||
| 260 | 'label' => 'Server Variable ' . $header, |
||
| 261 | 'value' => ( $_SERVER[ $header ] ) ? $_SERVER[ $header ] : 'false', |
||
| 262 | 'private' => false, |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | $debug_info['protect_header'] = array( |
||
| 268 | 'label' => 'Trusted IP', |
||
| 269 | 'value' => wp_json_encode( get_site_option( 'trusted_ip_header' ) ), |
||
| 270 | 'private' => false, |
||
| 271 | ); |
||
| 272 | |||
| 273 | /** Sync Debug Information */ |
||
| 274 | /** Load Sync modules */ |
||
| 275 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php'; |
||
| 276 | /** Load Sync sender */ |
||
| 277 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; |
||
| 278 | /** Load Sync functions */ |
||
| 279 | require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; |
||
| 280 | |||
| 281 | $sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); |
||
| 282 | if ( $sync_module ) { |
||
| 283 | $sync_statuses = $sync_module->get_status(); |
||
| 284 | $human_readable_sync_status = array(); |
||
| 285 | foreach ( $sync_statuses as $sync_status => $sync_status_value ) { |
||
| 286 | $human_readable_sync_status[ $sync_status ] = |
||
| 287 | in_array( $sync_status, array( 'started', 'queue_finished', 'send_started', 'finished' ), true ) |
||
| 288 | ? date( 'r', $sync_status_value ) : $sync_status_value; |
||
| 289 | } |
||
| 290 | $debug_info['full_sync'] = array( |
||
| 291 | 'label' => 'Full Sync Status', |
||
| 292 | 'value' => wp_json_encode( $human_readable_sync_status ), |
||
| 293 | 'private' => false, |
||
| 294 | ); |
||
| 295 | } |
||
| 296 | |||
| 297 | $queue = Jetpack_Sync_Sender::get_instance()->get_sync_queue(); |
||
| 298 | |||
| 299 | $debug_info['sync_size'] = array( |
||
| 300 | 'label' => 'Sync Queue Size', |
||
| 301 | 'value' => $queue->size(), |
||
| 302 | 'private' => false, |
||
| 303 | ); |
||
| 304 | $debug_info['sync_lag'] = array( |
||
| 305 | 'label' => 'Sync Queue Lag', |
||
| 306 | 'value' => self::seconds_to_time( $queue->lag() ), |
||
| 307 | 'private' => false, |
||
| 308 | ); |
||
| 309 | |||
| 310 | $full_sync_queue = Jetpack_Sync_Sender::get_instance()->get_full_sync_queue(); |
||
| 311 | |||
| 312 | $debug_info['full_sync_size'] = array( |
||
| 313 | 'label' => 'Full Sync Queue Size', |
||
| 314 | 'value' => $full_sync_queue->size(), |
||
| 315 | 'private' => false, |
||
| 316 | ); |
||
| 317 | $debug_info['full_sync_lag'] = array( |
||
| 318 | 'label' => 'Full Sync Queue Lag', |
||
| 319 | 'value' => self::seconds_to_time( $full_sync_queue->lag() ), |
||
| 320 | 'private' => false, |
||
| 321 | ); |
||
| 322 | |||
| 323 | /** |
||
| 324 | * IDC Information |
||
| 325 | * |
||
| 326 | * Must follow sync debug since it depends on sync functionality. |
||
| 327 | */ |
||
| 328 | $idc_urls = array( |
||
| 329 | 'home' => Jetpack_Sync_Functions::home_url(), |
||
| 330 | 'siteurl' => Jetpack_Sync_Functions::site_url(), |
||
| 331 | 'WP_HOME' => Jetpack_Constants::is_defined( 'WP_HOME' ) ? Jetpack_Constants::get_constant( 'WP_HOME' ) : '', |
||
| 332 | 'WP_SITEURL' => Jetpack_Constants::is_defined( 'WP_SITEURL' ) ? Jetpack_Constants::get_constant( 'WP_SITEURL' ) : '', |
||
| 333 | ); |
||
| 334 | |||
| 335 | $debug_info['idc_urls'] = array( |
||
| 336 | 'label' => 'IDC URLs', |
||
| 337 | 'value' => wp_json_encode( $idc_urls ), |
||
| 338 | 'private' => false, |
||
| 339 | ); |
||
| 340 | $debug_info['idc_error_option'] = array( |
||
| 341 | 'label' => 'IDC Error Option', |
||
| 342 | 'value' => wp_json_encode( Jetpack_Options::get_option( 'sync_error_idc' ) ), |
||
| 343 | 'private' => false, |
||
| 344 | ); |
||
| 345 | $debug_info['idc_optin'] = array( |
||
| 346 | 'label' => 'IDC Opt-in', |
||
| 347 | 'value' => Jetpack::sync_idc_optin(), |
||
| 348 | 'private' => false, |
||
| 349 | ); |
||
| 350 | |||
| 351 | // @todo -- Add testing results? |
||
| 352 | $cxn_tests = new Jetpack_Cxn_Tests(); |
||
| 353 | $debug_info['cxn_tests'] = array( |
||
| 354 | 'label' => 'Connection Tests', |
||
| 355 | 'value' => '', |
||
| 356 | 'private' => false, |
||
| 357 | ); |
||
| 358 | if ( $cxn_tests->pass() ) { |
||
| 359 | $debug_info['cxn_tests']['value'] = 'All Pass.'; |
||
| 360 | } else { |
||
| 361 | $debug_info['cxn_tests']['value'] = wp_json_encode( $cxn_tests->list_fails() ); |
||
| 362 | } |
||
| 363 | |||
| 364 | return $debug_info; |
||
| 365 | } |
||
| 366 | |||
| 401 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: