| Conditions | 12 |
| Paths | 1 |
| Total Lines | 75 |
| 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 |
||
| 83 | private function newRetryDecider() { |
||
| 84 | return function ( |
||
| 85 | $retries, |
||
| 86 | Request $request, |
||
| 87 | Response $response = null, |
||
| 88 | TransferException $exception = null |
||
| 89 | ) { |
||
| 90 | // Don't retry if we have run out of retries |
||
| 91 | if ( $retries >= 5 ) { |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | $shouldRetry = false; |
||
| 96 | |||
| 97 | // Retry connection exceptions |
||
| 98 | if ( $exception instanceof ConnectException ) { |
||
| 99 | $shouldRetry = true; |
||
| 100 | } |
||
| 101 | |||
| 102 | if ( $response !== null ) { |
||
| 103 | $data = json_decode( $response->getBody(), true ); |
||
| 104 | |||
| 105 | // Retry on server errors |
||
| 106 | if ( $response->getStatusCode() >= 500 ) { |
||
| 107 | $shouldRetry = true; |
||
| 108 | } |
||
| 109 | |||
| 110 | foreach ( $response->getHeader( 'Mediawiki-Api-Error' ) as $mediawikiApiErrorHeader ) { |
||
| 111 | $RetryAfterResponseHeaderLine = $response->getHeaderLine( 'Retry-After' ); |
||
| 112 | if ( |
||
| 113 | // Retry if the API explicitly tells us to: |
||
| 114 | // https://www.mediawiki.org/wiki/Manual:Maxlag_parameter |
||
| 115 | $RetryAfterResponseHeaderLine |
||
| 116 | || |
||
| 117 | // Retry if we have a response with an API error worth retrying |
||
| 118 | in_array( |
||
| 119 | $mediawikiApiErrorHeader, |
||
| 120 | [ |
||
| 121 | 'ratelimited', |
||
| 122 | 'maxlag', |
||
| 123 | 'readonly', |
||
| 124 | 'internal_api_error_DBQueryError', |
||
| 125 | ] |
||
| 126 | ) |
||
| 127 | || |
||
| 128 | // Or if we have been stopped from saving as an 'anti-abuse measure' |
||
| 129 | // Note: this tries to match "actionthrottledtext" i18n messagae for mediawiki |
||
| 130 | ( |
||
| 131 | $mediawikiApiErrorHeader == 'failed-save' && |
||
| 132 | strstr( $data['error']['info'], 'anti-abuse measure' ) |
||
| 133 | ) |
||
| 134 | ) { |
||
| 135 | $shouldRetry = true; |
||
| 136 | } |
||
| 137 | |||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // Log if we are retrying |
||
| 142 | if ( $shouldRetry ) { |
||
| 143 | $this->logger->warning( |
||
| 144 | sprintf( |
||
| 145 | 'Retrying %s %s %s/5, %s', |
||
| 146 | $request->getMethod(), |
||
| 147 | $request->getUri(), |
||
| 148 | $retries + 1, |
||
| 149 | $response !== null ? 'status code: ' . $response->getStatusCode() : |
||
| 150 | $exception->getMessage() |
||
|
|
|||
| 151 | ) |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | return $shouldRetry; |
||
| 156 | }; |
||
| 157 | } |
||
| 158 | |||
| 160 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: