| Conditions | 10 |
| Paths | 16 |
| Total Lines | 34 |
| 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 |
||
| 89 | public function validate_key() { |
||
| 90 | |||
| 91 | // Ensure we don't have garbage before us. |
||
| 92 | ob_clean(); |
||
| 93 | |||
| 94 | // Check if we have a key. |
||
| 95 | if ( ! isset( $_POST['key'] ) ) { |
||
| 96 | wp_send_json_error( 'The key parameter is required.' ); |
||
| 97 | } |
||
| 98 | $response = $this->is_valid( $_POST['key'] ); |
||
| 99 | $res_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
||
| 100 | $url = $res_body['url']; |
||
| 101 | |||
| 102 | //Set a response with valid set to true and messgae according to the key validity with url match |
||
| 103 | if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] && $url == site_url() ) { |
||
| 104 | $is_valid = true; |
||
| 105 | $message = " "; |
||
| 106 | } |
||
| 107 | |||
| 108 | if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] && $url != site_url() ) { |
||
| 109 | $is_valid = false; |
||
| 110 | $message = __( "The key is already used on another site, please contact us at [email protected] to move the key to another site.", 'wordlift' ); |
||
| 111 | Wordlift_Configuration_Service::get_instance()->set_key( '' ); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ( is_wp_error( $response ) || 500 === (int) $response['response']['code'] ) { |
||
| 115 | $is_valid = false; |
||
| 116 | $message = ""; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Set a response with valid set to true or false according to the key validity with message. |
||
| 120 | wp_send_json_success( array( 'valid' => $is_valid, 'message' => $message ) ); |
||
| 121 | |||
| 122 | } |
||
| 123 | |||
| 154 |
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: