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 |
||
17 | class Donation { |
||
18 | |||
19 | /** |
||
20 | * @since 2.0 |
||
21 | */ |
||
22 | const STATUS_NEW = 'N'; // status for direct debit |
||
23 | const STATUS_PROMISE = 'Z'; // status for bank transfer |
||
24 | const STATUS_EXTERNAL_INCOMPLETE = 'X'; // status for external payments |
||
25 | const STATUS_EXTERNAL_BOOKED = 'B'; // status for external payments |
||
26 | const STATUS_MODERATION = 'P'; |
||
27 | const STATUS_CANCELLED = 'D'; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | * |
||
32 | * @ORM\Column(name="id", type="integer") |
||
33 | * @ORM\Id |
||
34 | * @ORM\GeneratedValue(strategy="IDENTITY") |
||
35 | */ |
||
36 | private $id; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | * |
||
41 | * @ORM\Column(name="status", type="string", length=1, options={"default":"N", "fixed":true}, nullable=false) |
||
42 | */ |
||
43 | private $status = self::STATUS_NEW; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | * |
||
48 | * @ORM\Column(name="name", type="string", length=250, nullable=true) |
||
49 | */ |
||
50 | private $donorFullName; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | * |
||
55 | * @ORM\Column(name="ort", type="string", length=250, nullable=true) |
||
56 | */ |
||
57 | private $donorCity; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | * |
||
62 | * @ORM\Column(name="email", type="string", length=250, nullable=true) |
||
63 | */ |
||
64 | private $donorEmail; |
||
65 | |||
66 | /** |
||
67 | * @var boolean |
||
68 | * |
||
69 | * @ORM\Column(name="info", type="boolean", options={"default":0}, nullable=false) |
||
70 | */ |
||
71 | private $donorOptsIntoNewsletter = 0; |
||
72 | |||
73 | /** |
||
74 | * @var boolean |
||
75 | * |
||
76 | * @ORM\Column(name="bescheinigung", type="boolean", nullable=true) |
||
77 | */ |
||
78 | private $donationReceipt; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | * |
||
83 | * @ORM\Column(name="eintrag", type="string", length=250, options={"default":""}, nullable=false) |
||
84 | */ |
||
85 | private $publicRecord = ''; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | * |
||
90 | * @ORM\Column(name="betrag", type="string", length=250, nullable=true) |
||
91 | */ |
||
92 | private $amount; |
||
93 | |||
94 | /** |
||
95 | * @var integer |
||
96 | * |
||
97 | * @ORM\Column(name="periode", type="smallint", options={"default":0}, nullable=false) |
||
98 | */ |
||
99 | private $paymentIntervalInMonths = 0; |
||
100 | |||
101 | /** |
||
102 | * @var string |
||
103 | * |
||
104 | * @ORM\Column(name="zahlweise", type="string", length=3, options={"default":"BEZ", "fixed":true}, nullable=false) |
||
105 | */ |
||
106 | private $paymentType = 'BEZ'; |
||
107 | |||
108 | /** |
||
109 | * @var string |
||
110 | * |
||
111 | * @ORM\Column(name="kommentar", type="text", options={"default":""}, nullable=false) |
||
112 | */ |
||
113 | private $comment = ''; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | * |
||
118 | * @ORM\Column(name="ueb_code", type="string", length=32, options={"default":""}, nullable=false) |
||
119 | */ |
||
120 | private $bankTransferCode = ''; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | * |
||
125 | * @ORM\Column(name="data", type="text", nullable=true) |
||
126 | */ |
||
127 | private $data; |
||
128 | |||
129 | /** |
||
130 | * @var string |
||
131 | * |
||
132 | * @ORM\Column(name="source", type="string", length=250, nullable=true) |
||
133 | */ |
||
134 | private $source; |
||
135 | |||
136 | /** |
||
137 | * @var string |
||
138 | * |
||
139 | * @ORM\Column(name="remote_addr", type="string", length=250, options={"default":""}, nullable=false) |
||
140 | */ |
||
141 | private $remoteAddr = ''; |
||
142 | |||
143 | /** |
||
144 | * @var string |
||
145 | * |
||
146 | * @ORM\Column(name="hash", type="string", length=250, nullable=true) |
||
147 | */ |
||
148 | private $hash; |
||
149 | |||
150 | /** |
||
151 | * @var boolean |
||
152 | * |
||
153 | * @ORM\Column(name="is_public", type="boolean", options={"default":0}, nullable=false) |
||
154 | */ |
||
155 | private $isPublic = 0; |
||
156 | |||
157 | /** |
||
158 | * @var \DateTime |
||
159 | * |
||
160 | * @Gedmo\Timestampable(on="create") |
||
161 | * @ORM\Column(name="dt_new", type="datetime") |
||
162 | */ |
||
163 | private $creationTime; |
||
164 | |||
165 | /** |
||
166 | * @var \DateTime |
||
167 | * |
||
168 | * @ORM\Column(name="dt_del", type="datetime", nullable=true) |
||
169 | */ |
||
170 | private $deletionTime; |
||
171 | |||
172 | /** |
||
173 | * @var \DateTime |
||
174 | * |
||
175 | * @ORM\Column(name="dt_exp", type="datetime", nullable=true) |
||
176 | */ |
||
177 | private $dtExp; |
||
178 | |||
179 | /** |
||
180 | * @var \DateTime |
||
181 | * |
||
182 | * @ORM\Column(name="dt_gruen", type="datetime", nullable=true) |
||
183 | */ |
||
184 | private $dtGruen; |
||
185 | |||
186 | /** |
||
187 | * @var \DateTime |
||
188 | * |
||
189 | * @ORM\Column(name="dt_backup", type="datetime", nullable=true) |
||
190 | */ |
||
191 | private $dtBackup; |
||
192 | |||
193 | /** |
||
194 | * @param string $donorFullName |
||
195 | * |
||
196 | * @return self |
||
197 | */ |
||
198 | public function setDonorFullName( $donorFullName ) { |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getDonorFullName() { |
||
210 | |||
211 | /** |
||
212 | * @param string $donorCity |
||
213 | * |
||
214 | * @return self |
||
215 | */ |
||
216 | public function setDonorCity( $donorCity ) { |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getDonorCity() { |
||
228 | |||
229 | /** |
||
230 | * @param string $donorEmail |
||
231 | * |
||
232 | * @return self |
||
233 | */ |
||
234 | public function setDonorEmail( $donorEmail ) { |
||
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getDonorEmail() { |
||
246 | |||
247 | /** |
||
248 | * @param boolean $donorOptsIntoNewsletter |
||
249 | * |
||
250 | * @return self |
||
251 | */ |
||
252 | public function setDonorOptsIntoNewsletter( $donorOptsIntoNewsletter ) { |
||
257 | |||
258 | /** |
||
259 | * @return boolean |
||
260 | */ |
||
261 | public function getDonorOptsIntoNewsletter() { |
||
264 | |||
265 | /** |
||
266 | * Set donation receipt state |
||
267 | * |
||
268 | * @param boolean $donationReceipt |
||
269 | * @return self |
||
270 | */ |
||
271 | public function setDonationReceipt( $donationReceipt ) { |
||
276 | |||
277 | /** |
||
278 | * Get donation receipt state |
||
279 | * |
||
280 | * @return boolean |
||
281 | */ |
||
282 | public function getDonationReceipt() { |
||
285 | |||
286 | /** |
||
287 | * Set publicly displayed donation record |
||
288 | * |
||
289 | * @param string $publicRecord |
||
290 | * @return self |
||
291 | */ |
||
292 | public function setPublicRecord( $publicRecord ) { |
||
297 | |||
298 | /** |
||
299 | * Get publicly displayed donation record |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getPublicRecord() { |
||
306 | |||
307 | /** |
||
308 | * @param string $amount |
||
309 | * @return self |
||
310 | */ |
||
311 | public function setAmount( $amount ) { |
||
316 | |||
317 | /** |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getAmount() { |
||
323 | |||
324 | /** |
||
325 | * @param integer $paymentIntervalInMonths |
||
326 | * |
||
327 | * @return self |
||
328 | */ |
||
329 | public function setPaymentIntervalInMonths( $paymentIntervalInMonths ) { |
||
334 | |||
335 | /** |
||
336 | * @return integer |
||
337 | */ |
||
338 | public function getPaymentIntervalInMonths() { |
||
341 | |||
342 | /** |
||
343 | * Set payment type short code |
||
344 | * |
||
345 | * @param string $paymentType |
||
346 | * @return self |
||
347 | */ |
||
348 | public function setPaymentType( $paymentType ) { |
||
353 | |||
354 | /** |
||
355 | * Get payment type short code |
||
356 | * |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getPaymentType() { |
||
362 | |||
363 | /** |
||
364 | * @param string $comment |
||
365 | * @return self |
||
366 | */ |
||
367 | public function setComment( $comment ) { |
||
372 | |||
373 | /** |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getComment() { |
||
379 | |||
380 | /** |
||
381 | * Set bank transfer reference code |
||
382 | * |
||
383 | * @param string $bankTransferCode |
||
384 | * |
||
385 | * @return self |
||
386 | */ |
||
387 | public function setBankTransferCode( $bankTransferCode ) { |
||
392 | |||
393 | /** |
||
394 | * Get bank transfer reference code |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getBankTransferCode() { |
||
401 | |||
402 | /** |
||
403 | * @param string $source |
||
404 | * @return self |
||
405 | */ |
||
406 | public function setSource( $source ) { |
||
411 | |||
412 | /** |
||
413 | * @return string |
||
414 | */ |
||
415 | public function getSource() { |
||
418 | |||
419 | /** |
||
420 | * @param string $remoteAddr |
||
421 | * @return self |
||
422 | */ |
||
423 | public function setRemoteAddr( $remoteAddr ) { |
||
428 | |||
429 | /** |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getRemoteAddr() { |
||
435 | |||
436 | /** |
||
437 | * @param string $hash |
||
438 | * @return self |
||
439 | */ |
||
440 | public function setHash( $hash ) { |
||
445 | |||
446 | /** |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getHash() { |
||
452 | |||
453 | /** |
||
454 | * Sets if the donations comment should be public or private. |
||
455 | * @param boolean $isPublic |
||
456 | * @return self |
||
457 | */ |
||
458 | public function setIsPublic( $isPublic ) { |
||
463 | |||
464 | /** |
||
465 | * Gets if the donations comment is public or private. |
||
466 | * @return boolean |
||
467 | */ |
||
468 | public function getIsPublic() { |
||
471 | |||
472 | /** |
||
473 | * @param \DateTime $creationTime |
||
474 | * |
||
475 | * @return self |
||
476 | */ |
||
477 | public function setCreationTime( $creationTime ) { |
||
482 | |||
483 | /** |
||
484 | * @return \DateTime |
||
485 | */ |
||
486 | public function getCreationTime() { |
||
489 | |||
490 | /** |
||
491 | * @param \DateTime|null $deletionTime |
||
492 | * |
||
493 | * @return self |
||
494 | */ |
||
495 | public function setDeletionTime( $deletionTime ) { |
||
500 | |||
501 | /** |
||
502 | * @return \DateTime|null |
||
503 | */ |
||
504 | public function getDeletionTime() { |
||
507 | |||
508 | /** |
||
509 | * @param \DateTime $dtExp |
||
510 | * @return self |
||
511 | */ |
||
512 | public function setDtExp( $dtExp ) { |
||
517 | |||
518 | /** |
||
519 | * @return \DateTime |
||
520 | */ |
||
521 | public function getDtExp() { |
||
524 | |||
525 | /** |
||
526 | * @param string $status |
||
527 | * @return self |
||
528 | */ |
||
529 | public function setStatus( $status ) { |
||
534 | |||
535 | /** |
||
536 | * @return string |
||
537 | */ |
||
538 | public function getStatus() { |
||
541 | |||
542 | /** |
||
543 | * @param \DateTime $dtGruen |
||
544 | * @return self |
||
545 | */ |
||
546 | public function setDtGruen( $dtGruen ) { |
||
551 | |||
552 | /** |
||
553 | * @return \DateTime |
||
554 | */ |
||
555 | public function getDtGruen() { |
||
558 | |||
559 | /** |
||
560 | * @param \DateTime $dtBackup |
||
561 | * @return self |
||
562 | */ |
||
563 | public function setDtBackup( $dtBackup ) { |
||
568 | |||
569 | /** |
||
570 | * @return \DateTime |
||
571 | */ |
||
572 | public function getDtBackup() { |
||
575 | |||
576 | /** |
||
577 | * @return integer|null |
||
578 | */ |
||
579 | 3 | public function getId() { |
|
582 | |||
583 | /** |
||
584 | * @since 2.0 |
||
585 | * |
||
586 | * @param integer|null $id |
||
587 | */ |
||
588 | 2 | public function setId( $id ) { |
|
591 | |||
592 | public function getUExpiry() { |
||
595 | |||
596 | public function uTokenIsExpired() { |
||
599 | |||
600 | public function validateToken( $tokenToCheck, $serverSecret ) { |
||
609 | |||
610 | public function getEntryType( $mode = null ) { |
||
632 | |||
633 | /** |
||
634 | * @deprecated since 2.0, use encodeAndSetData or setDataObject instead |
||
635 | * |
||
636 | * @param string $data Base 64 encoded, serialized PHP array |
||
637 | * @return self |
||
638 | */ |
||
639 | public function setData( $data ) { |
||
644 | |||
645 | /** |
||
646 | * @deprecated since 2.0, use @see getDecodedData or @see getDataObject instead |
||
647 | * |
||
648 | * @return string Base 64 encoded, serialized PHP array |
||
649 | */ |
||
650 | public function getData() { |
||
653 | |||
654 | /** |
||
655 | * NOTE: if possible, use @see getDataObject instead, as it provides a nicer API. |
||
656 | * |
||
657 | * @since 2.0 |
||
658 | * @return array |
||
659 | */ |
||
660 | 8 | public function getDecodedData() { |
|
669 | |||
670 | /** |
||
671 | * NOTE: if possible, use @see modifyDataObject instead, as it provides a nicer API. |
||
672 | * |
||
673 | * @since 2.0 |
||
674 | * @param array $data |
||
675 | */ |
||
676 | 6 | public function encodeAndSetData( array $data ) { |
|
679 | |||
680 | /** |
||
681 | * WARNING: updates made to the return value will not be reflected in the Donation state. |
||
682 | * Similarly, updates to the Donation state will not propagate to the returned object. |
||
683 | * To update the Donation state, explicitly call @see setDataObject. |
||
684 | * |
||
685 | * @since 2.0 |
||
686 | * @return DonationData |
||
687 | */ |
||
688 | 3 | public function getDataObject() { |
|
699 | |||
700 | /** |
||
701 | * @since 2.0 |
||
702 | * @param DonationData $data |
||
703 | */ |
||
704 | 4 | public function setDataObject( DonationData $data ) { |
|
722 | |||
723 | /** |
||
724 | * @since 2.0 |
||
725 | * @param callable $modificationFunction Takes a modifiable DonationData parameter |
||
726 | */ |
||
727 | 1 | public function modifyDataObject( callable $modificationFunction ) { |
|
732 | |||
733 | } |
||
734 |