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. 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 Subscription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | final class Subscription implements CreatableFromArray, ContainsMetadata |
||
| 21 | { |
||
| 22 | use LivemodeTrait, MetadataTrait; |
||
| 23 | |||
| 24 | const BILLING_CHARGE_AUTOMATICALLY = 'charge_automatically'; |
||
| 25 | const BILLING_SEND_INVOICE = 'send_invoice'; |
||
| 26 | |||
| 27 | const STATUS_TRIALING = 'trialing'; |
||
| 28 | const STATUS_ACTIVE = 'active'; |
||
| 29 | const STATUS_PAST_DUE = 'past_due'; |
||
| 30 | const STATUS_CANCELED = 'canceled'; |
||
| 31 | const STATUS_UNPAID = 'unpaid'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $id; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ?float |
||
| 40 | */ |
||
| 41 | private $applicationFeePercent; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $billing; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var mixed |
||
| 50 | */ |
||
| 51 | private $billingCycleAnchor; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | private $cancelAtPeriodEnd; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ?\DateTimeImmutable |
||
| 60 | */ |
||
| 61 | private $canceledAt; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \DateTimeImmutable |
||
| 65 | */ |
||
| 66 | private $createdAt; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \DateTimeImmutable |
||
| 70 | */ |
||
| 71 | private $currentPeriodEndAt; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var \DateTimeImmutable |
||
| 75 | */ |
||
| 76 | private $currentPeriodStartAt; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $customer; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var ?int |
||
| 85 | */ |
||
| 86 | private $daysUntilDue; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var ?string |
||
| 90 | */ |
||
| 91 | private $defaultSource; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var ?Discount |
||
| 95 | */ |
||
| 96 | private $discount; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var ?\DateTimeImmutable |
||
| 100 | */ |
||
| 101 | private $endedAt; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var ItemCollection |
||
| 105 | */ |
||
| 106 | private $items; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var Plan |
||
| 110 | */ |
||
| 111 | private $plan; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var int |
||
| 115 | */ |
||
| 116 | private $quantity; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var \DateTimeImmutable |
||
| 120 | */ |
||
| 121 | private $startAt; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | private $status; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var ?float |
||
| 130 | */ |
||
| 131 | private $taxPercent; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var ?\DateTimeImmutable |
||
| 135 | */ |
||
| 136 | private $trialEndAt; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var ?\DateTimeImmutable |
||
| 140 | */ |
||
| 141 | private $trialStartAt; |
||
| 142 | |||
| 143 | 8 | public static function createFromArray(array $data): self |
|
| 173 | |||
| 174 | 1 | public function isBilledAutomatically(): bool |
|
| 178 | |||
| 179 | public function isTrialing(): bool |
||
| 183 | |||
| 184 | public function isActive(): bool |
||
| 188 | |||
| 189 | public function isPastDue(): bool |
||
| 193 | |||
| 194 | public function isCanceled(): bool |
||
| 198 | |||
| 199 | public function isUnpaid(): bool |
||
| 203 | |||
| 204 | public function hasDefaultSource(): bool |
||
| 208 | |||
| 209 | 1 | public function getId(): string |
|
| 213 | |||
| 214 | 1 | public function getApplicationFeePercent(): ?float |
|
| 218 | |||
| 219 | 1 | public function getBilling(): string |
|
| 223 | |||
| 224 | 1 | public function getBillingCycleAnchor() |
|
| 228 | |||
| 229 | 1 | public function willBeCanceledAtPeriodEnd(): bool |
|
| 233 | |||
| 234 | 1 | public function getCanceledAt(): ?\DateTimeImmutable |
|
| 238 | |||
| 239 | 1 | public function getCreatedAt(): \DateTimeImmutable |
|
| 243 | |||
| 244 | 1 | public function getCurrentPeriodEndAt(): \DateTimeImmutable |
|
| 248 | |||
| 249 | 1 | public function getCurrentPeriodStartAt(): \DateTimeImmutable |
|
| 253 | |||
| 254 | 1 | public function getCustomer(): string |
|
| 258 | |||
| 259 | 1 | public function getDaysUntilDue(): ?int |
|
| 263 | |||
| 264 | 1 | public function getDefaultSource(): ?string |
|
| 268 | |||
| 269 | 1 | public function getDiscount(): ?Discount |
|
| 273 | |||
| 274 | 1 | public function hasDiscount(): bool |
|
| 278 | |||
| 279 | 1 | public function hasActiveDiscountForCurrentPeriod(): bool |
|
| 298 | |||
| 299 | 1 | public function getEndedAt(): ?\DateTimeImmutable |
|
| 303 | |||
| 304 | 1 | public function getItems(): ItemCollection |
|
| 308 | |||
| 309 | public function getPlan(): Plan |
||
| 313 | |||
| 314 | 1 | public function getQuantity(): int |
|
| 318 | |||
| 319 | 1 | public function getStartAt(): \DateTimeImmutable |
|
| 323 | |||
| 324 | 1 | public function getStatus(): string |
|
| 328 | |||
| 329 | 1 | public function getTaxPercent(): ?float |
|
| 333 | |||
| 334 | 1 | public function getTrialEndAt(): ?\DateTimeImmutable |
|
| 338 | |||
| 339 | 1 | public function getTrialStartAt(): ?\DateTimeImmutable |
|
| 343 | } |
||
| 344 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..