Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 19 | class Guzzle5Adapter implements AdapterInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var Client |
||
| 23 | */ |
||
| 24 | protected $client; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var LoggerInterface |
||
| 28 | */ |
||
| 29 | protected $logger; |
||
| 30 | /** |
||
| 31 | * @param array $options |
||
| 32 | */ |
||
| 33 | public function __construct(array $options = []) |
||
| 55 | |||
| 56 | public function setLogger(LoggerInterface $logger) |
||
| 57 | { |
||
| 58 | $this->logger = $logger; |
||
| 59 | if ($logger instanceof LoggerInterface) { |
||
| 60 | $this->getEmitter()->attach(new LogSubscriber($logger)); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * @internal |
||
| 67 | * @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface |
||
| 68 | */ |
||
| 69 | public function getEmitter() |
||
| 70 | { |
||
| 71 | return $this->client->getEmitter(); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param RequestInterface $request |
||
| 76 | * @return ResponseInterface |
||
| 77 | * @throws \Commercetools\Core\Error\ApiException |
||
| 78 | * @throws \Commercetools\Core\Error\BadGatewayException |
||
| 79 | * @throws \Commercetools\Core\Error\ConcurrentModificationException |
||
| 80 | * @throws \Commercetools\Core\Error\ErrorResponseException |
||
| 81 | * @throws \Commercetools\Core\Error\GatewayTimeoutException |
||
| 82 | * @throws \Commercetools\Core\Error\InternalServerErrorException |
||
| 83 | * @throws \Commercetools\Core\Error\InvalidTokenException |
||
| 84 | * @throws \Commercetools\Core\Error\NotFoundException |
||
| 85 | * @throws \Commercetools\Core\Error\ServiceUnavailableException |
||
| 86 | */ |
||
| 87 | public function execute(RequestInterface $request) |
||
| 88 | { |
||
| 89 | $options = [ |
||
| 90 | 'headers' => $request->getHeaders(), |
||
| 91 | 'body' => (string)$request->getBody() |
||
| 92 | ]; |
||
| 93 | |||
| 94 | try { |
||
| 95 | $guzzleRequest = $this->client->createRequest($request->getMethod(), (string)$request->getUri(), $options); |
||
|
|
|||
| 96 | $guzzleResponse = $this->client->send($guzzleRequest); |
||
| 97 | $response = $this->packResponse($guzzleResponse); |
||
| 98 | } catch (RequestException $exception) { |
||
| 99 | $response = $this->packResponse($exception->getResponse()); |
||
| 100 | throw ApiException::create($request, $response, $exception); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $response; |
||
| 104 | } |
||
| 105 | |||
| 106 | protected function packResponse(\GuzzleHttp\Message\ResponseInterface $response = null) |
||
| 107 | { |
||
| 108 | if (!$response instanceof \GuzzleHttp\Message\ResponseInterface) { |
||
| 109 | return null; |
||
| 110 | } |
||
| 111 | return new Response( |
||
| 112 | $response->getStatusCode(), |
||
| 113 | $response->getHeaders(), |
||
| 114 | (string)$response->getBody() |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param RequestInterface[] $requests |
||
| 120 | * @return \Psr\Http\Message\ResponseInterface[] |
||
| 121 | * @throws \Commercetools\Core\Error\ApiException |
||
| 122 | * @throws \Commercetools\Core\Error\BadGatewayException |
||
| 123 | * @throws \Commercetools\Core\Error\ConcurrentModificationException |
||
| 124 | * @throws \Commercetools\Core\Error\ErrorResponseException |
||
| 125 | * @throws \Commercetools\Core\Error\GatewayTimeoutException |
||
| 126 | * @throws \Commercetools\Core\Error\InternalServerErrorException |
||
| 127 | * @throws \Commercetools\Core\Error\InvalidTokenException |
||
| 128 | * @throws \Commercetools\Core\Error\NotFoundException |
||
| 129 | * @throws \Commercetools\Core\Error\ServiceUnavailableException |
||
| 130 | */ |
||
| 131 | public function executeBatch(array $requests) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | protected function getBatchHttpRequests(array $requests) |
||
| 157 | { |
||
| 158 | $requests = array_map( |
||
| 159 | function ($request) { |
||
| 160 | /** |
||
| 161 | * @var RequestInterface $request |
||
| 162 | */ |
||
| 163 | return $this->client->createRequest( |
||
| 164 | $request->getMethod(), |
||
| 165 | (string)$request->getUri(), |
||
| 166 | ['headers' => $request->getHeaders()] |
||
| 167 | ); |
||
| 168 | }, |
||
| 169 | $requests |
||
| 170 | ); |
||
| 171 | |||
| 172 | return $requests; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param $oauthUri |
||
| 177 | * @param $clientId |
||
| 178 | * @param $clientSecret |
||
| 179 | * @param $formParams |
||
| 180 | * @return ResponseInterface |
||
| 181 | */ |
||
| 182 | public function authenticate($oauthUri, $clientId, $clientSecret, $formParams) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param RequestInterface $request |
||
| 207 | * @return AdapterPromiseInterface |
||
| 208 | */ |
||
| 209 | public function executeAsync(RequestInterface $request) |
||
| 234 | } |
||
| 235 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.