Complex classes like AddDonationRequest 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 AddDonationRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class AddDonationRequest { |
||
| 17 | |||
| 18 | private $donorType; |
||
| 19 | private $donorFirstName; |
||
| 20 | private $donorLastName; |
||
| 21 | private $donorSalutation; |
||
| 22 | private $donorTitle; |
||
| 23 | private $donorCompany; |
||
| 24 | private $donorStreetAddress; |
||
| 25 | private $donorPostalCode; |
||
| 26 | private $donorCity; |
||
| 27 | private $donorCountryCode; |
||
| 28 | private $donorEmailAddress; |
||
| 29 | |||
| 30 | private $optIn = ''; |
||
| 31 | |||
| 32 | # donation |
||
| 33 | private $amount; |
||
| 34 | private $paymentType = ''; |
||
| 35 | private $interval = 0; |
||
| 36 | |||
| 37 | # direct debit related |
||
| 38 | private $bankData; |
||
| 39 | |||
| 40 | # tracking |
||
| 41 | private $tracking = ''; |
||
| 42 | private $source = ''; # TODO: generated from referer |
||
| 43 | private $totalImpressionCount = 0; |
||
| 44 | private $singleBannerImpressionCount = 0; |
||
| 45 | // Legacy values, will be deprecated in the future |
||
| 46 | private $color = ''; |
||
| 47 | private $skin = ''; |
||
| 48 | private $layout = ''; |
||
| 49 | |||
| 50 | public function getOptIn(): string { |
||
| 53 | |||
| 54 | public function setOptIn( string $optIn ) { |
||
| 57 | |||
| 58 | public function getAmount(): Euro { |
||
| 61 | |||
| 62 | public function setAmount( Euro $amount ) { |
||
| 65 | |||
| 66 | public function getPaymentType(): string { |
||
| 69 | |||
| 70 | public function setPaymentType( string $paymentType ) { |
||
| 73 | |||
| 74 | public function getInterval(): int { |
||
| 77 | |||
| 78 | public function setInterval( int $interval ) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return BankData|null |
||
| 84 | */ |
||
| 85 | public function getBankData() { |
||
| 88 | |||
| 89 | public function setBankData( BankData $bankData ) { |
||
| 92 | |||
| 93 | public function getTracking(): string { |
||
| 96 | |||
| 97 | public function setTracking( string $tracking ) { |
||
| 100 | |||
| 101 | public function getSource(): string { |
||
| 104 | |||
| 105 | public function setSource( string $source ) { |
||
| 108 | |||
| 109 | public function getTotalImpressionCount(): int { |
||
| 112 | |||
| 113 | public function setTotalImpressionCount( int $totalImpressionCount ) { |
||
| 116 | |||
| 117 | public function getSingleBannerImpressionCount(): int { |
||
| 120 | |||
| 121 | public function setSingleBannerImpressionCount( int $singleBannerImpressionCount ) { |
||
| 124 | |||
| 125 | /* |
||
| 126 | * @deprecated |
||
| 127 | */ |
||
| 128 | public function getColor(): string { |
||
| 131 | |||
| 132 | /* |
||
| 133 | * @deprecated |
||
| 134 | */ |
||
| 135 | public function getSkin(): string { |
||
| 138 | |||
| 139 | /* |
||
| 140 | * @deprecated |
||
| 141 | */ |
||
| 142 | public function getLayout(): string { |
||
| 145 | |||
| 146 | public function getDonorType(): string { |
||
| 149 | |||
| 150 | public function setDonorType( string $donorType ) { |
||
| 153 | |||
| 154 | public function getDonorFirstName(): string { |
||
| 157 | |||
| 158 | public function setDonorFirstName( string $donorFirstName ) { |
||
| 161 | |||
| 162 | public function getDonorLastName(): string { |
||
| 165 | |||
| 166 | public function setDonorLastName( string $donorLastName ) { |
||
| 169 | |||
| 170 | public function getDonorSalutation(): string { |
||
| 173 | |||
| 174 | public function setDonorSalutation( string $donorSalutation ) { |
||
| 177 | |||
| 178 | public function getDonorTitle(): string { |
||
| 181 | |||
| 182 | public function setDonorTitle( string $donorTitle ) { |
||
| 185 | |||
| 186 | public function getDonorCompany(): string { |
||
| 189 | |||
| 190 | public function setDonorCompany( string $donorCompany ) { |
||
| 193 | |||
| 194 | public function getDonorStreetAddress(): string { |
||
| 197 | |||
| 198 | public function setDonorStreetAddress( string $donorStreetAddress ) { |
||
| 201 | |||
| 202 | public function getDonorPostalCode(): string { |
||
| 205 | |||
| 206 | public function setDonorPostalCode( string $donorPostalCode ) { |
||
| 209 | |||
| 210 | public function getDonorCity(): string { |
||
| 213 | |||
| 214 | public function setDonorCity( string $donorCity ) { |
||
| 217 | |||
| 218 | public function getDonorCountryCode(): string { |
||
| 221 | |||
| 222 | public function setDonorCountryCode( string $donorCountryCode ) { |
||
| 225 | |||
| 226 | public function getDonorEmailAddress(): string { |
||
| 229 | |||
| 230 | public function setDonorEmailAddress( string $donorEmailAddress ) { |
||
| 233 | |||
| 234 | public static function getPreferredValue( array $values ) { |
||
| 243 | |||
| 244 | public static function concatTrackingFromVarCouple( string $campaign, string $keyword ): string { |
||
| 251 | |||
| 252 | public function donorIsAnonymous(): bool { |
||
| 255 | |||
| 256 | public function donorIsCompany(): bool { |
||
| 259 | } |