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 |
||
| 21 | class Donation { |
||
| 22 | |||
| 23 | const STATUS_NEW = 'N'; // status for direct debit |
||
| 24 | const STATUS_PROMISE = 'Z'; // status for bank transfer |
||
| 25 | const STATUS_EXTERNAL_INCOMPLETE = 'X'; // status for external payments |
||
| 26 | const STATUS_EXTERNAL_BOOKED = 'B'; // status for external payments |
||
| 27 | const STATUS_MODERATION = 'P'; |
||
| 28 | const STATUS_CANCELLED = 'D'; |
||
| 29 | |||
| 30 | const OPTS_INTO_NEWSLETTER = true; |
||
| 31 | const DOES_NOT_OPT_INTO_NEWSLETTER = false; |
||
| 32 | |||
| 33 | const NO_APPLICANT = null; |
||
| 34 | |||
| 35 | private $id; |
||
| 36 | private $status; |
||
| 37 | private $donor; |
||
| 38 | private $payment; |
||
| 39 | private $optsIntoNewsletter; |
||
| 40 | private $comment; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * TODO: move out of Donation |
||
| 44 | */ |
||
| 45 | private $trackingInfo; |
||
| 46 | |||
| 47 | public function __construct( int $id = null, string $status, Donor $donor = null, DonationPayment $payment, |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return int|null |
||
| 61 | */ |
||
| 62 | public function getId() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param int $id |
||
| 68 | * @throws \RuntimeException |
||
| 69 | */ |
||
| 70 | public function assignId( int $id ) { |
||
| 77 | |||
| 78 | public function getStatus(): string { |
||
| 81 | |||
| 82 | public function getAmount(): Euro { |
||
| 85 | |||
| 86 | public function getPaymentIntervalInMonths(): int { |
||
| 89 | |||
| 90 | public function getPaymentType(): string { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the Donor or null for anonymous donations. |
||
| 96 | * |
||
| 97 | * @return Donor|null |
||
| 98 | */ |
||
| 99 | public function getDonor() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns the DonationComment or null for when there is none. |
||
| 105 | * |
||
| 106 | * @return DonationComment|null |
||
| 107 | */ |
||
| 108 | public function getComment() { |
||
| 111 | |||
| 112 | public function addComment( DonationComment $comment ) { |
||
| 119 | |||
| 120 | public function getPayment(): DonationPayment { |
||
| 123 | |||
| 124 | public function getPaymentMethod(): PaymentMethod { |
||
| 127 | |||
| 128 | public function getOptsIntoNewsletter(): bool { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @throws RuntimeException |
||
| 134 | */ |
||
| 135 | public function cancel() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @throws RuntimeException |
||
| 149 | */ |
||
| 150 | public function confirmBooked() { |
||
| 165 | |||
| 166 | private function makeCommentPrivate() { |
||
| 173 | |||
| 174 | public function hasComment(): bool { |
||
| 177 | |||
| 178 | private function statusAllowsForBooking(): bool { |
||
| 181 | |||
| 182 | public function markForModeration() { |
||
| 185 | |||
| 186 | public function getTrackingInfo(): DonationTrackingInfo { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param PayPalData $payPalData |
||
| 192 | * @throws RuntimeException |
||
| 193 | */ |
||
| 194 | public function addPayPalData( PayPalData $payPalData ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param CreditCardTransactionData $creditCardData |
||
| 206 | * |
||
| 207 | * @throws RuntimeException |
||
| 208 | */ |
||
| 209 | public function addCreditCardData( CreditCardTransactionData $creditCardData ) { |
||
| 218 | |||
| 219 | private function statusIsCancellable(): bool { |
||
| 222 | |||
| 223 | public function hasExternalPayment(): bool { |
||
| 226 | |||
| 227 | private function isIncomplete(): bool { |
||
| 230 | |||
| 231 | public function needsModeration(): bool { |
||
| 234 | |||
| 235 | public function isBooked(): bool { |
||
| 238 | |||
| 239 | public function isCancelled(): bool { |
||
| 242 | |||
| 243 | } |
||
| 244 |