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 AdapterOptionInterface, CorrelationIdAware, TokenProviderAware |
||
23 | { |
||
24 | const DEFAULT_CONCURRENCY = 25; |
||
25 | /** |
||
26 | * @var Client |
||
27 | */ |
||
28 | protected $client; |
||
29 | |||
30 | protected $logger; |
||
31 | |||
32 | private $concurrency; |
||
33 | |||
34 | 112 | public function __construct(array $options = []) |
|
49 | |||
50 | 44 | public function setLogger(LoggerInterface $logger, $logLevel = LogLevel::INFO, $formatter = null) |
|
58 | |||
59 | public function setCorrelationIdProvider(CorrelationIdProvider $provider) |
||
68 | |||
69 | public function setOAuthTokenProvider(TokenProvider $tokenProvider) |
||
78 | |||
79 | /** |
||
80 | * Middleware that logs requests, responses, and errors using a message |
||
81 | * formatter. |
||
82 | * |
||
83 | * @param LoggerInterface $logger Logs messages. |
||
84 | * @param MessageFormatter $formatter Formatter used to create message strings. |
||
85 | * @param string $logLevel Level at which to log requests. |
||
86 | * |
||
87 | * @return callable Returns a function that accepts the next handler. |
||
88 | */ |
||
89 | private static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = LogLevel::INFO) |
||
121 | |||
122 | 66 | public function addHandler($handler) |
|
126 | |||
127 | /** |
||
128 | * @param RequestInterface $request |
||
129 | * @param array $clientOptions |
||
130 | * @return ResponseInterface |
||
131 | * @throws ApiException |
||
132 | * @throws \Commercetools\Core\Error\ApiException |
||
133 | * @throws \Commercetools\Core\Error\BadGatewayException |
||
134 | * @throws \Commercetools\Core\Error\ConcurrentModificationException |
||
135 | * @throws \Commercetools\Core\Error\ErrorResponseException |
||
136 | * @throws \Commercetools\Core\Error\GatewayTimeoutException |
||
137 | * @throws \Commercetools\Core\Error\InternalServerErrorException |
||
138 | * @throws \Commercetools\Core\Error\InvalidTokenException |
||
139 | * @throws \Commercetools\Core\Error\NotFoundException |
||
140 | * @throws \Commercetools\Core\Error\ServiceUnavailableException |
||
141 | */ |
||
142 | 527 | public function execute(RequestInterface $request, array $clientOptions = []) |
|
153 | |||
154 | /** |
||
155 | * @param RequestInterface[] $requests |
||
156 | * @param array $clientOptions |
||
157 | * @return ResponseInterface[] |
||
158 | */ |
||
159 | 406 | public function executeBatch(array $requests, array $clientOptions = []) |
|
183 | |||
184 | /** |
||
185 | * @param $oauthUri |
||
186 | * @param $clientId |
||
187 | * @param $clientSecret |
||
188 | * @param $formParams |
||
189 | * @return ResponseInterface |
||
190 | */ |
||
191 | 45 | public function authenticate($oauthUri, $clientId, $clientSecret, $formParams) |
|
205 | |||
206 | /** |
||
207 | * @param RequestInterface $request |
||
208 | * @param array $clientOptions |
||
209 | * @return AdapterPromiseInterface |
||
210 | */ |
||
211 | 4 | public function executeAsync(RequestInterface $request, array $clientOptions = []) |
|
217 | |||
218 | 75 | public static function getAdapterInfo() |
|
222 | } |
||
223 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.