| Total Complexity | 42 |
| Total Lines | 256 |
| 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 $validUntil = null; |
||
| 55 | |||
| 56 | private \DateTimeInterface $updatedAt; |
||
| 57 | |||
| 58 | private ?\DateTimeInterface $endedAt = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return mixed |
||
| 62 | */ |
||
| 63 | public function getId() |
||
| 64 | { |
||
| 65 | return $this->id; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param mixed $id |
||
| 70 | */ |
||
| 71 | public function setId($id): void |
||
| 72 | { |
||
| 73 | $this->id = $id; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function getPlanName(): string |
||
| 77 | { |
||
| 78 | return $this->planName; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function setPlanName(string $planName): void |
||
| 82 | { |
||
| 83 | $this->planName = $planName; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getPaymentSchedule(): string |
||
| 87 | { |
||
| 88 | return $this->paymentSchedule; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function setPaymentSchedule(string $paymentSchedule): void |
||
| 92 | { |
||
| 93 | $this->paymentSchedule = $paymentSchedule; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getSeats(): ?int |
||
| 97 | { |
||
| 98 | return $this->seats; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function setSeats(?int $seats): void |
||
| 102 | { |
||
| 103 | $this->seats = $seats; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function getStatus(): string |
||
| 107 | { |
||
| 108 | return $this->status; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function setStatus(string $status): void |
||
| 112 | { |
||
| 113 | $this->status = $status; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function getSubscriptionPlan(): ?SubscriptionPlan |
||
| 117 | { |
||
| 118 | return $this->subscriptionPlan; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function setSubscriptionPlan(?SubscriptionPlan $subscriptionPlan): void |
||
| 122 | { |
||
| 123 | $this->subscriptionPlan = $subscriptionPlan; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function getMainExternalReference(): string |
||
| 127 | { |
||
| 128 | return $this->mainExternalReference; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function setMainExternalReference(string $mainExternalReference): void |
||
| 132 | { |
||
| 133 | $this->mainExternalReference = $mainExternalReference; |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getChildExternalReference(): string |
||
| 137 | { |
||
| 138 | return $this->childExternalReference; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function setChildExternalReference(string $childExternalReference): void |
||
| 142 | { |
||
| 143 | $this->childExternalReference = $childExternalReference; |
||
| 144 | } |
||
| 145 | |||
| 146 | public function getAmount(): ?int |
||
| 147 | { |
||
| 148 | return $this->amount; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function setAmount(?int $amount): void |
||
| 152 | { |
||
| 153 | $this->amount = $amount; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getCurrency(): ?string |
||
| 157 | { |
||
| 158 | return $this->currency; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function setCurrency(?string $currency): void |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getPrice(): ?Price |
||
| 167 | { |
||
| 168 | return $this->price; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function setPrice(?Price $price): void |
||
| 172 | { |
||
| 173 | $this->price = $price; |
||
| 174 | } |
||
| 175 | |||
| 176 | public function getCreatedAt(): \DateTimeInterface |
||
| 177 | { |
||
| 178 | return $this->createdAt; |
||
| 179 | } |
||
| 180 | |||
| 181 | public function setCreatedAt(\DateTimeInterface $createdAt): void |
||
| 182 | { |
||
| 183 | $this->createdAt = $createdAt; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function getUpdatedAt(): \DateTimeInterface |
||
| 187 | { |
||
| 188 | return $this->updatedAt; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function setUpdatedAt(\DateTimeInterface $updatedAt): void |
||
| 192 | { |
||
| 193 | $this->updatedAt = $updatedAt; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function getEndedAt(): ?\DateTimeInterface |
||
| 199 | } |
||
| 200 | |||
| 201 | public function setEndedAt(?\DateTimeInterface $endedAt): void |
||
| 202 | { |
||
| 203 | $this->endedAt = $endedAt; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function endAtEndOfPeriod(): void |
||
| 207 | { |
||
| 208 | $this->endedAt = clone $this->validUntil; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function endNow(): void |
||
| 212 | { |
||
| 213 | $this->endedAt = new \DateTime('now'); |
||
| 214 | $this->validUntil = new \DateTime('now'); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function isActive(): bool |
||
| 218 | { |
||
| 219 | return $this->active; |
||
| 220 | } |
||
| 221 | |||
| 222 | public function setActive(bool $active): void |
||
| 223 | { |
||
| 224 | $this->active = $active; |
||
| 225 | } |
||
| 226 | |||
| 227 | public function getValidUntil(): \DateTimeInterface |
||
| 228 | { |
||
| 229 | return $this->validUntil; |
||
|
|
|||
| 230 | } |
||
| 231 | |||
| 232 | public function setValidUntil(\DateTimeInterface $validUntil): void |
||
| 233 | { |
||
| 234 | $this->validUntil = $validUntil; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function getMoneyAmount(): Money |
||
| 240 | } |
||
| 241 | |||
| 242 | public function setMoneyAmount(Money $money) |
||
| 243 | { |
||
| 244 | $this->amount = $money->getAmount()->getUnscaledValue()->toInt(); |
||
| 245 | $this->currency = $money->getCurrency()->getCurrencyCode(); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function getCustomer(): CustomerInterface |
||
| 249 | { |
||
| 250 | return $this->customer; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function setCustomer(CustomerInterface $customer): void |
||
| 254 | { |
||
| 255 | $this->customer = $customer; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function getMainExternalReferenceDetailsUrl(): ?string |
||
| 259 | { |
||
| 260 | return $this->mainExternalReferenceDetailsUrl; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function setMainExternalReferenceDetailsUrl(?string $mainExternalReferenceDetailsUrl): void |
||
| 264 | { |
||
| 265 | $this->mainExternalReferenceDetailsUrl = $mainExternalReferenceDetailsUrl; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function getPaymentExternalReference(): string |
||
| 269 | { |
||
| 270 | return $this->paymentExternalReference; |
||
| 271 | } |
||
| 272 | |||
| 273 | public function setPaymentExternalReference(?string $paymentExternalReference): void |
||
| 276 | } |
||
| 277 | } |
||
| 278 |