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 |
||
| 22 | class Guzzle6Adapter implements AdapterInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var Client |
||
| 26 | */ |
||
| 27 | protected $client; |
||
| 28 | |||
| 29 | protected $logger; |
||
| 30 | |||
| 31 | 108 | public function __construct(array $options = []) |
|
| 32 | { |
||
| 33 | 108 | $options = array_merge( |
|
| 34 | [ |
||
| 35 | 108 | 'allow_redirects' => false, |
|
| 36 | 'verify' => true, |
||
| 37 | 'timeout' => 60, |
||
| 38 | 'connect_timeout' => 10, |
||
| 39 | 'pool_size' => 25 |
||
| 40 | ], |
||
| 41 | 108 | $options |
|
| 42 | ); |
||
| 43 | 108 | $this->client = new Client($options); |
|
| 44 | 108 | } |
|
| 45 | |||
| 46 | 43 | public function setLogger(LoggerInterface $logger, $logLevel = LogLevel::INFO, $formatter = null) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Middleware that logs requests, responses, and errors using a message |
||
| 57 | * formatter. |
||
| 58 | * |
||
| 59 | * @param LoggerInterface $logger Logs messages. |
||
| 60 | * @param MessageFormatter $formatter Formatter used to create message strings. |
||
| 61 | * @param string $logLevel Level at which to log requests. |
||
| 62 | * |
||
| 63 | * @return callable Returns a function that accepts the next handler. |
||
| 64 | */ |
||
| 65 | 474 | private static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = LogLevel::INFO) |
|
| 66 | { |
||
| 67 | return function (callable $handler) use ($logger, $formatter, $logLevel) { |
||
| 68 | return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) { |
||
| 69 | 474 | return $handler($request, $options)->then( |
|
| 70 | function ($response) use ($logger, $request, $formatter, $logLevel) { |
||
| 71 | 474 | $message = $formatter->format($request, $response); |
|
| 72 | 474 | $context[AbstractApiResponse::X_CORRELATION_ID] = $response->getHeader( |
|
|
|
|||
| 73 | 474 | AbstractApiResponse::X_CORRELATION_ID |
|
| 74 | ); |
||
| 75 | 474 | $logger->log($logLevel, $message, $context); |
|
| 76 | 474 | return $response; |
|
| 77 | 474 | }, |
|
| 78 | 474 | function ($reason) use ($logger, $request, $formatter) { |
|
| 79 | $response = null; |
||
| 80 | $context = []; |
||
| 81 | if ($reason instanceof RequestException) { |
||
| 82 | $response = $reason->getResponse(); |
||
| 83 | if (!is_null($response)) { |
||
| 84 | $context[AbstractApiResponse::X_CORRELATION_ID] = $response->getHeader( |
||
| 85 | AbstractApiResponse::X_CORRELATION_ID |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | $message = $formatter->format($request, $response, $reason); |
||
| 90 | $logger->notice($message, $context); |
||
| 91 | return \GuzzleHttp\Promise\rejection_for($reason); |
||
| 92 | 474 | } |
|
| 93 | ); |
||
| 94 | 38 | }; |
|
| 95 | 43 | }; |
|
| 96 | } |
||
| 97 | |||
| 98 | 43 | public function addHandler($handler) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param RequestInterface $request |
||
| 105 | * @return ResponseInterface |
||
| 106 | */ |
||
| 107 | 517 | public function execute(RequestInterface $request) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param RequestInterface[] $requests |
||
| 121 | * @return ResponseInterface[] |
||
| 122 | */ |
||
| 123 | 397 | public function executeBatch(array $requests) |
|
| 124 | { |
||
| 125 | 397 | $results = Pool::batch( |
|
| 126 | 397 | $this->client, |
|
| 127 | 397 | $requests |
|
| 128 | ); |
||
| 129 | |||
| 130 | 397 | $responses = []; |
|
| 131 | 397 | foreach ($results as $key => $result) { |
|
| 132 | 397 | $httpResponse = $result; |
|
| 133 | 397 | View Code Duplication | if ($result instanceof RequestException) { |
| 134 | 16 | $request = $requests[$key]; |
|
| 135 | 16 | $httpResponse = $result->getResponse(); |
|
| 136 | 16 | $httpResponse = ApiException::create($request, $httpResponse, $result); |
|
| 137 | } |
||
| 138 | 397 | $responses[$key] = $httpResponse; |
|
| 139 | } |
||
| 140 | |||
| 141 | 397 | return $responses; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param $oauthUri |
||
| 146 | * @param $clientId |
||
| 147 | * @param $clientSecret |
||
| 148 | * @param $formParams |
||
| 149 | * @return ResponseInterface |
||
| 150 | */ |
||
| 151 | 44 | public function authenticate($oauthUri, $clientId, $clientSecret, $formParams) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param RequestInterface $request |
||
| 168 | * @return AdapterPromiseInterface |
||
| 169 | */ |
||
| 170 | 4 | public function executeAsync(RequestInterface $request) |
|
| 176 | |||
| 177 | 72 | public static function getAdapterInfo() |
|
| 181 | } |
||
| 182 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.