| Conditions | 12 |
| Paths | 170 |
| Total Lines | 80 |
| Code Lines | 52 |
| Lines | 10 |
| Ratio | 12.5 % |
| 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 |
||
| 149 | protected function run($argument) { |
||
| 150 | $target = $argument['url']; |
||
| 151 | $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
||
| 152 | $currentTime = $this->timeFactory->getTime(); |
||
| 153 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
||
| 154 | $source = rtrim($source, '/'); |
||
| 155 | $token = $argument['token']; |
||
| 156 | |||
| 157 | // kill job after 30 days of trying |
||
| 158 | $deadline = $currentTime - $this->maxLifespan; |
||
| 159 | View Code Duplication | if ($created < $deadline) { |
|
| 160 | $this->retainJob = false; |
||
| 161 | $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
||
| 162 | return; |
||
| 163 | } |
||
| 164 | |||
| 165 | $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
||
| 166 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
||
| 167 | |||
| 168 | // make sure that we have a well formatted url |
||
| 169 | $url = rtrim($target, '/') . '/' . trim($endPoint, '/') . $this->format; |
||
| 170 | |||
| 171 | $result = null; |
||
| 172 | try { |
||
| 173 | $result = $this->httpClient->get( |
||
| 174 | $url, |
||
| 175 | [ |
||
| 176 | 'query' => |
||
| 177 | [ |
||
| 178 | 'url' => $source, |
||
| 179 | 'token' => $token |
||
| 180 | ], |
||
| 181 | 'timeout' => 3, |
||
| 182 | 'connect_timeout' => 3, |
||
| 183 | ] |
||
| 184 | ); |
||
| 185 | |||
| 186 | $status = $result->getStatusCode(); |
||
| 187 | |||
| 188 | } catch (ClientException $e) { |
||
| 189 | $status = $e->getCode(); |
||
| 190 | View Code Duplication | if ($status === Http::STATUS_FORBIDDEN) { |
|
| 191 | $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
||
| 192 | } else { |
||
| 193 | $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
||
| 194 | } |
||
| 195 | } catch (\Exception $e) { |
||
| 196 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
||
| 197 | $this->logger->logException($e, ['app' => 'federation']); |
||
| 198 | } |
||
| 199 | |||
| 200 | // if we received a unexpected response we try again later |
||
| 201 | if ( |
||
| 202 | $status !== Http::STATUS_OK |
||
| 203 | && $status !== Http::STATUS_FORBIDDEN |
||
| 204 | ) { |
||
| 205 | $this->retainJob = true; |
||
| 206 | } else { |
||
| 207 | // reset token if we received a valid response |
||
| 208 | $this->dbHandler->addToken($target, ''); |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
||
| 212 | $body = $result->getBody(); |
||
| 213 | $result = json_decode($body, true); |
||
| 214 | if (isset($result['ocs']['data']['sharedSecret'])) { |
||
| 215 | $this->trustedServers->addSharedSecret( |
||
| 216 | $target, |
||
| 217 | $result['ocs']['data']['sharedSecret'] |
||
| 218 | ); |
||
| 219 | } else { |
||
| 220 | $this->logger->error( |
||
| 221 | 'remote server "' . $target . '"" does not return a valid shared secret', |
||
| 222 | ['app' => 'federation'] |
||
| 223 | ); |
||
| 224 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | } |
||
| 229 | |||
| 249 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.