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 | /** |
||
| 32 | * @param array $options |
||
| 33 | */ |
||
| 34 | public function __construct(array $options = []) |
||
| 35 | { |
||
| 36 | if (isset($options['base_uri'])) { |
||
| 37 | $options['base_url'] = $options['base_uri']; |
||
| 38 | unset($options['base_uri']); |
||
| 39 | } |
||
| 40 | if (isset($options['headers'])) { |
||
| 41 | $options['defaults']['headers'] = $options['headers']; |
||
| 42 | unset($options['headers']); |
||
| 43 | } |
||
| 44 | $options = array_merge( |
||
| 45 | [ |
||
| 46 | 'allow_redirects' => false, |
||
| 47 | 'verify' => true, |
||
| 48 | 'timeout' => 60, |
||
| 49 | 'connect_timeout' => 10, |
||
| 50 | 'pool_size' => 25 |
||
| 51 | ], |
||
| 52 | $options |
||
| 53 | ); |
||
| 54 | $this->client = new Client($options); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function setLogger(LoggerInterface $logger) |
||
| 58 | { |
||
| 59 | $this->logger = $logger; |
||
| 60 | if ($logger instanceof LoggerInterface) { |
||
| 61 | $this->getEmitter()->attach(new LogSubscriber($logger)); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | public function addHandler($handler) |
||
| 66 | { |
||
| 67 | $this->getEmitter()->attach($handler); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @internal |
||
| 72 | * @return \GuzzleHttp\Event\Emitter|\GuzzleHttp\Event\EmitterInterface |
||
| 73 | */ |
||
| 74 | public function getEmitter() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param RequestInterface $request |
||
| 81 | * @return ResponseInterface |
||
| 82 | * @throws \Commercetools\Core\Error\ApiException |
||
| 83 | * @throws \Commercetools\Core\Error\BadGatewayException |
||
| 84 | * @throws \Commercetools\Core\Error\ConcurrentModificationException |
||
| 85 | * @throws \Commercetools\Core\Error\ErrorResponseException |
||
| 86 | * @throws \Commercetools\Core\Error\GatewayTimeoutException |
||
| 87 | * @throws \Commercetools\Core\Error\InternalServerErrorException |
||
| 88 | * @throws \Commercetools\Core\Error\InvalidTokenException |
||
| 89 | * @throws \Commercetools\Core\Error\NotFoundException |
||
| 90 | * @throws \Commercetools\Core\Error\ServiceUnavailableException |
||
| 91 | */ |
||
| 92 | public function execute(RequestInterface $request) |
||
| 110 | |||
| 111 | protected function packResponse(\GuzzleHttp\Message\ResponseInterface $response = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param RequestInterface[] $requests |
||
| 125 | * @return \Psr\Http\Message\ResponseInterface[] |
||
| 126 | * @throws \Commercetools\Core\Error\ApiException |
||
| 127 | * @throws \Commercetools\Core\Error\BadGatewayException |
||
| 128 | * @throws \Commercetools\Core\Error\ConcurrentModificationException |
||
| 129 | * @throws \Commercetools\Core\Error\ErrorResponseException |
||
| 130 | * @throws \Commercetools\Core\Error\GatewayTimeoutException |
||
| 131 | * @throws \Commercetools\Core\Error\InternalServerErrorException |
||
| 132 | * @throws \Commercetools\Core\Error\InvalidTokenException |
||
| 133 | * @throws \Commercetools\Core\Error\NotFoundException |
||
| 134 | * @throws \Commercetools\Core\Error\ServiceUnavailableException |
||
| 135 | */ |
||
| 136 | public function executeBatch(array $requests) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | protected function getBatchHttpRequests(array $requests) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param $oauthUri |
||
| 182 | * @param $clientId |
||
| 183 | * @param $clientSecret |
||
| 184 | * @param $formParams |
||
| 185 | * @return ResponseInterface |
||
| 186 | */ |
||
| 187 | public function authenticate($oauthUri, $clientId, $clientSecret, $formParams) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param RequestInterface $request |
||
| 212 | * @return AdapterPromiseInterface |
||
| 213 | */ |
||
| 214 | public function executeAsync(RequestInterface $request) |
||
| 239 | |||
| 240 | public static function getAdapterInfo() |
||
| 241 | { |
||
| 242 | return 'GuzzleHttp/' . Client::VERSION; |
||
| 244 | } |
||
| 245 |
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.