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_Builder 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_Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Payone_Builder |
||
| 33 | { |
||
| 34 | |||
| 35 | const KEY_API = 'api'; |
||
| 36 | const KEY_CLIENTAPI = 'client_api'; |
||
| 37 | const KEY_PROTOCOL = 'protocol'; |
||
| 38 | const KEY_SETTINGS = 'settings'; |
||
| 39 | const KEY_TRANSACTIONSTATUS = 'transaction_status'; |
||
| 40 | const KEY_SESSIONSTATUS = 'session_status'; |
||
| 41 | |||
| 42 | /** @var array */ |
||
| 43 | protected $factories = array(); |
||
| 44 | |||
| 45 | /** @var Payone_Config */ |
||
| 46 | protected $config = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @constructor |
||
| 50 | * @param null|Payone_Config $config config can be set via constructor or setConfig() |
||
| 51 | */ |
||
| 52 | public function __construct(Payone_Config $config = null) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @api |
||
| 70 | * |
||
| 71 | * @return Payone_ClientApi_Service_GenerateHash |
||
| 72 | */ |
||
| 73 | public function buildServiceClientApiGenerateHash() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @api |
||
| 80 | * |
||
| 81 | * @return Payone_Api_Service_Payment_Authorize |
||
| 82 | */ |
||
| 83 | public function buildServicePaymentAuthorize() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @api |
||
| 90 | * |
||
| 91 | * @return Payone_Api_Service_Payment_Preauthorize |
||
| 92 | */ |
||
| 93 | public function buildServicePaymentPreauthorize() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @api |
||
| 100 | * |
||
| 101 | * @return Payone_Api_Service_Payment_Capture |
||
| 102 | */ |
||
| 103 | public function buildServicePaymentCapture() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @api |
||
| 110 | * |
||
| 111 | * @return Payone_Api_Service_Payment_Debit |
||
| 112 | */ |
||
| 113 | public function buildServicePaymentDebit() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @api |
||
| 120 | * |
||
| 121 | * @return Payone_Api_Service_Payment_Refund |
||
| 122 | */ |
||
| 123 | public function buildServicePaymentRefund() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @api |
||
| 130 | * |
||
| 131 | * @return Payone_Api_Service_Verification_3dsCheck |
||
| 132 | */ |
||
| 133 | public function buildServiceVerification3dsCheck() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @api |
||
| 140 | * |
||
| 141 | * @return Payone_Api_Service_Management_GetInvoice |
||
| 142 | */ |
||
| 143 | public function buildServiceManagementGetInvoice() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @api |
||
| 150 | * |
||
| 151 | * @return Payone_Api_Service_Management_GetFile |
||
| 152 | */ |
||
| 153 | public function buildServiceManagementGetFile() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @api |
||
| 160 | * |
||
| 161 | * @return Payone_Api_Service_Management_ManageMandate |
||
| 162 | */ |
||
| 163 | public function buildServiceManagementManageMandate() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @api |
||
| 170 | * |
||
| 171 | * @return Payone_Api_Service_Verification_AddressCheck |
||
| 172 | */ |
||
| 173 | public function buildServiceVerificationAddressCheck() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @api |
||
| 180 | * |
||
| 181 | * @return Payone_Api_Service_Verification_CreditCardCheck |
||
| 182 | */ |
||
| 183 | public function buildServiceVerificationCreditCardCheck() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @api |
||
| 190 | * |
||
| 191 | * @return Payone_Api_Service_Verification_BankAccountCheck |
||
| 192 | */ |
||
| 193 | public function buildServiceVerificationBankAccountCheck() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @api |
||
| 200 | * |
||
| 201 | * @return Payone_Api_Service_Verification_Consumerscore |
||
| 202 | */ |
||
| 203 | public function buildServiceVerificationConsumerscore() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @api |
||
| 210 | * |
||
| 211 | * @return Payone_Settings_Service_XmlGenerate |
||
| 212 | */ |
||
| 213 | public function buildServiceSettingsXmlGenerate() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Service to start the paypal express checkout and |
||
| 220 | * in step two get customers shipping address from paypal. |
||
| 221 | * usage: |
||
| 222 | * $builder = $this->getPayoneBuilder(); |
||
| 223 | * $service = $builder->buildServicePaymentGenericpayment(); |
||
| 224 | * $response = $service->request($request); |
||
| 225 | * |
||
| 226 | * @api |
||
| 227 | * @return Payone_Api_Service_Payment_Genericpayment |
||
| 228 | */ |
||
| 229 | public function buildServicePaymentGenericpayment() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @api |
||
| 236 | * @param $key |
||
| 237 | * @param array $validIps |
||
| 238 | * @return Payone_TransactionStatus_Service_HandleRequest |
||
| 239 | */ |
||
| 240 | View Code Duplication | public function buildServiceTransactionStatusHandleRequest($key, array $validIps) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @api |
||
| 262 | * @param $key |
||
| 263 | * @param array $validIps |
||
| 264 | * @return Payone_SessionStatus_Service_HandleRequest |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function buildServiceSessionStatusHandleRequest($key, array $validIps) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $key Service key, e.g. "api/payment/authorize" |
||
| 288 | * @return Payone_Api_Service_Payment_Abstract |
||
| 289 | * @throws Exception |
||
| 290 | */ |
||
| 291 | protected function buildService($key) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param array $protocolConfig |
||
| 344 | * @param string $factoryKey |
||
| 345 | * @return Payone_Protocol_Service_Protocol_Abstract |
||
| 346 | */ |
||
| 347 | protected function buildServiceProtocol(array $protocolConfig, $factoryKey) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param $validatorConfig |
||
| 415 | * @return null|validator |
||
| 416 | */ |
||
| 417 | protected function buildServiceValidation($validatorConfig) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param \Payone_Config $config |
||
| 443 | */ |
||
| 444 | public function setConfig(Payone_Config $config) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @return \Payone_Config |
||
| 451 | */ |
||
| 452 | protected function getConfig() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param $key |
||
| 459 | * @return null|Payone_Api_Factory|Payone_Protocol_Factory|Payone_Settings_Factory|Payone_TransactionStatus_Factory |
||
| 460 | * @throws Exception |
||
| 461 | */ |
||
| 462 | protected function getFactory($key) |
||
| 470 | |||
| 471 | } |
||
| 472 |
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.