Complex classes like PaymentSlipData 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 PaymentSlipData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | abstract class PaymentSlipData |
||
| 60 | { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The array table for calculating the check digit by modulo 10 |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $moduloTable = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5]; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Determines if the payment slip has a recipient bank. Can be disabled for pre-printed payment slips |
||
| 71 | * |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | protected $withBank = true; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Determines if the payment slip has a account number. Can be disabled for pre-printed payment slips |
||
| 78 | * |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | protected $withAccountNumber = true; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Determines if the payment slip has a recipient. Can be disabled for pre-printed payment slips |
||
| 85 | * |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | protected $withRecipient = true; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Determines if it's an ESR or an ESR+ |
||
| 92 | * |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | protected $withAmount = true; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Determines if the payment slip has a payer. Can be disabled for pre-printed payment slips |
||
| 99 | * |
||
| 100 | * @var bool |
||
| 101 | */ |
||
| 102 | protected $withPayer = true; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The name of the bank |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | protected $bankName = ''; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The postal code and city of the bank |
||
| 113 | * |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | protected $bankCity = ''; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The bank or post cheque account where the money will be transferred to |
||
| 120 | * |
||
| 121 | * @var string |
||
| 122 | */ |
||
| 123 | protected $accountNumber = ''; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The first line of the recipient, e.g. "My Company Ltd." |
||
| 127 | * |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $recipientLine1 = ''; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * The second line of the recipient, e.g. "Examplestreet 61" |
||
| 134 | * |
||
| 135 | * @var string |
||
| 136 | */ |
||
| 137 | protected $recipientLine2 = ''; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * The third line of the recipient, e.g. "8000 Zürich" |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $recipientLine3 = ''; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * The fourth line of the recipient, if needed |
||
| 148 | * |
||
| 149 | * @var string |
||
| 150 | */ |
||
| 151 | protected $recipientLine4 = ''; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * The amount to be payed into. Can be disabled with withAmount = false for ESR+ slips |
||
| 155 | * |
||
| 156 | * @var float |
||
| 157 | */ |
||
| 158 | protected $amount = 0.0; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * The first line of the payer, e.g. "Hans Mustermann" |
||
| 162 | * |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | protected $payerLine1 = ''; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * The second line of the payer, e.g. "Main Street 11" |
||
| 169 | * |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | protected $payerLine2 = ''; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * The third line of the payer, e.g. "4052 Basel" |
||
| 176 | * |
||
| 177 | * @var string |
||
| 178 | */ |
||
| 179 | protected $payerLine3 = ''; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * The fourth line of the payer, if needed |
||
| 183 | * |
||
| 184 | * @var string |
||
| 185 | */ |
||
| 186 | protected $payerLine4 = ''; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Determines if the payment slip must not be used for payment (XXXed out) |
||
| 190 | * |
||
| 191 | * @var bool |
||
| 192 | */ |
||
| 193 | protected $notForPayment = false; |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set if payment slip has a bank specified |
||
| 197 | * |
||
| 198 | * Resets the bank data when disabling. |
||
| 199 | * |
||
| 200 | * @param bool $withBank True for yes, false for no |
||
| 201 | * @return $this The current instance for a fluent interface. |
||
| 202 | */ |
||
| 203 | 2 | public function setWithBank($withBank = true) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Get if payment slip has recipient specified |
||
| 218 | * |
||
| 219 | * @return bool True if payment slip has the recipient specified, else false. |
||
| 220 | */ |
||
| 221 | 1 | public function getWithBank() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Set if payment slip has an account number specified |
||
| 228 | * |
||
| 229 | * Resets the account number when disabling. |
||
| 230 | * |
||
| 231 | * @param bool $withAccountNumber True if yes, false if no. |
||
| 232 | * @return $this The current instance for a fluent interface. |
||
| 233 | */ |
||
| 234 | 2 | public function setWithAccountNumber($withAccountNumber = true) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Get if payment slip has an account number specified |
||
| 248 | * |
||
| 249 | * @return bool True if payment slip has an account number specified, else false. |
||
| 250 | */ |
||
| 251 | 1 | public function getWithAccountNumber() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Set if payment slip has a recipient specified |
||
| 258 | * |
||
| 259 | * Resets the recipient data when disabling. |
||
| 260 | * |
||
| 261 | * @param bool $withRecipient True if yes, false if no. |
||
| 262 | * @return $this The current instance for a fluent interface. |
||
| 263 | */ |
||
| 264 | 2 | public function setWithRecipient($withRecipient = true) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Get if payment slip has a recipient specified |
||
| 281 | * |
||
| 282 | * @return bool True if payment slip has a recipient specified, else false. |
||
| 283 | */ |
||
| 284 | 1 | public function getWithRecipient() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Set if payment slip has an amount specified |
||
| 291 | * |
||
| 292 | * Resets the amount when disabling. |
||
| 293 | * |
||
| 294 | * @param bool $withAmount True for yes, false for no. |
||
| 295 | * @return $this The current instance for a fluent interface. |
||
| 296 | */ |
||
| 297 | 2 | public function setWithAmount($withAmount = true) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Get if payment slip has an amount specified |
||
| 311 | * |
||
| 312 | * @return bool True if payment slip has an amount specified, else false. |
||
| 313 | */ |
||
| 314 | 1 | public function getWithAmount() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Set if payment slip has a payer specified |
||
| 321 | * |
||
| 322 | * Resets the payer data when disabling. |
||
| 323 | * |
||
| 324 | * @param bool $withPayer True if yes, false if no. |
||
| 325 | * @return $this The current instance for a fluent interface. |
||
| 326 | */ |
||
| 327 | 2 | public function setWithPayer($withPayer = true) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get if payment slip has a payer specified |
||
| 344 | * |
||
| 345 | * @return bool True if payment slip has a payer specified, else false. |
||
| 346 | */ |
||
| 347 | 1 | public function getWithPayer() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Sets the name, city and account number of the bank |
||
| 354 | * |
||
| 355 | * @param string $bankName Name of the bank. |
||
| 356 | * @param string $bankCity City of the bank. |
||
| 357 | * @return $this The current instance for a fluent interface. |
||
| 358 | */ |
||
| 359 | 1 | public function setBankData($bankName, $bankCity) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Set the name of the bank |
||
| 369 | * |
||
| 370 | * @param string $bankName The name of the bank. |
||
| 371 | * @return $this The current instance for a fluent interface. |
||
| 372 | * @throws DisabledDataException If the data is disabled. |
||
| 373 | * |
||
| 374 | * @todo Implement max length check |
||
| 375 | */ |
||
| 376 | 2 | public function setBankName($bankName) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Get the name of the bank |
||
| 388 | * |
||
| 389 | * @return string The name of the bank, if withBank is set to true. |
||
| 390 | * @throws DisabledDataException If the data is disabled. |
||
| 391 | */ |
||
| 392 | 2 | public function getBankName() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Set the postal code and city of the bank |
||
| 402 | * |
||
| 403 | * @param string $bankCity The postal code and city of the bank |
||
| 404 | * @return $this The current instance for a fluent interface. |
||
| 405 | * @throws DisabledDataException If the data is disabled. |
||
| 406 | * |
||
| 407 | * @todo Implement max length check |
||
| 408 | */ |
||
| 409 | 2 | public function setBankCity($bankCity) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Get the postal code and city of the bank |
||
| 421 | * |
||
| 422 | * @return string The postal code and city, if withBank is set to true. |
||
| 423 | * @throws DisabledDataException If the data is disabled. |
||
| 424 | */ |
||
| 425 | 2 | public function getBankCity() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Set the bank or post cheque account where the money will be transferred to |
||
| 435 | * |
||
| 436 | * @param string $accountNumber The bank or post cheque account. |
||
| 437 | * @return $this The current instance for a fluent interface. |
||
| 438 | * @throws DisabledDataException If the data is disabled. |
||
| 439 | * |
||
| 440 | * @todo Implement parameter validation (two hyphens, min & max length) |
||
| 441 | */ |
||
| 442 | 2 | public function setAccountNumber($accountNumber) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Get the bank or post cheque account where the money will be transferred to |
||
| 454 | * |
||
| 455 | * @return string The bank or post cheque account, if withAccountNumber is set to true. |
||
| 456 | * @throws DisabledDataException If the data is disabled. |
||
| 457 | */ |
||
| 458 | 2 | public function getAccountNumber() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Sets the four lines of the recipient |
||
| 468 | * |
||
| 469 | * @param string $recipientLine1 The first line of the recipient, e.g. "My Company Ltd.". |
||
| 470 | * @param string $recipientLine2 The second line of the recipient, e.g. "Examplestreet 61". |
||
| 471 | * @param string $recipientLine3 The third line of the recipient, e.g. "8000 Zürich". |
||
| 472 | * @param string $recipientLine4 The fourth line of the recipient, if needed. |
||
| 473 | * @return $this The current instance for a fluent interface. |
||
| 474 | */ |
||
| 475 | 1 | public function setRecipientData($recipientLine1, $recipientLine2, $recipientLine3 = '', $recipientLine4 = '') |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Set the first line of the recipient |
||
| 487 | * |
||
| 488 | * @param string $recipientLine1 The first line of the recipient, e.g. "My Company Ltd.". |
||
| 489 | * @return $this The current instance for a fluent interface. |
||
| 490 | * @throws DisabledDataException If the data is disabled. |
||
| 491 | */ |
||
| 492 | 2 | public function setRecipientLine1($recipientLine1) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Get the first line of the recipient |
||
| 504 | * |
||
| 505 | * @return string The first line of the recipient, if withRecipient is set to true. |
||
| 506 | * @throws DisabledDataException If the data is disabled. |
||
| 507 | */ |
||
| 508 | 2 | public function getRecipientLine1() |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Set the second line of the recipient |
||
| 518 | * |
||
| 519 | * @param string $recipientLine2 The second line of the recipient, e.g. "Examplestreet 61". |
||
| 520 | * @return $this The current instance for a fluent interface. |
||
| 521 | * @throws DisabledDataException If the data is disabled. |
||
| 522 | */ |
||
| 523 | 2 | public function setRecipientLine2($recipientLine2) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Get the second line of the recipient |
||
| 535 | * |
||
| 536 | * @return string The second line of the recipient, if withRecipient is set to true. |
||
| 537 | * @throws DisabledDataException If the data is disabled. |
||
| 538 | */ |
||
| 539 | 2 | public function getRecipientLine2() |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Set the third line of the recipient |
||
| 549 | * |
||
| 550 | * @param string $recipientLine3 The third line of the recipient, e.g. "8000 Zürich". |
||
| 551 | * @return $this The current instance for a fluent interface. |
||
| 552 | * @throws DisabledDataException If the data is disabled. |
||
| 553 | */ |
||
| 554 | 2 | public function setRecipientLine3($recipientLine3) |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Get the third line of the recipient |
||
| 566 | * |
||
| 567 | * @return string The third line of the recipient, if withRecipient is set to true. |
||
| 568 | * @throws DisabledDataException If the data is disabled. |
||
| 569 | */ |
||
| 570 | 2 | public function getRecipientLine3() |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Set the fourth line of the recipient |
||
| 580 | * |
||
| 581 | * @param string $recipientLine4 The fourth line of the recipient, if needed. |
||
| 582 | * @return $this The current instance for a fluent interface. |
||
| 583 | * @throws DisabledDataException If the data is disabled. |
||
| 584 | */ |
||
| 585 | 2 | public function setRecipientLine4($recipientLine4) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Get the fourth line of the recipient |
||
| 597 | * |
||
| 598 | * @return string The fourth line of the recipient, if withRecipient is set to true. |
||
| 599 | * @throws DisabledDataException If the data is disabled. |
||
| 600 | */ |
||
| 601 | 2 | public function getRecipientLine4() |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Set the amount of the payment slip. Only possible if it's not a ESR+. |
||
| 611 | * |
||
| 612 | * @param float $amount The amount to be payed into |
||
| 613 | * @return $this The current instance for a fluent interface. |
||
| 614 | * @throws DisabledDataException If the data is disabled. |
||
| 615 | */ |
||
| 616 | 4 | public function setAmount($amount = 0.0) |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Get the amount to be payed into |
||
| 632 | * |
||
| 633 | * @return float The amount to be payed into. |
||
| 634 | * @throws DisabledDataException If the data is disabled. |
||
| 635 | */ |
||
| 636 | 2 | public function getAmount() |
|
| 643 | |||
| 644 | /** |
||
| 645 | * Sets the four lines of the payer |
||
| 646 | * |
||
| 647 | * At least two lines are necessary. |
||
| 648 | * |
||
| 649 | * @param string $payerLine1 The first line of the payer, e.g. "Hans Mustermann". |
||
| 650 | * @param string $payerLine2 The second line of the payer, e.g. "Main Street 11". |
||
| 651 | * @param string $payerLine3 The third line of the payer, e.g. "4052 Basel". |
||
| 652 | * @param string $payerLine4 The fourth line of the payer, if needed. |
||
| 653 | * @return $this The current instance for a fluent interface. |
||
| 654 | */ |
||
| 655 | 1 | public function setPayerData($payerLine1, $payerLine2, $payerLine3 = '', $payerLine4 = '') |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Set the first line of the payer |
||
| 667 | * |
||
| 668 | * @param string $payerLine1 The first line of the payer, e.g. "Hans Mustermann". |
||
| 669 | * @return $this The current instance for a fluent interface. |
||
| 670 | * @throws DisabledDataException If the data is disabled. |
||
| 671 | */ |
||
| 672 | 2 | public function setPayerLine1($payerLine1) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Get the first line of the payer |
||
| 684 | * |
||
| 685 | * @return string The first line of the payer, if withPayer is set to true. |
||
| 686 | * @throws DisabledDataException If the data is disabled. |
||
| 687 | */ |
||
| 688 | 2 | public function getPayerLine1() |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Set the second line of the payer |
||
| 698 | * |
||
| 699 | * @param string $payerLine2 The second line of the payer, e.g. "Main Street 11". |
||
| 700 | * @return $this The current instance for a fluent interface. |
||
| 701 | * @throws DisabledDataException If the data is disabled. |
||
| 702 | */ |
||
| 703 | 2 | public function setPayerLine2($payerLine2) |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Get the second line of the payer |
||
| 715 | * |
||
| 716 | * @return string The second line of the payer, if withPayer is set to true. |
||
| 717 | * @throws DisabledDataException If the data is disabled. |
||
| 718 | */ |
||
| 719 | 2 | public function getPayerLine2() |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Set the third line of the payer |
||
| 729 | * |
||
| 730 | * @param string $payerLine3 The third line of the payer, e.g. "4052 Basel". |
||
| 731 | * @return $this The current instance for a fluent interface. |
||
| 732 | * @throws DisabledDataException If the data is disabled. |
||
| 733 | */ |
||
| 734 | 2 | public function setPayerLine3($payerLine3) |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Get the third line of the payer |
||
| 746 | * |
||
| 747 | * @return string The third line of the payer, if withPayer is set to true. |
||
| 748 | * @throws DisabledDataException If the data is disabled. |
||
| 749 | */ |
||
| 750 | 2 | public function getPayerLine3() |
|
| 757 | |||
| 758 | /** |
||
| 759 | * Set the fourth line of the payer |
||
| 760 | * |
||
| 761 | * @param string $payerLine4 The fourth line of the payer, if needed. |
||
| 762 | * @return $this The current instance for a fluent interface. |
||
| 763 | * @throws DisabledDataException If the data is disabled. |
||
| 764 | */ |
||
| 765 | 2 | public function setPayerLine4($payerLine4) |
|
| 774 | |||
| 775 | /** |
||
| 776 | * Get the fourth line of the payer |
||
| 777 | * |
||
| 778 | * @return string The fourth line of the payer, if withPayer is set to true. |
||
| 779 | * @throws DisabledDataException If the data is disabled. |
||
| 780 | */ |
||
| 781 | 2 | public function getPayerLine4() |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Clear the account of the two hyphens |
||
| 791 | * |
||
| 792 | * @return string The account of the two hyphens, 'XXXXXXXXX' if not for payment or else false. |
||
| 793 | * @throws DisabledDataException If the data is disabled. |
||
| 794 | * @throws PaymentSlipException If account number does not contain two hyphens. |
||
| 795 | * @todo Cover the edge cases with tests |
||
| 796 | */ |
||
| 797 | 2 | protected function getAccountDigits() |
|
| 815 | |||
| 816 | /** |
||
| 817 | * Get the francs amount without cents |
||
| 818 | * |
||
| 819 | * @return bool|int Francs amount without cents. |
||
| 820 | */ |
||
| 821 | 2 | public function getAmountFrancs() |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Get the zero filled, right padded, two digits long cents amount |
||
| 833 | * |
||
| 834 | * @return bool|string Amount of Cents, zero filled, right padded, two digits long. |
||
| 835 | */ |
||
| 836 | 2 | public function getAmountCents() |
|
| 846 | |||
| 847 | /** |
||
| 848 | * Set payment slip for not to be used for payment |
||
| 849 | * |
||
| 850 | * XXXes out all fields to prevent people using the payment slip. |
||
| 851 | * |
||
| 852 | * @param boolean $notForPayment True if not for payment, else false. |
||
| 853 | * @return $this The current instance for a fluent interface. |
||
| 854 | */ |
||
| 855 | 2 | public function setNotForPayment($notForPayment = true) |
|
| 879 | |||
| 880 | /** |
||
| 881 | * Get whether this payment slip must not be used for payment |
||
| 882 | * |
||
| 883 | * @return bool True if yes, else false. |
||
| 884 | */ |
||
| 885 | 1 | public function getNotForPayment() |
|
| 889 | |||
| 890 | /** |
||
| 891 | * Creates Modulo10 recursive check digit |
||
| 892 | * |
||
| 893 | * @copyright As found on http://www.developers-guide.net/forums/5431,modulo10-rekursiv (thanks, dude!) |
||
| 894 | * @param string $number Number to create recursive check digit off. |
||
| 895 | * @return int Recursive check digit. |
||
| 896 | */ |
||
| 897 | 2 | protected function modulo10($number) |
|
| 906 | |||
| 907 | /** |
||
| 908 | * Get a given string broken down in blocks of a certain size |
||
| 909 | * |
||
| 910 | * Example: 000000000000000 becomes more readable 00000 00000 00000 |
||
| 911 | * |
||
| 912 | * @param string $string The to be formatted string. |
||
| 913 | * @param int $blockSize The Block size of choice. |
||
| 914 | * @param bool $alignFromRight Right aligned, blocks are build from right. |
||
| 915 | * @return string Given string divided in blocks of given block size separated by one space. |
||
| 916 | */ |
||
| 917 | 1 | protected function breakStringIntoBlocks($string, $blockSize = 5, $alignFromRight = true) |
|
| 934 | |||
| 935 | /** |
||
| 936 | * Verify that a given parameter is boolean |
||
| 937 | * |
||
| 938 | * @param mixed $parameter The given parameter to validate. |
||
| 939 | * @param string $varName The name of the variable. |
||
| 940 | * @return true If the parameter is a boolean. |
||
| 941 | * @throws InvalidArgumentException If the parameter is not a boolean. |
||
| 942 | */ |
||
| 943 | 14 | protected function isBool($parameter, $varName) |
|
| 955 | } |
||
| 956 |