Complex classes like Product 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 Product, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Product extends Entity |
||
| 8 | { |
||
| 9 | const TYPE_FREE_PRICE = 'FREE_PRICE'; |
||
| 10 | const TYPE_FIXED_PRICE = 'FIXED_PRICE'; |
||
| 11 | const TYPE_SUBSCRIBE = 'SUBSCRIBE'; |
||
| 12 | |||
| 13 | private $status; |
||
| 14 | private $type; |
||
| 15 | private $price; |
||
| 16 | private $description; |
||
| 17 | private $allowRandomPrice; |
||
| 18 | private $fields; |
||
| 19 | private $currencyId; |
||
| 20 | private $siteId; |
||
| 21 | private $site; |
||
| 22 | private $notifyUrl; |
||
| 23 | private $workThrough; |
||
| 24 | private $confirmPaymentType; |
||
| 25 | private $notifyEmail; |
||
| 26 | private $messengerNotifyUrl; |
||
| 27 | private $supportEmail; |
||
| 28 | private $supportPhone; |
||
| 29 | private $accessUrl; |
||
| 30 | private $secretWord; |
||
| 31 | private $allowRebill; |
||
| 32 | private $UUID; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Payment[] |
||
| 36 | */ |
||
| 37 | private $payments; |
||
| 38 | |||
| 39 | |||
| 40 | public function getType(): ?string |
||
| 44 | |||
| 45 | public function setType(?string $type): void |
||
| 49 | |||
| 50 | public function getPrice(): ?float |
||
| 54 | |||
| 55 | public function setPrice(?float $price): void |
||
| 59 | |||
| 60 | public function getDescription(): ?string |
||
| 64 | |||
| 65 | public function setDescription(?string $description): void |
||
| 69 | |||
| 70 | public function getAllowRandomPrice(): bool |
||
| 74 | |||
| 75 | public function setAllowRandomPrice(bool $allowRandomPrice): void |
||
| 79 | |||
| 80 | public function getFields(): ?string |
||
| 84 | |||
| 85 | public function setFields(?string $fields): void |
||
| 89 | |||
| 90 | public function getCurrencyId(): ?int |
||
| 94 | |||
| 95 | public function setCurrencyId(?int $currencyId): void |
||
| 99 | |||
| 100 | public function getSiteId(): ?int |
||
| 104 | |||
| 105 | public function setSiteId(?int $siteId): void |
||
| 109 | |||
| 110 | public function getSite(): ?Site |
||
| 114 | |||
| 115 | public function setSite(?Site $site): void |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return Payment[] |
||
| 122 | */ |
||
| 123 | public function getPayments(): array |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param Payment[] $payments |
||
| 130 | */ |
||
| 131 | public function setPayments(array $payments) |
||
| 135 | |||
| 136 | public function getStatus(): ?string |
||
| 140 | |||
| 141 | public function setStatus(?string $status) |
||
| 145 | |||
| 146 | public function getNotifyUrl(): ?string |
||
| 150 | |||
| 151 | public function setNotifyUrl(?string $notifyUrl) |
||
| 155 | |||
| 156 | public function getWorkThrough():?string |
||
| 160 | |||
| 161 | public function setWorkThrough(?string $workThrough) |
||
| 165 | |||
| 166 | public function getConfirmPaymentType(): string |
||
| 170 | |||
| 171 | public function setConfirmPaymentType(string $confirmPaymentType) |
||
| 175 | |||
| 176 | public function getNotifyEmail(): ?string |
||
| 180 | |||
| 181 | public function setNotifyEmail(?string $notifyEmail) |
||
| 185 | |||
| 186 | public function getMessengerNotifyUrl(): ?string |
||
| 190 | |||
| 191 | public function setMessengerNotifyUrl(?string $messengerNotifyUrl) |
||
| 195 | |||
| 196 | public function getSupportEmail(): ?string |
||
| 200 | |||
| 201 | public function setSupportEmail(?string $supportEmail) |
||
| 205 | |||
| 206 | public function getSupportPhone(): ?string |
||
| 210 | |||
| 211 | public function setSupportPhone(?string $supportPhone) |
||
| 215 | |||
| 216 | public function getAccessUrl(): ?string |
||
| 220 | |||
| 221 | public function setAccessUrl(?string $accessUrl) |
||
| 225 | |||
| 226 | public function getSecretWord(): ?string |
||
| 230 | |||
| 231 | public function setSecretWord(?string $secretWord) |
||
| 235 | |||
| 236 | public function getAllowRebill():?int |
||
| 240 | |||
| 241 | public function setAllowRebill(?int $allowRebill) |
||
| 245 | |||
| 246 | public function getUUID(): ?string |
||
| 250 | |||
| 251 | public function setUUID(?string $UUID) |
||
| 255 | } |
||
| 256 |