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 | * @ORM\JoinColumn(name="address_change_id", referencedColumnName="id") |
||
| 222 | */ |
||
| 223 | private $addressChange; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $donorFullName |
||
| 227 | * |
||
| 228 | * @return self |
||
| 229 | */ |
||
| 230 | public function setDonorFullName( $donorFullName ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getDonorFullName() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $donorCity |
||
| 245 | * |
||
| 246 | * @return self |
||
| 247 | */ |
||
| 248 | public function setDonorCity( $donorCity ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getDonorCity() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $donorEmail |
||
| 263 | * |
||
| 264 | * @return self |
||
| 265 | */ |
||
| 266 | public function setDonorEmail( $donorEmail ) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function getDonorEmail() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param boolean $donorOptsIntoNewsletter |
||
| 281 | * |
||
| 282 | * @return self |
||
| 283 | */ |
||
| 284 | public function setDonorOptsIntoNewsletter( $donorOptsIntoNewsletter ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return boolean |
||
| 292 | */ |
||
| 293 | public function getDonorOptsIntoNewsletter() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set donation receipt state |
||
| 299 | * |
||
| 300 | * @param boolean $donationReceipt |
||
| 301 | * @return self |
||
| 302 | */ |
||
| 303 | public function setDonationReceipt( $donationReceipt ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get donation receipt state |
||
| 311 | * |
||
| 312 | * @return boolean |
||
| 313 | */ |
||
| 314 | public function getDonationReceipt() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Set publicly displayed donation record |
||
| 320 | * |
||
| 321 | * @param string $publicRecord |
||
| 322 | * @return self |
||
| 323 | */ |
||
| 324 | public function setPublicRecord( $publicRecord ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get publicly displayed donation record |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getPublicRecord() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $amount |
||
| 341 | * @return self |
||
| 342 | */ |
||
| 343 | public function setAmount( $amount ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public function getAmount() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param integer $paymentIntervalInMonths |
||
| 358 | * |
||
| 359 | * @return self |
||
| 360 | */ |
||
| 361 | public function setPaymentIntervalInMonths( $paymentIntervalInMonths ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return integer |
||
| 369 | */ |
||
| 370 | public function getPaymentIntervalInMonths() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set payment type short code |
||
| 376 | * |
||
| 377 | * @param string $paymentType |
||
| 378 | * @return self |
||
| 379 | */ |
||
| 380 | public function setPaymentType( $paymentType ) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get payment type short code |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function getPaymentType() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $comment |
||
| 397 | * @return self |
||
| 398 | */ |
||
| 399 | public function setComment( $comment ) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getComment() { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Set bank transfer reference code |
||
| 414 | * |
||
| 415 | * @param string $bankTransferCode |
||
| 416 | * |
||
| 417 | * @return self |
||
| 418 | */ |
||
| 419 | public function setBankTransferCode( $bankTransferCode ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get bank transfer reference code |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getBankTransferCode() { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $source |
||
| 436 | * @return self |
||
| 437 | */ |
||
| 438 | public function setSource( $source ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getSource() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $remoteAddr |
||
| 453 | * @return self |
||
| 454 | */ |
||
| 455 | public function setRemoteAddr( $remoteAddr ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public function getRemoteAddr() { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param string $hash |
||
| 470 | * @return self |
||
| 471 | */ |
||
| 472 | public function setHash( $hash ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getHash() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Sets if the donations comment should be public or private. |
||
| 487 | * @param boolean $isPublic |
||
| 488 | * @return self |
||
| 489 | */ |
||
| 490 | public function setIsPublic( $isPublic ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Gets if the donations comment is public or private. |
||
| 498 | * @return boolean |
||
| 499 | */ |
||
| 500 | public function getIsPublic() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param \DateTime $creationTime |
||
| 506 | * |
||
| 507 | * @return self |
||
| 508 | */ |
||
| 509 | public function setCreationTime( $creationTime ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @return \DateTime |
||
| 517 | */ |
||
| 518 | public function getCreationTime() { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param \DateTime|null $deletionTime |
||
| 524 | * |
||
| 525 | * @return self |
||
| 526 | */ |
||
| 527 | public function setDeletionTime( $deletionTime ) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @return \DateTime|null |
||
| 535 | */ |
||
| 536 | public function getDeletionTime() { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param \DateTime $dtExp |
||
| 542 | * @return self |
||
| 543 | */ |
||
| 544 | public function setDtExp( $dtExp ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @return \DateTime |
||
| 552 | */ |
||
| 553 | public function getDtExp() { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param string $status |
||
| 559 | * @return self |
||
| 560 | */ |
||
| 561 | public function setStatus( $status ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function getStatus() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param \DateTime $dtGruen |
||
| 576 | * @return self |
||
| 577 | */ |
||
| 578 | public function setDtGruen( $dtGruen ) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @return \DateTime |
||
| 586 | */ |
||
| 587 | public function getDtGruen() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param \DateTime $dtBackup |
||
| 593 | * @return self |
||
| 594 | */ |
||
| 595 | public function setDtBackup( $dtBackup ) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return \DateTime |
||
| 603 | */ |
||
| 604 | public function getDtBackup() { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @return integer|null |
||
| 610 | */ |
||
| 611 | 3 | public function getId() { |
|
| 614 | |||
| 615 | public function getPayment(): ?DonationPayment { |
||
| 618 | |||
| 619 | public function setPayment( DonationPayment $payment ) { |
||
| 622 | |||
| 623 | |||
| 624 | /** |
||
| 625 | * @since 2.0 |
||
| 626 | * |
||
| 627 | * @param integer|null $id |
||
| 628 | */ |
||
| 629 | 2 | public function setId( $id ) { |
|
| 632 | |||
| 633 | public function getUExpiry() { |
||
| 636 | |||
| 637 | public function uTokenIsExpired() { |
||
| 640 | |||
| 641 | public function validateToken( $tokenToCheck, $serverSecret ) { |
||
| 650 | |||
| 651 | public function getEntryType( $mode = null ) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @deprecated since 2.0, use encodeAndSetData or setDataObject instead |
||
| 676 | * |
||
| 677 | * @param string $data Base 64 encoded, serialized PHP array |
||
| 678 | * @return self |
||
| 679 | */ |
||
| 680 | public function setData( $data ) { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @deprecated since 2.0, use @see getDecodedData or @see getDataObject instead |
||
| 688 | * |
||
| 689 | * @return string Base 64 encoded, serialized PHP array |
||
| 690 | */ |
||
| 691 | public function getData() { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * NOTE: if possible, use @see getDataObject instead, as it provides a nicer API. |
||
| 697 | * |
||
| 698 | * @since 2.0 |
||
| 699 | * @return array |
||
| 700 | */ |
||
| 701 | 8 | public function getDecodedData() { |
|
| 710 | |||
| 711 | /** |
||
| 712 | * NOTE: if possible, use @see modifyDataObject instead, as it provides a nicer API. |
||
| 713 | * |
||
| 714 | * @since 2.0 |
||
| 715 | * @param array $data |
||
| 716 | */ |
||
| 717 | 6 | public function encodeAndSetData( array $data ) { |
|
| 720 | |||
| 721 | /** |
||
| 722 | * WARNING: updates made to the return value will not be reflected in the Donation state. |
||
| 723 | * Similarly, updates to the Donation state will not propagate to the returned object. |
||
| 724 | * To update the Donation state, explicitly call @see setDataObject. |
||
| 725 | * |
||
| 726 | * @since 2.0 |
||
| 727 | * @return DonationData |
||
| 728 | */ |
||
| 729 | 3 | public function getDataObject() { |
|
| 740 | |||
| 741 | /** |
||
| 742 | * @since 2.0 |
||
| 743 | * @param DonationData $data |
||
| 744 | */ |
||
| 745 | 4 | public function setDataObject( DonationData $data ) { |
|
| 763 | |||
| 764 | /** |
||
| 765 | * @since 2.0 |
||
| 766 | * @param callable $modificationFunction Takes a modifiable DonationData parameter |
||
| 767 | */ |
||
| 768 | 1 | public function modifyDataObject( callable $modificationFunction ) { |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Get AddressChange reference |
||
| 776 | * |
||
| 777 | * @since 8.0 |
||
| 778 | * |
||
| 779 | * @return AddressChange |
||
| 780 | */ |
||
| 781 | public function getAddressChange(): AddressChange { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Set AddressChange reference |
||
| 787 | * |
||
| 788 | * @since 8.0 |
||
| 789 | * @param $addressChange AddressChange |
||
| 790 | * |
||
| 791 | * @return AddressChange |
||
| 792 | */ |
||
| 793 | public function setAddressChange(AddressChange $addressChange) { |
||
| 796 | } |
||
| 797 |