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