| Total Complexity | 44 |
| Total Lines | 268 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Subscription 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.
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 Subscription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Subscription |
||
| 21 | { |
||
| 22 | private $id; |
||
| 23 | |||
| 24 | private CustomerInterface $customer; |
||
| 25 | |||
| 26 | private string $planName; |
||
| 27 | |||
| 28 | private string $paymentSchedule; |
||
| 29 | |||
| 30 | private ?int $seats = 1; |
||
| 31 | |||
| 32 | private bool $active; |
||
| 33 | |||
| 34 | private string $status; |
||
| 35 | |||
| 36 | private ?int $amount = null; |
||
| 37 | |||
| 38 | private ?string $currency = null; |
||
| 39 | |||
| 40 | private string $mainExternalReference; |
||
| 41 | |||
| 42 | private ?string $mainExternalReferenceDetailsUrl = null; |
||
| 43 | |||
| 44 | private string $childExternalReference; |
||
| 45 | |||
| 46 | private string $paymentExternalReference; |
||
| 47 | |||
| 48 | private ?SubscriptionPlan $subscriptionPlan = null; |
||
| 49 | |||
| 50 | private ?Price $price = null; |
||
| 51 | |||
| 52 | private \DateTimeInterface $createdAt; |
||
| 53 | |||
| 54 | private ?\DateTimeInterface $startOfCurrentPeriod = null; |
||
| 55 | |||
| 56 | private ?\DateTimeInterface $validUntil = null; |
||
| 57 | |||
| 58 | private \DateTimeInterface $updatedAt; |
||
| 59 | |||
| 60 | private ?\DateTimeInterface $endedAt = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return mixed |
||
| 64 | */ |
||
| 65 | public function getId() |
||
| 66 | { |
||
| 67 | return $this->id; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param mixed $id |
||
| 72 | */ |
||
| 73 | public function setId($id): void |
||
| 74 | { |
||
| 75 | $this->id = $id; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getPlanName(): string |
||
| 79 | { |
||
| 80 | return $this->planName; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function setPlanName(string $planName): void |
||
| 84 | { |
||
| 85 | $this->planName = $planName; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getPaymentSchedule(): string |
||
| 89 | { |
||
| 90 | return $this->paymentSchedule; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function setPaymentSchedule(string $paymentSchedule): void |
||
| 94 | { |
||
| 95 | $this->paymentSchedule = $paymentSchedule; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function getSeats(): ?int |
||
| 99 | { |
||
| 100 | return $this->seats; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function setSeats(?int $seats): void |
||
| 104 | { |
||
| 105 | $this->seats = $seats; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getStatus(): string |
||
| 109 | { |
||
| 110 | return $this->status; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function setStatus(string $status): void |
||
| 114 | { |
||
| 115 | $this->status = $status; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function getSubscriptionPlan(): ?SubscriptionPlan |
||
| 119 | { |
||
| 120 | return $this->subscriptionPlan; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function setSubscriptionPlan(?SubscriptionPlan $subscriptionPlan): void |
||
| 124 | { |
||
| 125 | $this->subscriptionPlan = $subscriptionPlan; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function getMainExternalReference(): string |
||
| 129 | { |
||
| 130 | return $this->mainExternalReference; |
||
| 131 | } |
||
| 132 | |||
| 133 | public function setMainExternalReference(string $mainExternalReference): void |
||
| 134 | { |
||
| 135 | $this->mainExternalReference = $mainExternalReference; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function getChildExternalReference(): string |
||
| 139 | { |
||
| 140 | return $this->childExternalReference; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function setChildExternalReference(string $childExternalReference): void |
||
| 144 | { |
||
| 145 | $this->childExternalReference = $childExternalReference; |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getAmount(): ?int |
||
| 149 | { |
||
| 150 | return $this->amount; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function setAmount(?int $amount): void |
||
| 154 | { |
||
| 155 | $this->amount = $amount; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getCurrency(): ?string |
||
| 159 | { |
||
| 160 | return $this->currency; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function setCurrency(?string $currency): void |
||
| 164 | { |
||
| 165 | $this->currency = $currency; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getPrice(): ?Price |
||
| 169 | { |
||
| 170 | return $this->price; |
||
| 171 | } |
||
| 172 | |||
| 173 | public function setPrice(?Price $price): void |
||
| 174 | { |
||
| 175 | $this->price = $price; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function getCreatedAt(): \DateTimeInterface |
||
| 179 | { |
||
| 180 | return $this->createdAt; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function setCreatedAt(\DateTimeInterface $createdAt): void |
||
| 184 | { |
||
| 185 | $this->createdAt = $createdAt; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getUpdatedAt(): \DateTimeInterface |
||
| 189 | { |
||
| 190 | return $this->updatedAt; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function setUpdatedAt(\DateTimeInterface $updatedAt): void |
||
| 194 | { |
||
| 195 | $this->updatedAt = $updatedAt; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getEndedAt(): ?\DateTimeInterface |
||
| 199 | { |
||
| 200 | return $this->endedAt; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function setEndedAt(?\DateTimeInterface $endedAt): void |
||
| 204 | { |
||
| 205 | $this->endedAt = $endedAt; |
||
| 206 | } |
||
| 207 | |||
| 208 | public function endAtEndOfPeriod(): void |
||
| 209 | { |
||
| 210 | $this->endedAt = clone $this->validUntil; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function endNow(): void |
||
| 214 | { |
||
| 215 | $this->endedAt = new \DateTime('now'); |
||
| 216 | $this->validUntil = new \DateTime('now'); |
||
| 217 | } |
||
| 218 | |||
| 219 | public function isActive(): bool |
||
| 220 | { |
||
| 221 | return $this->active; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function setActive(bool $active): void |
||
| 225 | { |
||
| 226 | $this->active = $active; |
||
| 227 | } |
||
| 228 | |||
| 229 | public function getValidUntil(): \DateTimeInterface |
||
| 230 | { |
||
| 231 | return $this->validUntil; |
||
|
|
|||
| 232 | } |
||
| 233 | |||
| 234 | public function setValidUntil(\DateTimeInterface $validUntil): void |
||
| 235 | { |
||
| 236 | $this->validUntil = $validUntil; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function getMoneyAmount(): Money |
||
| 242 | } |
||
| 243 | |||
| 244 | public function setMoneyAmount(Money $money) |
||
| 245 | { |
||
| 246 | $this->amount = $money->getAmount()->getUnscaledValue()->toInt(); |
||
| 247 | $this->currency = $money->getCurrency()->getCurrencyCode(); |
||
| 248 | } |
||
| 249 | |||
| 250 | public function getCustomer(): CustomerInterface |
||
| 251 | { |
||
| 252 | return $this->customer; |
||
| 253 | } |
||
| 254 | |||
| 255 | public function setCustomer(CustomerInterface $customer): void |
||
| 256 | { |
||
| 257 | $this->customer = $customer; |
||
| 258 | } |
||
| 259 | |||
| 260 | public function getMainExternalReferenceDetailsUrl(): ?string |
||
| 261 | { |
||
| 262 | return $this->mainExternalReferenceDetailsUrl; |
||
| 263 | } |
||
| 264 | |||
| 265 | public function setMainExternalReferenceDetailsUrl(?string $mainExternalReferenceDetailsUrl): void |
||
| 266 | { |
||
| 267 | $this->mainExternalReferenceDetailsUrl = $mainExternalReferenceDetailsUrl; |
||
| 268 | } |
||
| 269 | |||
| 270 | public function getPaymentExternalReference(): string |
||
| 271 | { |
||
| 272 | return $this->paymentExternalReference; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function setPaymentExternalReference(?string $paymentExternalReference): void |
||
| 278 | } |
||
| 279 | |||
| 280 | public function getStartOfCurrentPeriod(): ?\DateTimeInterface |
||
| 281 | { |
||
| 282 | return $this->startOfCurrentPeriod; |
||
| 283 | } |
||
| 284 | |||
| 285 | public function setStartOfCurrentPeriod(?\DateTimeInterface $startOfCurrentPeriod): void |
||
| 288 | } |
||
| 289 | } |
||
| 290 |