Complex classes like PaymentRequest 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 PaymentRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class PaymentRequest extends Payload implements PaymentRequestInterface |
||
| 13 | { |
||
| 14 | use OrderLineArrayTrait; |
||
| 15 | |||
| 16 | const ACCOUNT_OFFER_REQUIRED = 'required'; |
||
| 17 | const ACCOUNT_OFFER_DISABLED = 'disabled'; |
||
| 18 | |||
| 19 | const PAYMENT_SOURCE_ECOMMERCE = 'eCommerce'; |
||
| 20 | const PAYMENT_SOURCE_MOBI = 'mobi'; |
||
| 21 | const PAYMENT_SOURCE_MOTO = 'moto'; |
||
| 22 | const PAYMENT_SOURCE_MAIL_ORDER = 'mail_order'; |
||
| 23 | const PAYMENT_SOURCE_TELEPHONE_ORDER = 'telephone_order'; |
||
| 24 | |||
| 25 | const SHIPPING_METHOD_LOW_COST = 'LowCost'; |
||
| 26 | const SHIPPING_METHOD_DESIGNATED_BY_CUSTOMER = 'DesignatedByCustomer'; |
||
| 27 | const SHIPPING_METHOD_INTERNATIONAL = 'International'; |
||
| 28 | const SHIPPING_METHOD_MILITARY = 'Military'; |
||
| 29 | const SHIPPING_METHOD_NEXT_DAY = 'NextDay'; |
||
| 30 | const SHIPPING_METHOD_OTHER = 'Other'; |
||
| 31 | const SHIPPING_METHOD_STORE_PICKUP = 'StorePickup'; |
||
| 32 | const SHIPPING_METHOD_TWO_DAY_SERVICE = 'TwoDayService'; |
||
| 33 | const SHIPPING_METHOD_THREE_DAY_SERVICE = 'ThreeDayService'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $terminal; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $shopOrderId; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | private $amount; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Currency in ISO-4217 format |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $currency; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $language; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $transactionInfo; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $type; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $ccToken; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $saleReconciliationIdentifier; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $saleInvoiceNumber; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var int |
||
| 89 | */ |
||
| 90 | private $salesTax; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | private $cookieParts; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | private $paymentSource; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private $fraudService; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | private $shippingMethod; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var \DateTimeInterface |
||
| 114 | */ |
||
| 115 | private $customerCreatedDate; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | private $organisationNumber; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private $accountOffer; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var CustomerInfoInterface |
||
| 129 | */ |
||
| 130 | private $customerInfo; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var ConfigInterface |
||
| 134 | */ |
||
| 135 | private $config; |
||
| 136 | |||
| 137 | 15 | public function __construct(string $terminal, string $shopOrderId, Money $amount) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | 9 | public function getPayload() : array |
|
| 183 | |||
| 184 | 9 | public function validate() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Takes an array of cookie parts and returns an urlencoded string ready to send |
||
| 208 | * |
||
| 209 | * @param array $cookieParts |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 12 | public static function parseCookieParts(array $cookieParts) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $key |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 3 | public function getCookiePart(string $key) : string |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $key |
||
| 234 | * @param string $value |
||
| 235 | * @return PaymentRequest |
||
| 236 | */ |
||
| 237 | 6 | public function setCookiePart(string $key, string $value) : self |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | 6 | public function getTerminal() : string |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $terminal |
||
| 253 | * @return PaymentRequest |
||
| 254 | */ |
||
| 255 | 15 | public function setTerminal(string $terminal) : self |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 6 | public function getShopOrderId() : string |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $shopOrderId |
||
| 271 | * @return PaymentRequest |
||
| 272 | */ |
||
| 273 | 15 | public function setShopOrderId(string $shopOrderId) : self |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @return Money |
||
| 281 | */ |
||
| 282 | 15 | public function getAmount() : ?Money |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param Money $amount |
||
| 289 | * @return PaymentRequest |
||
| 290 | */ |
||
| 291 | 15 | public function setAmount(Money $amount) : self |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | 3 | public function getLanguage() : ?string |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $language |
||
| 312 | * @return PaymentRequest |
||
| 313 | */ |
||
| 314 | 6 | public function setLanguage(string $language) : self |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 3 | public function getTransactionInfo() : ?array |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param array $transactionInfo |
||
| 330 | * @return PaymentRequest |
||
| 331 | */ |
||
| 332 | 6 | public function setTransactionInfo(array $transactionInfo) : self |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | 3 | public function getType() : ?string |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $type |
||
| 348 | * @return PaymentRequest |
||
| 349 | */ |
||
| 350 | 6 | public function setType(string $type) : self |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | 3 | public function getCcToken() : ?string |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $ccToken |
||
| 366 | * @return PaymentRequest |
||
| 367 | */ |
||
| 368 | 6 | public function setCcToken(string $ccToken) : self |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | 3 | public function getSaleReconciliationIdentifier() : ?string |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $saleReconciliationIdentifier |
||
| 384 | * @return PaymentRequest |
||
| 385 | */ |
||
| 386 | 6 | public function setSaleReconciliationIdentifier(string $saleReconciliationIdentifier) : self |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | 3 | public function getSaleInvoiceNumber() : ?string |
|
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $saleInvoiceNumber |
||
| 402 | * @return PaymentRequest |
||
| 403 | */ |
||
| 404 | 6 | public function setSaleInvoiceNumber(string $saleInvoiceNumber) : self |
|
| 409 | |||
| 410 | /** |
||
| 411 | * @return Money |
||
| 412 | */ |
||
| 413 | 12 | public function getSalesTax() : ?Money |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param Money $salesTax |
||
| 424 | * @return PaymentRequest |
||
| 425 | */ |
||
| 426 | 6 | public function setSalesTax(Money $salesTax) : self |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | 3 | public function getCookieParts(): array |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @param array $cookieParts |
||
| 446 | * @return PaymentRequest |
||
| 447 | */ |
||
| 448 | 3 | public function setCookieParts(array $cookieParts) : self |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | 3 | public function getPaymentSource() : ?string |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $paymentSource |
||
| 464 | * @return PaymentRequest |
||
| 465 | */ |
||
| 466 | 6 | public function setPaymentSource(string $paymentSource) : self |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 3 | public function getFraudService() : ?string |
|
| 479 | |||
| 480 | /** |
||
| 481 | * @param string $fraudService |
||
| 482 | * @return PaymentRequest |
||
| 483 | */ |
||
| 484 | 6 | public function setFraudService(string $fraudService) : self |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | 3 | public function getShippingMethod() : ?string |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @param string $shippingMethod |
||
| 500 | * @return PaymentRequest |
||
| 501 | */ |
||
| 502 | 6 | public function setShippingMethod(string $shippingMethod) : self |
|
| 507 | |||
| 508 | /** |
||
| 509 | * @return \DateTimeInterface |
||
| 510 | */ |
||
| 511 | 3 | public function getCustomerCreatedDate() : ?\DateTimeInterface |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @param \DateTimeInterface $customerCreatedDate |
||
| 518 | * @return PaymentRequest |
||
| 519 | */ |
||
| 520 | 6 | public function setCustomerCreatedDate(\DateTimeInterface $customerCreatedDate) : self |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | 3 | public function getOrganisationNumber() : ?string |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $organisationNumber |
||
| 536 | * @return PaymentRequest |
||
| 537 | */ |
||
| 538 | 6 | public function setOrganisationNumber(string $organisationNumber) : self |
|
| 543 | |||
| 544 | /** |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | 3 | public function getAccountOffer() : ?string |
|
| 551 | |||
| 552 | /** |
||
| 553 | * @param string $accountOffer |
||
| 554 | * @return PaymentRequest |
||
| 555 | */ |
||
| 556 | 6 | public function setAccountOffer(string $accountOffer) : self |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @return CustomerInfoInterface |
||
| 564 | */ |
||
| 565 | 9 | public function getCustomerInfo() : CustomerInfoInterface |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @param CustomerInfoInterface $customerInfo |
||
| 575 | * @return PaymentRequest |
||
| 576 | */ |
||
| 577 | 6 | public function setCustomerInfo(CustomerInfoInterface $customerInfo) : self |
|
| 582 | |||
| 583 | /** |
||
| 584 | * @return ConfigInterface |
||
| 585 | */ |
||
| 586 | 9 | public function getConfig() : ConfigInterface |
|
| 593 | |||
| 594 | /** |
||
| 595 | * @param ConfigInterface $config |
||
| 596 | * @return PaymentRequest |
||
| 597 | */ |
||
| 598 | 3 | public function setConfig(ConfigInterface $config) : self |
|
| 603 | } |
||
| 604 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.