Complex classes like Donation 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 Donation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Donation { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @since 2.0 |
||
| 35 | */ |
||
| 36 | public const STATUS_NEW = 'N'; // status for direct debit |
||
| 37 | public const STATUS_PROMISE = 'Z'; // status for bank transfer |
||
| 38 | public const STATUS_EXTERNAL_INCOMPLETE = 'X'; // status for external payments |
||
| 39 | public const STATUS_EXTERNAL_BOOKED = 'B'; // status for external payments |
||
| 40 | public const STATUS_MODERATION = 'P'; |
||
| 41 | public const STATUS_CANCELLED = 'D'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @since 6.1 |
||
| 45 | * @deprecated since 6.1; This status is defined for historical reasons. It should not be used to define a |
||
| 46 | * donation's status anymore. |
||
| 47 | */ |
||
| 48 | public const STATUS_EXPORTED = 'E'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var integer |
||
| 52 | * |
||
| 53 | * @ORM\Column(name="id", type="integer") |
||
| 54 | * @ORM\Id |
||
| 55 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
| 56 | */ |
||
| 57 | private $id; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | * |
||
| 62 | * @ORM\Column(name="status", type="string", length=1, options={"default":"N", "fixed":true}, nullable=false) |
||
| 63 | */ |
||
| 64 | private $status = self::STATUS_NEW; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | * |
||
| 69 | * @ORM\Column(name="name", type="string", length=250, nullable=true) |
||
| 70 | */ |
||
| 71 | private $donorFullName; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | * |
||
| 76 | * @ORM\Column(name="ort", type="string", length=250, nullable=true) |
||
| 77 | */ |
||
| 78 | private $donorCity; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | * |
||
| 83 | * @ORM\Column(name="email", type="string", length=250, nullable=true) |
||
| 84 | */ |
||
| 85 | private $donorEmail; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var boolean |
||
| 89 | * |
||
| 90 | * @ORM\Column(name="info", type="boolean", options={"default":0}, nullable=false) |
||
| 91 | */ |
||
| 92 | private $donorOptsIntoNewsletter = 0; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var boolean |
||
| 96 | * |
||
| 97 | * @ORM\Column(name="bescheinigung", type="boolean", nullable=true) |
||
| 98 | */ |
||
| 99 | private $donationReceipt; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string |
||
| 103 | * |
||
| 104 | * @ORM\Column(name="eintrag", type="string", length=250, options={"default":""}, nullable=false) |
||
| 105 | */ |
||
| 106 | private $publicRecord = ''; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string |
||
| 110 | * |
||
| 111 | * @ORM\Column(name="betrag", type="string", length=250, nullable=true) |
||
| 112 | */ |
||
| 113 | private $amount; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var integer |
||
| 117 | * |
||
| 118 | * @ORM\Column(name="periode", type="smallint", options={"default":0}, nullable=false) |
||
| 119 | */ |
||
| 120 | private $paymentIntervalInMonths = 0; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | * |
||
| 125 | * @ORM\Column(name="zahlweise", type="string", length=3, options={"default":"BEZ", "fixed":true}, nullable=false) |
||
| 126 | */ |
||
| 127 | private $paymentType = 'BEZ'; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string |
||
| 131 | * |
||
| 132 | * @ORM\Column(name="kommentar", type="text", options={"default":""}, nullable=false) |
||
| 133 | */ |
||
| 134 | private $comment = ''; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string |
||
| 138 | * |
||
| 139 | * @ORM\Column(name="ueb_code", type="string", length=32, options={"default":""}, nullable=false) |
||
| 140 | */ |
||
| 141 | private $bankTransferCode = ''; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var string |
||
| 145 | * |
||
| 146 | * @ORM\Column(name="data", type="text", nullable=true) |
||
| 147 | */ |
||
| 148 | private $data; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var string |
||
| 152 | * |
||
| 153 | * @ORM\Column(name="source", type="string", length=250, nullable=true) |
||
| 154 | */ |
||
| 155 | private $source; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var string |
||
| 159 | * |
||
| 160 | * @ORM\Column(name="remote_addr", type="string", length=250, options={"default":""}, nullable=false) |
||
| 161 | */ |
||
| 162 | private $remoteAddr = ''; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var string |
||
| 166 | * |
||
| 167 | * @ORM\Column(name="hash", type="string", length=250, nullable=true) |
||
| 168 | */ |
||
| 169 | private $hash; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var boolean |
||
| 173 | * |
||
| 174 | * @ORM\Column(name="is_public", type="boolean", options={"default":0}, nullable=false) |
||
| 175 | */ |
||
| 176 | private $isPublic = 0; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var \DateTime |
||
| 180 | * |
||
| 181 | * @Gedmo\Timestampable(on="create") |
||
| 182 | * @ORM\Column(name="dt_new", type="datetime") |
||
| 183 | */ |
||
| 184 | private $creationTime; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var \DateTime |
||
| 188 | * |
||
| 189 | * @ORM\Column(name="dt_del", type="datetime", nullable=true) |
||
| 190 | */ |
||
| 191 | private $deletionTime; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var \DateTime |
||
| 195 | * |
||
| 196 | * @ORM\Column(name="dt_exp", type="datetime", nullable=true) |
||
| 197 | */ |
||
| 198 | private $dtExp; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @var \DateTime |
||
| 202 | * |
||
| 203 | * @ORM\Column(name="dt_gruen", type="datetime", nullable=true) |
||
| 204 | */ |
||
| 205 | private $dtGruen; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @var \DateTime |
||
| 209 | * |
||
| 210 | * @ORM\Column(name="dt_backup", type="datetime", nullable=true) |
||
| 211 | */ |
||
| 212 | private $dtBackup; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @ORM\OneToOne(targetEntity="WMDE\Fundraising\Entities\DonationPayment", cascade={"all"}, fetch="EAGER") |
||
| 216 | */ |
||
| 217 | private $payment; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @ORM\OneToOne(targetEntity="WMDE\Fundraising\Entities\AddressChange", cascade={"all"}, fetch="EAGER") |
||
| 221 | */ |
||
| 222 | private $addressChange; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Donation constructor creates a new AddressChange entity unless one is supplied by Doctrine |
||
| 226 | */ |
||
| 227 | 11 | public function __construct() { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $donorFullName |
||
| 235 | * |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | public function setDonorFullName( $donorFullName ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getDonorFullName() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $donorCity |
||
| 253 | * |
||
| 254 | * @return self |
||
| 255 | */ |
||
| 256 | public function setDonorCity( $donorCity ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getDonorCity() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $donorEmail |
||
| 271 | * |
||
| 272 | * @return self |
||
| 273 | */ |
||
| 274 | public function setDonorEmail( $donorEmail ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function getDonorEmail() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param boolean $donorOptsIntoNewsletter |
||
| 289 | * |
||
| 290 | * @return self |
||
| 291 | */ |
||
| 292 | public function setDonorOptsIntoNewsletter( $donorOptsIntoNewsletter ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return boolean |
||
| 300 | */ |
||
| 301 | public function getDonorOptsIntoNewsletter() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Set donation receipt state |
||
| 307 | * |
||
| 308 | * @param boolean $donationReceipt |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | public function setDonationReceipt( $donationReceipt ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Get donation receipt state |
||
| 319 | * |
||
| 320 | * @return boolean |
||
| 321 | */ |
||
| 322 | public function getDonationReceipt() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set publicly displayed donation record |
||
| 328 | * |
||
| 329 | * @param string $publicRecord |
||
| 330 | * @return self |
||
| 331 | */ |
||
| 332 | public function setPublicRecord( $publicRecord ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get publicly displayed donation record |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function getPublicRecord() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $amount |
||
| 349 | * @return self |
||
| 350 | */ |
||
| 351 | public function setAmount( $amount ) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getAmount() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param integer $paymentIntervalInMonths |
||
| 366 | * |
||
| 367 | * @return self |
||
| 368 | */ |
||
| 369 | public function setPaymentIntervalInMonths( $paymentIntervalInMonths ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return integer |
||
| 377 | */ |
||
| 378 | public function getPaymentIntervalInMonths() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set payment type short code |
||
| 384 | * |
||
| 385 | * @param string $paymentType |
||
| 386 | * @return self |
||
| 387 | */ |
||
| 388 | public function setPaymentType( $paymentType ) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get payment type short code |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | public function getPaymentType() { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $comment |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | public function setComment( $comment ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | public function getComment() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Set bank transfer reference code |
||
| 422 | * |
||
| 423 | * @param string $bankTransferCode |
||
| 424 | * |
||
| 425 | * @return self |
||
| 426 | */ |
||
| 427 | public function setBankTransferCode( $bankTransferCode ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get bank transfer reference code |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function getBankTransferCode() { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param string $source |
||
| 444 | * @return self |
||
| 445 | */ |
||
| 446 | public function setSource( $source ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function getSource() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param string $remoteAddr |
||
| 461 | * @return self |
||
| 462 | */ |
||
| 463 | public function setRemoteAddr( $remoteAddr ) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getRemoteAddr() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $hash |
||
| 478 | * @return self |
||
| 479 | */ |
||
| 480 | public function setHash( $hash ) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getHash() { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Sets if the donations comment should be public or private. |
||
| 495 | * @param boolean $isPublic |
||
| 496 | * @return self |
||
| 497 | */ |
||
| 498 | public function setIsPublic( $isPublic ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Gets if the donations comment is public or private. |
||
| 506 | * @return boolean |
||
| 507 | */ |
||
| 508 | public function getIsPublic() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param \DateTime $creationTime |
||
| 514 | * |
||
| 515 | * @return self |
||
| 516 | */ |
||
| 517 | public function setCreationTime( $creationTime ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return \DateTime |
||
| 525 | */ |
||
| 526 | public function getCreationTime() { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param \DateTime|null $deletionTime |
||
| 532 | * |
||
| 533 | * @return self |
||
| 534 | */ |
||
| 535 | public function setDeletionTime( $deletionTime ) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return \DateTime|null |
||
| 543 | */ |
||
| 544 | public function getDeletionTime() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @param \DateTime $dtExp |
||
| 550 | * @return self |
||
| 551 | */ |
||
| 552 | public function setDtExp( $dtExp ) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @return \DateTime |
||
| 560 | */ |
||
| 561 | public function getDtExp() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $status |
||
| 567 | * @return self |
||
| 568 | */ |
||
| 569 | public function setStatus( $status ) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function getStatus() { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @param \DateTime $dtGruen |
||
| 584 | * @return self |
||
| 585 | */ |
||
| 586 | public function setDtGruen( $dtGruen ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @return \DateTime |
||
| 594 | */ |
||
| 595 | public function getDtGruen() { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param \DateTime $dtBackup |
||
| 601 | * @return self |
||
| 602 | */ |
||
| 603 | public function setDtBackup( $dtBackup ) { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @return \DateTime |
||
| 611 | */ |
||
| 612 | public function getDtBackup() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @return integer|null |
||
| 618 | */ |
||
| 619 | 3 | public function getId() { |
|
| 622 | |||
| 623 | public function getPayment(): ?DonationPayment { |
||
| 626 | |||
| 627 | public function setPayment( DonationPayment $payment ) { |
||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * @since 2.0 |
||
| 634 | * |
||
| 635 | * @param integer|null $id |
||
| 636 | */ |
||
| 637 | 2 | public function setId( $id ) { |
|
| 640 | |||
| 641 | public function getUExpiry() { |
||
| 644 | |||
| 645 | public function uTokenIsExpired() { |
||
| 648 | |||
| 649 | public function validateToken( $tokenToCheck, $serverSecret ) { |
||
| 658 | |||
| 659 | public function getEntryType( $mode = null ) { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @deprecated since 2.0, use encodeAndSetData or setDataObject instead |
||
| 684 | * |
||
| 685 | * @param string $data Base 64 encoded, serialized PHP array |
||
| 686 | * @return self |
||
| 687 | */ |
||
| 688 | public function setData( $data ) { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @deprecated since 2.0, use @see getDecodedData or @see getDataObject instead |
||
| 696 | * |
||
| 697 | * @return string Base 64 encoded, serialized PHP array |
||
| 698 | */ |
||
| 699 | public function getData() { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * NOTE: if possible, use @see getDataObject instead, as it provides a nicer API. |
||
| 705 | * |
||
| 706 | * @since 2.0 |
||
| 707 | * @return array |
||
| 708 | */ |
||
| 709 | 8 | public function getDecodedData() { |
|
| 718 | |||
| 719 | /** |
||
| 720 | * NOTE: if possible, use @see modifyDataObject instead, as it provides a nicer API. |
||
| 721 | * |
||
| 722 | * @since 2.0 |
||
| 723 | * @param array $data |
||
| 724 | */ |
||
| 725 | 6 | public function encodeAndSetData( array $data ) { |
|
| 728 | |||
| 729 | /** |
||
| 730 | * WARNING: updates made to the return value will not be reflected in the Donation state. |
||
| 731 | * Similarly, updates to the Donation state will not propagate to the returned object. |
||
| 732 | * To update the Donation state, explicitly call @see setDataObject. |
||
| 733 | * |
||
| 734 | * @since 2.0 |
||
| 735 | * @return DonationData |
||
| 736 | */ |
||
| 737 | 3 | public function getDataObject() { |
|
| 748 | |||
| 749 | /** |
||
| 750 | * @since 2.0 |
||
| 751 | * @param DonationData $data |
||
| 752 | */ |
||
| 753 | 4 | public function setDataObject( DonationData $data ) { |
|
| 771 | |||
| 772 | /** |
||
| 773 | * @since 2.0 |
||
| 774 | * @param callable $modificationFunction Takes a modifiable DonationData parameter |
||
| 775 | */ |
||
| 776 | 1 | public function modifyDataObject( callable $modificationFunction ) { |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Get AddressChange reference |
||
| 784 | * |
||
| 785 | * @since 8.0 |
||
| 786 | * |
||
| 787 | * @return AddressChange |
||
| 788 | */ |
||
| 789 | public function getAddressChange(): AddressChange { |
||
| 792 | } |
||
| 793 |