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