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:
Complex classes like Payone_Api_Factory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Payone_Api_Factory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Payone_Api_Factory |
||
| 24 | { |
||
| 25 | |||
| 26 | /** @var Payone_Api_Config */ |
||
| 27 | protected $config = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @constructor |
||
| 31 | * @param Payone_Api_Config $config |
||
| 32 | */ |
||
| 33 | public function __construct(Payone_Api_Config $config = null) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return Payone_Api_Adapter_Interface |
||
| 40 | */ |
||
| 41 | protected function buildHttpClient() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | protected function isEnabledCurl() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $key Service Key, e.g. 'payment/refund' |
||
| 63 | * @return Payone_Api_Service_Payment_Authorize|Payone_Api_Service_Payment_Debit|Payone_Api_Service_Payment_Preauthorize|Payone_Api_Service_Payment_Refund |
||
| 64 | * @throws Exception |
||
| 65 | */ |
||
| 66 | public function buildService($key) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return Payone_Api_Mapper_Currency |
||
| 80 | */ |
||
| 81 | public function buildMapperCurrency() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns Path to currency.properties file |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | protected function getCurrencyPropertiesPath() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return Payone_Api_Mapper_Request_Payment_Preauthorization |
||
| 99 | */ |
||
| 100 | public function buildMapperRequestPreauthorize() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return Payone_Api_Mapper_Request_Payment_Authorization |
||
| 109 | */ |
||
| 110 | public function buildMapperRequestAuthorize() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return Payone_Api_Mapper_Request_Payment_Capture |
||
| 119 | */ |
||
| 120 | public function buildMapperRequestCapture() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return Payone_Api_Mapper_Request_Payment_Debit |
||
| 129 | */ |
||
| 130 | public function buildMapperRequestDebit() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return Payone_Api_Mapper_Request_Payment_Refund |
||
| 139 | */ |
||
| 140 | public function buildMapperRequestRefund() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return Payone_Api_Mapper_Request_Payment_Vauthorization |
||
| 149 | */ |
||
| 150 | public function buildMapperRequestVauthorize() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return Payone_Api_Mapper_Request_Payment_CreateAccess |
||
| 159 | */ |
||
| 160 | public function buildMapperRequestCreateAccess() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return Payone_Api_Mapper_Request_Payment_Genericpayment |
||
| 169 | */ |
||
| 170 | public function buildMapperRequestGenericpayment() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return Payone_Api_Mapper_Request_Management_UpdateAccess |
||
| 179 | */ |
||
| 180 | public function buildMapperRequestUpdateAccess() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return Payone_Api_Mapper_Response_Preauthorization |
||
| 189 | */ |
||
| 190 | protected function buildMapperResponsePreauthorize() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return Payone_Api_Mapper_Response_Authorization |
||
| 198 | */ |
||
| 199 | protected function buildMapperResponseAuthorize() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return Payone_Api_Mapper_Response_Capture |
||
| 207 | */ |
||
| 208 | protected function buildMapperResponseCapture() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return Payone_Api_Mapper_Response_Debit |
||
| 216 | */ |
||
| 217 | protected function buildMapperResponseDebit() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return Payone_Api_Mapper_Response_Refund |
||
| 225 | */ |
||
| 226 | protected function buildMapperResponseRefund() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return Payone_Api_Mapper_Response_3dsCheck |
||
| 234 | */ |
||
| 235 | protected function buildMapperResponse3dsCheck() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return Payone_Api_Mapper_Response_AddressCheck |
||
| 243 | */ |
||
| 244 | protected function buildMapperResponseAddressCheck() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return Payone_Api_Mapper_Response_BankAccountCheck |
||
| 252 | */ |
||
| 253 | protected function buildMapperResponseBankAccountCheck() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return Payone_Api_Mapper_Response_Consumerscore |
||
| 261 | */ |
||
| 262 | protected function buildMapperResponseConsumerscore() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return Payone_Api_Mapper_Response_CreditCardCheck |
||
| 270 | */ |
||
| 271 | protected function buildMapperResponseCreditCardCheck() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return Payone_Api_Mapper_Response_GetInvoice |
||
| 279 | */ |
||
| 280 | protected function buildMapperResponseGetInvoice() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return Payone_Api_Mapper_Response_GetFile |
||
| 288 | */ |
||
| 289 | protected function buildMapperResponseGetFile() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return Payone_Api_Mapper_Response_Vauthorization |
||
| 297 | */ |
||
| 298 | public function buildMapperResponseVauthorize() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return Payone_Api_Mapper_Response_CreateAccess |
||
| 306 | */ |
||
| 307 | public function buildMapperResponseCreateAccess() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return Payone_Api_Mapper_Response_Genericpayment |
||
| 315 | */ |
||
| 316 | public function buildMapperResponseGenericpayment() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return Payone_Api_Mapper_Response_UpdateAccess |
||
| 324 | */ |
||
| 325 | public function buildMapperResponseUpdateAccess() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return Payone_Api_Mapper_Response_ManageMandate |
||
| 333 | */ |
||
| 334 | public function buildMapperResponseManageMandate() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return Payone_Api_Service_Payment_Preauthorize |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function buildServicePaymentPreauthorize() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @return Payone_Api_Service_Payment_Authorize |
||
| 355 | */ |
||
| 356 | View Code Duplication | public function buildServicePaymentAuthorize() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @return Payone_Api_Service_Payment_Capture |
||
| 368 | */ |
||
| 369 | View Code Duplication | public function buildServicePaymentCapture() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @return Payone_Api_Service_Payment_Debit |
||
| 381 | */ |
||
| 382 | View Code Duplication | public function buildServicePaymentDebit() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @return Payone_Api_Service_Payment_Refund |
||
| 394 | */ |
||
| 395 | View Code Duplication | public function buildServicePaymentRefund() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @return Payone_Api_Service_Verification_3dsCheck |
||
| 407 | */ |
||
| 408 | public function buildServiceVerification3dscheck() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return Payone_Api_Service_Verification_AddressCheck |
||
| 419 | */ |
||
| 420 | public function buildServiceVerificationAddressCheck() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return Payone_Api_Service_Verification_BankAccountCheck |
||
| 431 | */ |
||
| 432 | public function buildServiceVerificationBankAccountCheck() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return Payone_Api_Service_Verification_Consumerscore |
||
| 443 | */ |
||
| 444 | public function buildServiceVerificationConsumerscore() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return Payone_Api_Service_Verification_CreditCardCheck |
||
| 455 | */ |
||
| 456 | public function buildServiceVerificationCreditCardCheck() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @return Payone_Api_Service_Management_GetInvoice |
||
| 467 | */ |
||
| 468 | public function buildServiceManagementGetInvoice() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return Payone_Api_Service_Management_GetFile |
||
| 479 | */ |
||
| 480 | public function buildServiceManagementGetFile() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return Payone_Api_Service_Management_ManageMandate |
||
| 491 | */ |
||
| 492 | public function buildServiceManagementManageMandate() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @return Payone_Api_Service_Payment_Vauthorize |
||
| 503 | */ |
||
| 504 | View Code Duplication | public function buildServicePaymentVauthorize() |
|
| 513 | |||
| 514 | /** |
||
| 515 | * @return Payone_Api_Service_Payment_CreateAccess |
||
| 516 | */ |
||
| 517 | View Code Duplication | public function buildServicePaymentCreateAccess() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Create service for genericpayment request. |
||
| 529 | * @return Payone_Api_Service_Payment_Genericpayment |
||
| 530 | */ |
||
| 531 | View Code Duplication | public function buildServicePaymentGenericpayment() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * @return Payone_Api_Service_Management_UpdateAccess |
||
| 543 | */ |
||
| 544 | View Code Duplication | public function buildServiceManagementUpdateAccess() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * @return Payone_Api_Service_ProtocolRequest |
||
| 556 | */ |
||
| 557 | public function buildServiceProtocolRequest() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @return Payone_Api_Validator_DefaultParameters |
||
| 566 | */ |
||
| 567 | public function buildValidatorDefault() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param Payone_Api_Config $config |
||
| 576 | */ |
||
| 577 | public function setConfig($config) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @return Payone_Api_Config |
||
| 584 | */ |
||
| 585 | public function getConfig() |
||
| 589 | |||
| 590 | } |
||
| 591 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.