| Conditions | 9 |
| Paths | 17 |
| Total Lines | 64 |
| 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 |
||
| 80 | static function admin_page_load() { |
||
| 81 | if ( isset( $_GET['action'] ) ) { |
||
| 82 | if ( isset( $_GET['service'] ) ) { |
||
| 83 | $service_name = $_GET['service']; |
||
| 84 | } |
||
| 85 | |||
| 86 | switch ( $_GET['action'] ) { |
||
| 87 | |||
| 88 | case 'request': |
||
| 89 | check_admin_referer( 'keyring-request', 'kr_nonce' ); |
||
| 90 | check_admin_referer( "keyring-request-$service_name", 'nonce' ); |
||
|
|
|||
| 91 | |||
| 92 | $verification = Jetpack::generate_secrets( 'publicize' ); |
||
| 93 | if ( ! $verification ) { |
||
| 94 | $url = Jetpack::admin_url( 'jetpack#/settings' ); |
||
| 95 | wp_die( sprintf( __( "Jetpack is not connected. Please connect Jetpack by visiting <a href='%s'>Settings</a>.", 'jetpack' ), $url ) ); |
||
| 96 | |||
| 97 | } |
||
| 98 | $stats_options = get_option( 'stats_options' ); |
||
| 99 | $wpcom_blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 100 | $wpcom_blog_id = ! empty( $wpcom_blog_id ) ? $wpcom_blog_id : $stats_options['blog_id']; |
||
| 101 | |||
| 102 | $user = wp_get_current_user(); |
||
| 103 | $redirect = Jetpack_Keyring_Service_Helper::api_url( $service_name, urlencode_deep( array( |
||
| 104 | 'action' => 'request', |
||
| 105 | 'redirect_uri' => add_query_arg( array( 'action' => 'done' ), menu_page_url( 'sharing', false ) ), |
||
| 106 | 'for' => 'publicize', |
||
| 107 | // required flag that says this connection is intended for publicize |
||
| 108 | 'siteurl' => site_url(), |
||
| 109 | 'state' => $user->ID, |
||
| 110 | 'blog_id' => $wpcom_blog_id, |
||
| 111 | 'secret_1' => $verification['secret_1'], |
||
| 112 | 'secret_2' => $verification['secret_2'], |
||
| 113 | 'eol' => $verification['exp'], |
||
| 114 | ) ) ); |
||
| 115 | wp_redirect( $redirect ); |
||
| 116 | exit; |
||
| 117 | break; |
||
| 118 | |||
| 119 | case 'completed': |
||
| 120 | Jetpack::load_xml_rpc_client(); |
||
| 121 | $xml = new Jetpack_IXR_Client(); |
||
| 122 | $xml->query( 'jetpack.fetchPublicizeConnections' ); |
||
| 123 | |||
| 124 | if ( ! $xml->isError() ) { |
||
| 125 | $response = $xml->getResponse(); |
||
| 126 | Jetpack_Options::update_option( 'publicize_connections', $response ); |
||
| 127 | } |
||
| 128 | |||
| 129 | break; |
||
| 130 | |||
| 131 | case 'delete': |
||
| 132 | $id = $_GET['id']; |
||
| 133 | |||
| 134 | check_admin_referer( 'keyring-request', 'kr_nonce' ); |
||
| 135 | check_admin_referer( "keyring-request-$service_name", 'nonce' ); |
||
| 136 | |||
| 137 | Jetpack_Keyring_Service_Helper::disconnect( $service_name, $id ); |
||
| 138 | |||
| 139 | do_action( 'connection_disconnected', $service_name ); |
||
| 140 | break; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 161 |
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: