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() { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $donorFullName |
||
| 233 | * |
||
| 234 | * @return self |
||
| 235 | */ |
||
| 236 | public function setDonorFullName( $donorFullName ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function getDonorFullName() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $donorCity |
||
| 251 | * |
||
| 252 | * @return self |
||
| 253 | */ |
||
| 254 | public function setDonorCity( $donorCity ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getDonorCity() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $donorEmail |
||
| 269 | * |
||
| 270 | * @return self |
||
| 271 | */ |
||
| 272 | public function setDonorEmail( $donorEmail ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getDonorEmail() { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param boolean $donorOptsIntoNewsletter |
||
| 287 | * |
||
| 288 | * @return self |
||
| 289 | */ |
||
| 290 | public function setDonorOptsIntoNewsletter( $donorOptsIntoNewsletter ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return boolean |
||
| 298 | */ |
||
| 299 | public function getDonorOptsIntoNewsletter() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set donation receipt state |
||
| 305 | * |
||
| 306 | * @param boolean $donationReceipt |
||
| 307 | * @return self |
||
| 308 | */ |
||
| 309 | public function setDonationReceipt( $donationReceipt ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get donation receipt state |
||
| 317 | * |
||
| 318 | * @return boolean |
||
| 319 | */ |
||
| 320 | public function getDonationReceipt() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Set publicly displayed donation record |
||
| 326 | * |
||
| 327 | * @param string $publicRecord |
||
| 328 | * @return self |
||
| 329 | */ |
||
| 330 | public function setPublicRecord( $publicRecord ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get publicly displayed donation record |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getPublicRecord() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $amount |
||
| 347 | * @return self |
||
| 348 | */ |
||
| 349 | public function setAmount( $amount ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function getAmount() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param integer $paymentIntervalInMonths |
||
| 364 | * |
||
| 365 | * @return self |
||
| 366 | */ |
||
| 367 | public function setPaymentIntervalInMonths( $paymentIntervalInMonths ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return integer |
||
| 375 | */ |
||
| 376 | public function getPaymentIntervalInMonths() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Set payment type short code |
||
| 382 | * |
||
| 383 | * @param string $paymentType |
||
| 384 | * @return self |
||
| 385 | */ |
||
| 386 | public function setPaymentType( $paymentType ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get payment type short code |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function getPaymentType() { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param string $comment |
||
| 403 | * @return self |
||
| 404 | */ |
||
| 405 | public function setComment( $comment ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getComment() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Set bank transfer reference code |
||
| 420 | * |
||
| 421 | * @param string $bankTransferCode |
||
| 422 | * |
||
| 423 | * @return self |
||
| 424 | */ |
||
| 425 | public function setBankTransferCode( $bankTransferCode ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get bank transfer reference code |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public function getBankTransferCode() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param string $source |
||
| 442 | * @return self |
||
| 443 | */ |
||
| 444 | public function setSource( $source ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getSource() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param string $remoteAddr |
||
| 459 | * @return self |
||
| 460 | */ |
||
| 461 | public function setRemoteAddr( $remoteAddr ) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function getRemoteAddr() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $hash |
||
| 476 | * @return self |
||
| 477 | */ |
||
| 478 | public function setHash( $hash ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | public function getHash() { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Sets if the donations comment should be public or private. |
||
| 493 | * @param boolean $isPublic |
||
| 494 | * @return self |
||
| 495 | */ |
||
| 496 | public function setIsPublic( $isPublic ) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Gets if the donations comment is public or private. |
||
| 504 | * @return boolean |
||
| 505 | */ |
||
| 506 | public function getIsPublic() { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param \DateTime $creationTime |
||
| 512 | * |
||
| 513 | * @return self |
||
| 514 | */ |
||
| 515 | public function setCreationTime( $creationTime ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @return \DateTime |
||
| 523 | */ |
||
| 524 | public function getCreationTime() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param \DateTime|null $deletionTime |
||
| 530 | * |
||
| 531 | * @return self |
||
| 532 | */ |
||
| 533 | public function setDeletionTime( $deletionTime ) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return \DateTime|null |
||
| 541 | */ |
||
| 542 | public function getDeletionTime() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param \DateTime $dtExp |
||
| 548 | * @return self |
||
| 549 | */ |
||
| 550 | public function setDtExp( $dtExp ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @return \DateTime |
||
| 558 | */ |
||
| 559 | public function getDtExp() { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param string $status |
||
| 565 | * @return self |
||
| 566 | */ |
||
| 567 | public function setStatus( $status ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @return string |
||
| 575 | */ |
||
| 576 | public function getStatus() { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @param \DateTime $dtGruen |
||
| 582 | * @return self |
||
| 583 | */ |
||
| 584 | public function setDtGruen( $dtGruen ) { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @return \DateTime |
||
| 592 | */ |
||
| 593 | public function getDtGruen() { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @param \DateTime $dtBackup |
||
| 599 | * @return self |
||
| 600 | */ |
||
| 601 | public function setDtBackup( $dtBackup ) { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @return \DateTime |
||
| 609 | */ |
||
| 610 | public function getDtBackup() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @return integer|null |
||
| 616 | */ |
||
| 617 | 3 | public function getId() { |
|
| 620 | |||
| 621 | public function getPayment(): ?DonationPayment { |
||
| 624 | |||
| 625 | public function setPayment( DonationPayment $payment ) { |
||
| 628 | |||
| 629 | |||
| 630 | /** |
||
| 631 | * @since 2.0 |
||
| 632 | * |
||
| 633 | * @param integer|null $id |
||
| 634 | */ |
||
| 635 | 2 | public function setId( $id ) { |
|
| 638 | |||
| 639 | public function getUExpiry() { |
||
| 642 | |||
| 643 | public function uTokenIsExpired() { |
||
| 646 | |||
| 647 | public function validateToken( $tokenToCheck, $serverSecret ) { |
||
| 656 | |||
| 657 | public function getEntryType( $mode = null ) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @deprecated since 2.0, use encodeAndSetData or setDataObject instead |
||
| 682 | * |
||
| 683 | * @param string $data Base 64 encoded, serialized PHP array |
||
| 684 | * @return self |
||
| 685 | */ |
||
| 686 | public function setData( $data ) { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @deprecated since 2.0, use @see getDecodedData or @see getDataObject instead |
||
| 694 | * |
||
| 695 | * @return string Base 64 encoded, serialized PHP array |
||
| 696 | */ |
||
| 697 | public function getData() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * NOTE: if possible, use @see getDataObject instead, as it provides a nicer API. |
||
| 703 | * |
||
| 704 | * @since 2.0 |
||
| 705 | * @return array |
||
| 706 | */ |
||
| 707 | 8 | public function getDecodedData() { |
|
| 716 | |||
| 717 | /** |
||
| 718 | * NOTE: if possible, use @see modifyDataObject instead, as it provides a nicer API. |
||
| 719 | * |
||
| 720 | * @since 2.0 |
||
| 721 | * @param array $data |
||
| 722 | */ |
||
| 723 | 6 | public function encodeAndSetData( array $data ) { |
|
| 726 | |||
| 727 | /** |
||
| 728 | * WARNING: updates made to the return value will not be reflected in the Donation state. |
||
| 729 | * Similarly, updates to the Donation state will not propagate to the returned object. |
||
| 730 | * To update the Donation state, explicitly call @see setDataObject. |
||
| 731 | * |
||
| 732 | * @since 2.0 |
||
| 733 | * @return DonationData |
||
| 734 | */ |
||
| 735 | 3 | public function getDataObject() { |
|
| 746 | |||
| 747 | /** |
||
| 748 | * @since 2.0 |
||
| 749 | * @param DonationData $data |
||
| 750 | */ |
||
| 751 | 4 | public function setDataObject( DonationData $data ) { |
|
| 769 | |||
| 770 | /** |
||
| 771 | * @since 2.0 |
||
| 772 | * @param callable $modificationFunction Takes a modifiable DonationData parameter |
||
| 773 | */ |
||
| 774 | 1 | public function modifyDataObject( callable $modificationFunction ) { |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Get AddressChange reference |
||
| 782 | * |
||
| 783 | * @since 8.0 |
||
| 784 | * |
||
| 785 | * @return AddressChange |
||
| 786 | */ |
||
| 787 | public function getAddressChange(): AddressChange { |
||
| 790 | } |
||
| 791 |