| Conditions | 13 |
| Paths | 5 |
| Total Lines | 73 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 68 | public function ajaxCheckNow() { |
||
| 69 | //phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is checked in preAjaxRequest(). |
||
| 70 | if ( !isset($_POST['uid']) || ($_POST['uid'] !== $this->updateChecker->getUniqueName('uid')) ) { |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | $this->preAjaxRequest(); |
||
| 74 | $update = $this->updateChecker->checkForUpdates(); |
||
| 75 | if ( $update !== null ) { |
||
| 76 | echo "An update is available:"; |
||
| 77 | //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r -- For debugging output. |
||
| 78 | echo '<pre>', esc_html(print_r($update, true)), '</pre>'; |
||
| 79 | } else { |
||
| 80 | echo 'No updates found.'; |
||
| 81 | } |
||
| 82 | |||
| 83 | $errors = $this->updateChecker->getLastRequestApiErrors(); |
||
| 84 | if ( !empty($errors) ) { |
||
| 85 | printf('<p>The update checker encountered %d API error%s.</p>', count($errors), (count($errors) > 1) ? 's' : ''); |
||
| 86 | |||
| 87 | foreach (array_values($errors) as $num => $item) { |
||
| 88 | $wpError = $item['error']; |
||
| 89 | /** @var \WP_Error $wpError */ |
||
| 90 | printf('<h4>%d) %s</h4>', intval($num + 1), esc_html($wpError->get_error_message())); |
||
| 91 | |||
| 92 | echo '<dl>'; |
||
| 93 | printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code())); |
||
| 94 | |||
| 95 | if ( isset($item['url']) ) { |
||
| 96 | printf('<dt>Requested URL:</dt><dd><code>%s</code></dd>', esc_html($item['url'])); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( isset($item['httpResponse']) ) { |
||
| 100 | if ( is_wp_error($item['httpResponse']) ) { |
||
| 101 | $httpError = $item['httpResponse']; |
||
| 102 | /** @var \WP_Error $httpError */ |
||
| 103 | printf( |
||
| 104 | '<dt>WordPress HTTP API error:</dt><dd>%s (<code>%s</code>)</dd>', |
||
| 105 | esc_html($httpError->get_error_message()), |
||
| 106 | esc_html($httpError->get_error_code()) |
||
| 107 | ); |
||
| 108 | } else { |
||
| 109 | //Status code. |
||
| 110 | printf( |
||
| 111 | '<dt>HTTP status:</dt><dd><code>%d %s</code></dd>', |
||
| 112 | esc_html(wp_remote_retrieve_response_code($item['httpResponse'])), |
||
| 113 | esc_html(wp_remote_retrieve_response_message($item['httpResponse'])) |
||
| 114 | ); |
||
| 115 | |||
| 116 | //Headers. |
||
| 117 | echo '<dt>Response headers:</dt><dd><pre>'; |
||
| 118 | foreach (wp_remote_retrieve_headers($item['httpResponse']) as $name => $value) { |
||
| 119 | printf("%s: %s\n", esc_html($name), esc_html($value)); |
||
| 120 | } |
||
| 121 | echo '</pre></dd>'; |
||
| 122 | |||
| 123 | //Body. |
||
| 124 | $body = wp_remote_retrieve_body($item['httpResponse']); |
||
| 125 | if ( $body === '' ) { |
||
| 126 | $body = '(Empty response.)'; |
||
| 127 | } else if ( strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT ) { |
||
| 128 | $length = strlen($body); |
||
| 129 | $body = substr($body, 0, self::RESPONSE_BODY_LENGTH_LIMIT) |
||
| 130 | . sprintf("\n(Long string truncated. Total length: %d bytes.)", $length); |
||
| 131 | } |
||
| 132 | |||
| 133 | printf('<dt>Response body:</dt><dd><pre>%s</pre></dd>', esc_html($body)); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | echo '<dl>'; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | exit; |
||
| 141 | } |
||
| 200 |
If you suppress an error, we recommend checking for the error condition explicitly: