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 |
||
| 11 | class Api |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var \Payum\Core\HttpClientInterface |
||
| 15 | */ |
||
| 16 | protected $client; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var \Http\Message\MessageFactory |
||
| 20 | */ |
||
| 21 | protected $messageFactory; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $options = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * $encrypter. |
||
| 30 | * |
||
| 31 | * @var Encrypter |
||
| 32 | */ |
||
| 33 | protected $encrypter; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param array $options |
||
| 37 | * @param \Payum\Core\HttpClientInterface $client |
||
| 38 | * @param \Http\Message\MessageFactory $messageFactory |
||
| 39 | * @param Encrypter $encrypter |
||
| 40 | * |
||
| 41 | * @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid |
||
| 42 | */ |
||
| 43 | 14 | public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, Encrypter $encrypter = null) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $fields |
||
| 54 | * @return array |
||
| 55 | */ |
||
| 56 | 3 | protected function doRequest(array $fields, $type = 'sync') |
|
| 72 | |||
| 73 | /** |
||
| 74 | * parseResponse. |
||
| 75 | * |
||
| 76 | * @param array $response |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | 9 | public function parseResponse($response) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * parseStr. |
||
| 114 | * |
||
| 115 | * @param string $str |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | 9 | protected function parseStr($str) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * getApiEndpoint. |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | 3 | public function getApiEndpoint($type = 'capture') |
|
| 151 | |||
| 152 | /** |
||
| 153 | * createTransaction. |
||
| 154 | * |
||
| 155 | * @param array $params |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 4 | public function createTransaction(array $params) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * getTransactionData. |
||
| 198 | * |
||
| 199 | * @param mixed $params |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | 1 | View Code Duplication | public function getTransactionData(array $params) |
| 218 | |||
| 219 | /** |
||
| 220 | * refundTransaction. |
||
| 221 | * |
||
| 222 | * @param array $params |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 1 | public function refundTransaction(array $params) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * cancelTransaction. |
||
| 248 | * |
||
| 249 | * @param array $params |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | 1 | View Code Duplication | public function cancelTransaction(array $params) |
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $params |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 4 | public function calculateHash($params) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * verifyHash. |
||
| 280 | * |
||
| 281 | * @param array $response |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | 4 | public function verifyHash($response) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * isMobile. |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 3 | protected function isMobile() |
|
| 307 | } |
||
| 308 |