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 | * @var integer |
||
45 | * |
||
46 | * @ORM\Column(name="id", type="integer") |
||
47 | * @ORM\Id |
||
48 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
49 | */ |
||
50 | private $id; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | * |
||
55 | * @ORM\Column(name="status", type="string", length=1, options={"default":"N", "fixed":true}, nullable=false) |
||
56 | */ |
||
57 | private $status = self::STATUS_NEW; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | * |
||
62 | * @ORM\Column(name="name", type="string", length=250, nullable=true) |
||
63 | */ |
||
64 | private $donorFullName; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | * |
||
69 | * @ORM\Column(name="ort", type="string", length=250, nullable=true) |
||
70 | */ |
||
71 | private $donorCity; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | * |
||
76 | * @ORM\Column(name="email", type="string", length=250, nullable=true) |
||
77 | */ |
||
78 | private $donorEmail; |
||
79 | |||
80 | /** |
||
81 | * @var boolean |
||
82 | * |
||
83 | * @ORM\Column(name="info", type="boolean", options={"default":0}, nullable=false) |
||
84 | */ |
||
85 | private $donorOptsIntoNewsletter = 0; |
||
86 | |||
87 | /** |
||
88 | * @var boolean |
||
89 | * |
||
90 | * @ORM\Column(name="bescheinigung", type="boolean", nullable=true) |
||
91 | */ |
||
92 | private $donationReceipt; |
||
93 | |||
94 | /** |
||
95 | * @var string |
||
96 | * |
||
97 | * @ORM\Column(name="eintrag", type="string", length=250, options={"default":""}, nullable=false) |
||
98 | */ |
||
99 | private $publicRecord = ''; |
||
100 | |||
101 | /** |
||
102 | * @var string |
||
103 | * |
||
104 | * @ORM\Column(name="betrag", type="string", length=250, nullable=true) |
||
105 | */ |
||
106 | private $amount; |
||
107 | |||
108 | /** |
||
109 | * @var integer |
||
110 | * |
||
111 | * @ORM\Column(name="periode", type="smallint", options={"default":0}, nullable=false) |
||
112 | */ |
||
113 | private $paymentIntervalInMonths = 0; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | * |
||
118 | * @ORM\Column(name="zahlweise", type="string", length=3, options={"default":"BEZ", "fixed":true}, nullable=false) |
||
119 | */ |
||
120 | private $paymentType = 'BEZ'; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | * |
||
125 | * @ORM\Column(name="kommentar", type="text", options={"default":""}, nullable=false) |
||
126 | */ |
||
127 | private $comment = ''; |
||
128 | |||
129 | /** |
||
130 | * @var string |
||
131 | * |
||
132 | * @ORM\Column(name="ueb_code", type="string", length=32, options={"default":""}, nullable=false) |
||
133 | */ |
||
134 | private $bankTransferCode = ''; |
||
135 | |||
136 | /** |
||
137 | * @var string |
||
138 | * |
||
139 | * @ORM\Column(name="data", type="text", nullable=true) |
||
140 | */ |
||
141 | private $data; |
||
142 | |||
143 | /** |
||
144 | * @var string |
||
145 | * |
||
146 | * @ORM\Column(name="source", type="string", length=250, nullable=true) |
||
147 | */ |
||
148 | private $source; |
||
149 | |||
150 | /** |
||
151 | * @var string |
||
152 | * |
||
153 | * @ORM\Column(name="remote_addr", type="string", length=250, options={"default":""}, nullable=false) |
||
154 | */ |
||
155 | private $remoteAddr = ''; |
||
156 | |||
157 | /** |
||
158 | * @var string |
||
159 | * |
||
160 | * @ORM\Column(name="hash", type="string", length=250, nullable=true) |
||
161 | */ |
||
162 | private $hash; |
||
163 | |||
164 | /** |
||
165 | * @var boolean |
||
166 | * |
||
167 | * @ORM\Column(name="is_public", type="boolean", options={"default":0}, nullable=false) |
||
168 | */ |
||
169 | private $isPublic = 0; |
||
170 | |||
171 | /** |
||
172 | * @var \DateTime |
||
173 | * |
||
174 | * @Gedmo\Timestampable(on="create") |
||
175 | * @ORM\Column(name="dt_new", type="datetime") |
||
176 | */ |
||
177 | private $creationTime; |
||
178 | |||
179 | /** |
||
180 | * @var \DateTime |
||
181 | * |
||
182 | * @ORM\Column(name="dt_del", type="datetime", nullable=true) |
||
183 | */ |
||
184 | private $deletionTime; |
||
185 | |||
186 | /** |
||
187 | * @var \DateTime |
||
188 | * |
||
189 | * @ORM\Column(name="dt_exp", type="datetime", nullable=true) |
||
190 | */ |
||
191 | private $dtExp; |
||
192 | |||
193 | /** |
||
194 | * @var \DateTime |
||
195 | * |
||
196 | * @ORM\Column(name="dt_gruen", type="datetime", nullable=true) |
||
197 | */ |
||
198 | private $dtGruen; |
||
199 | |||
200 | /** |
||
201 | * @var \DateTime |
||
202 | * |
||
203 | * @ORM\Column(name="dt_backup", type="datetime", nullable=true) |
||
204 | */ |
||
205 | private $dtBackup; |
||
206 | |||
207 | /** |
||
208 | * @ORM\OneToOne(targetEntity="WMDE\Fundraising\Entities\DonationPayment", cascade={"all"}, fetch="EAGER") |
||
209 | */ |
||
210 | private $payment; |
||
211 | |||
212 | /** |
||
213 | * @param string $donorFullName |
||
214 | * |
||
215 | * @return self |
||
216 | */ |
||
217 | public function setDonorFullName( $donorFullName ) { |
||
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getDonorFullName() { |
||
229 | |||
230 | /** |
||
231 | * @param string $donorCity |
||
232 | * |
||
233 | * @return self |
||
234 | */ |
||
235 | public function setDonorCity( $donorCity ) { |
||
240 | |||
241 | /** |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getDonorCity() { |
||
247 | |||
248 | /** |
||
249 | * @param string $donorEmail |
||
250 | * |
||
251 | * @return self |
||
252 | */ |
||
253 | public function setDonorEmail( $donorEmail ) { |
||
258 | |||
259 | /** |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getDonorEmail() { |
||
265 | |||
266 | /** |
||
267 | * @param boolean $donorOptsIntoNewsletter |
||
268 | * |
||
269 | * @return self |
||
270 | */ |
||
271 | public function setDonorOptsIntoNewsletter( $donorOptsIntoNewsletter ) { |
||
276 | |||
277 | /** |
||
278 | * @return boolean |
||
279 | */ |
||
280 | public function getDonorOptsIntoNewsletter() { |
||
283 | |||
284 | /** |
||
285 | * Set donation receipt state |
||
286 | * |
||
287 | * @param boolean $donationReceipt |
||
288 | * @return self |
||
289 | */ |
||
290 | public function setDonationReceipt( $donationReceipt ) { |
||
295 | |||
296 | /** |
||
297 | * Get donation receipt state |
||
298 | * |
||
299 | * @return boolean |
||
300 | */ |
||
301 | public function getDonationReceipt() { |
||
304 | |||
305 | /** |
||
306 | * Set publicly displayed donation record |
||
307 | * |
||
308 | * @param string $publicRecord |
||
309 | * @return self |
||
310 | */ |
||
311 | public function setPublicRecord( $publicRecord ) { |
||
316 | |||
317 | /** |
||
318 | * Get publicly displayed donation record |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public function getPublicRecord() { |
||
325 | |||
326 | /** |
||
327 | * @param string $amount |
||
328 | * @return self |
||
329 | */ |
||
330 | public function setAmount( $amount ) { |
||
335 | |||
336 | /** |
||
337 | * @return string |
||
338 | */ |
||
339 | public function getAmount() { |
||
342 | |||
343 | /** |
||
344 | * @param integer $paymentIntervalInMonths |
||
345 | * |
||
346 | * @return self |
||
347 | */ |
||
348 | public function setPaymentIntervalInMonths( $paymentIntervalInMonths ) { |
||
353 | |||
354 | /** |
||
355 | * @return integer |
||
356 | */ |
||
357 | public function getPaymentIntervalInMonths() { |
||
360 | |||
361 | /** |
||
362 | * Set payment type short code |
||
363 | * |
||
364 | * @param string $paymentType |
||
365 | * @return self |
||
366 | */ |
||
367 | public function setPaymentType( $paymentType ) { |
||
372 | |||
373 | /** |
||
374 | * Get payment type short code |
||
375 | * |
||
376 | * @return string |
||
377 | */ |
||
378 | public function getPaymentType() { |
||
381 | |||
382 | /** |
||
383 | * @param string $comment |
||
384 | * @return self |
||
385 | */ |
||
386 | public function setComment( $comment ) { |
||
391 | |||
392 | /** |
||
393 | * @return string |
||
394 | */ |
||
395 | public function getComment() { |
||
398 | |||
399 | /** |
||
400 | * Set bank transfer reference code |
||
401 | * |
||
402 | * @param string $bankTransferCode |
||
403 | * |
||
404 | * @return self |
||
405 | */ |
||
406 | public function setBankTransferCode( $bankTransferCode ) { |
||
411 | |||
412 | /** |
||
413 | * Get bank transfer reference code |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | public function getBankTransferCode() { |
||
420 | |||
421 | /** |
||
422 | * @param string $source |
||
423 | * @return self |
||
424 | */ |
||
425 | public function setSource( $source ) { |
||
430 | |||
431 | /** |
||
432 | * @return string |
||
433 | */ |
||
434 | public function getSource() { |
||
437 | |||
438 | /** |
||
439 | * @param string $remoteAddr |
||
440 | * @return self |
||
441 | */ |
||
442 | public function setRemoteAddr( $remoteAddr ) { |
||
447 | |||
448 | /** |
||
449 | * @return string |
||
450 | */ |
||
451 | public function getRemoteAddr() { |
||
454 | |||
455 | /** |
||
456 | * @param string $hash |
||
457 | * @return self |
||
458 | */ |
||
459 | public function setHash( $hash ) { |
||
464 | |||
465 | /** |
||
466 | * @return string |
||
467 | */ |
||
468 | public function getHash() { |
||
471 | |||
472 | /** |
||
473 | * Sets if the donations comment should be public or private. |
||
474 | * @param boolean $isPublic |
||
475 | * @return self |
||
476 | */ |
||
477 | public function setIsPublic( $isPublic ) { |
||
482 | |||
483 | /** |
||
484 | * Gets if the donations comment is public or private. |
||
485 | * @return boolean |
||
486 | */ |
||
487 | public function getIsPublic() { |
||
490 | |||
491 | /** |
||
492 | * @param \DateTime $creationTime |
||
493 | * |
||
494 | * @return self |
||
495 | */ |
||
496 | public function setCreationTime( $creationTime ) { |
||
501 | |||
502 | /** |
||
503 | * @return \DateTime |
||
504 | */ |
||
505 | public function getCreationTime() { |
||
508 | |||
509 | /** |
||
510 | * @param \DateTime|null $deletionTime |
||
511 | * |
||
512 | * @return self |
||
513 | */ |
||
514 | public function setDeletionTime( $deletionTime ) { |
||
519 | |||
520 | /** |
||
521 | * @return \DateTime|null |
||
522 | */ |
||
523 | public function getDeletionTime() { |
||
526 | |||
527 | /** |
||
528 | * @param \DateTime $dtExp |
||
529 | * @return self |
||
530 | */ |
||
531 | public function setDtExp( $dtExp ) { |
||
536 | |||
537 | /** |
||
538 | * @return \DateTime |
||
539 | */ |
||
540 | public function getDtExp() { |
||
543 | |||
544 | /** |
||
545 | * @param string $status |
||
546 | * @return self |
||
547 | */ |
||
548 | public function setStatus( $status ) { |
||
553 | |||
554 | /** |
||
555 | * @return string |
||
556 | */ |
||
557 | public function getStatus() { |
||
560 | |||
561 | /** |
||
562 | * @param \DateTime $dtGruen |
||
563 | * @return self |
||
564 | */ |
||
565 | public function setDtGruen( $dtGruen ) { |
||
570 | |||
571 | /** |
||
572 | * @return \DateTime |
||
573 | */ |
||
574 | public function getDtGruen() { |
||
577 | |||
578 | /** |
||
579 | * @param \DateTime $dtBackup |
||
580 | * @return self |
||
581 | */ |
||
582 | public function setDtBackup( $dtBackup ) { |
||
587 | |||
588 | /** |
||
589 | * @return \DateTime |
||
590 | */ |
||
591 | public function getDtBackup() { |
||
594 | |||
595 | /** |
||
596 | * @return integer|null |
||
597 | */ |
||
598 | 3 | public function getId() { |
|
601 | |||
602 | public function getPayment(): ?DonationPayment { |
||
605 | |||
606 | public function setPayment( DonationPayment $payment ) { |
||
609 | |||
610 | |||
611 | /** |
||
612 | * @since 2.0 |
||
613 | * |
||
614 | * @param integer|null $id |
||
615 | */ |
||
616 | 2 | public function setId( $id ) { |
|
619 | |||
620 | public function getUExpiry() { |
||
623 | |||
624 | public function uTokenIsExpired() { |
||
627 | |||
628 | public function validateToken( $tokenToCheck, $serverSecret ) { |
||
637 | |||
638 | public function getEntryType( $mode = null ) { |
||
660 | |||
661 | /** |
||
662 | * @deprecated since 2.0, use encodeAndSetData or setDataObject instead |
||
663 | * |
||
664 | * @param string $data Base 64 encoded, serialized PHP array |
||
665 | * @return self |
||
666 | */ |
||
667 | public function setData( $data ) { |
||
672 | |||
673 | /** |
||
674 | * @deprecated since 2.0, use @see getDecodedData or @see getDataObject instead |
||
675 | * |
||
676 | * @return string Base 64 encoded, serialized PHP array |
||
677 | */ |
||
678 | public function getData() { |
||
681 | |||
682 | /** |
||
683 | * NOTE: if possible, use @see getDataObject instead, as it provides a nicer API. |
||
684 | * |
||
685 | * @since 2.0 |
||
686 | * @return array |
||
687 | */ |
||
688 | 8 | public function getDecodedData() { |
|
697 | |||
698 | /** |
||
699 | * NOTE: if possible, use @see modifyDataObject instead, as it provides a nicer API. |
||
700 | * |
||
701 | * @since 2.0 |
||
702 | * @param array $data |
||
703 | */ |
||
704 | 6 | public function encodeAndSetData( array $data ) { |
|
707 | |||
708 | /** |
||
709 | * WARNING: updates made to the return value will not be reflected in the Donation state. |
||
710 | * Similarly, updates to the Donation state will not propagate to the returned object. |
||
711 | * To update the Donation state, explicitly call @see setDataObject. |
||
712 | * |
||
713 | * @since 2.0 |
||
714 | * @return DonationData |
||
715 | */ |
||
716 | 3 | public function getDataObject() { |
|
727 | |||
728 | /** |
||
729 | * @since 2.0 |
||
730 | * @param DonationData $data |
||
731 | */ |
||
732 | 4 | public function setDataObject( DonationData $data ) { |
|
750 | |||
751 | /** |
||
752 | * @since 2.0 |
||
753 | * @param callable $modificationFunction Takes a modifiable DonationData parameter |
||
754 | */ |
||
755 | 1 | public function modifyDataObject( callable $modificationFunction ) { |
|
760 | |||
761 | } |
||
762 |