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; |
||
| 23 | use MetadataTrait; |
||
| 24 | |||
| 25 | const BILLING_CHARGE_AUTOMATICALLY = 'charge_automatically'; |
||
| 26 | const BILLING_SEND_INVOICE = 'send_invoice'; |
||
| 27 | |||
| 28 | const STATUS_EXPIRED = 'incomplete_expired'; |
||
| 29 | const STATUS_INCOMPLETE = 'incomplete'; |
||
| 30 | const STATUS_TRIALING = 'trialing'; |
||
| 31 | const STATUS_ACTIVE = 'active'; |
||
| 32 | const STATUS_PAST_DUE = 'past_due'; |
||
| 33 | const STATUS_CANCELED = 'canceled'; |
||
| 34 | const STATUS_UNPAID = 'unpaid'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $id; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ?float |
||
| 43 | */ |
||
| 44 | private $applicationFeePercent; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $billing; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var mixed |
||
| 53 | */ |
||
| 54 | private $billingCycleAnchor; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | private $cancelAtPeriodEnd; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ?\DateTimeImmutable |
||
| 63 | */ |
||
| 64 | private $canceledAt; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \DateTimeImmutable |
||
| 68 | */ |
||
| 69 | private $createdAt; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \DateTimeImmutable |
||
| 73 | */ |
||
| 74 | private $currentPeriodEndAt; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \DateTimeImmutable |
||
| 78 | */ |
||
| 79 | private $currentPeriodStartAt; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $customer; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var ?int |
||
| 88 | */ |
||
| 89 | private $daysUntilDue; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var ?string |
||
| 93 | */ |
||
| 94 | private $defaultSource; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private $defaultTaxRates; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var ?Discount |
||
| 103 | */ |
||
| 104 | private $discount; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var ?\DateTimeImmutable |
||
| 108 | */ |
||
| 109 | private $endedAt; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var ItemCollection |
||
| 113 | */ |
||
| 114 | private $items; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var ?string |
||
| 118 | */ |
||
| 119 | private $latestInvoiceId; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var ?string |
||
| 123 | */ |
||
| 124 | private $pendingSetupIntentId; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var Plan |
||
| 128 | */ |
||
| 129 | private $plan; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | private $quantity; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var \DateTimeImmutable |
||
| 138 | */ |
||
| 139 | private $startAt; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | private $status; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var ?\DateTimeImmutable |
||
| 148 | */ |
||
| 149 | private $trialEndAt; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var ?\DateTimeImmutable |
||
| 153 | */ |
||
| 154 | private $trialStartAt; |
||
| 155 | |||
| 156 | 8 | public static function createFromArray(array $data): self |
|
| 188 | |||
| 189 | 1 | public function isBilledAutomatically(): bool |
|
| 193 | |||
| 194 | public function isIncomplete(): bool |
||
| 198 | |||
| 199 | public function isExpired(): bool |
||
| 203 | |||
| 204 | public function isTrialing(): bool |
||
| 208 | |||
| 209 | public function isActive(): bool |
||
| 213 | |||
| 214 | public function isPastDue(): bool |
||
| 218 | |||
| 219 | public function isCanceled(): bool |
||
| 223 | |||
| 224 | public function isUnpaid(): bool |
||
| 228 | |||
| 229 | public function hasDefaultSource(): bool |
||
| 233 | |||
| 234 | 1 | public function getId(): string |
|
| 238 | |||
| 239 | 1 | public function getApplicationFeePercent(): ?float |
|
| 243 | |||
| 244 | 1 | public function getBilling(): string |
|
| 248 | |||
| 249 | 1 | public function getBillingCycleAnchor() |
|
| 253 | |||
| 254 | 1 | public function willBeCanceledAtPeriodEnd(): bool |
|
| 258 | |||
| 259 | 1 | public function getCanceledAt(): ?\DateTimeImmutable |
|
| 263 | |||
| 264 | 1 | public function getCreatedAt(): \DateTimeImmutable |
|
| 268 | |||
| 269 | 1 | public function getCurrentPeriodEndAt(): \DateTimeImmutable |
|
| 273 | |||
| 274 | 1 | public function getCurrentPeriodStartAt(): \DateTimeImmutable |
|
| 278 | |||
| 279 | 1 | public function getCustomer(): string |
|
| 283 | |||
| 284 | 1 | public function getDaysUntilDue(): ?int |
|
| 288 | |||
| 289 | 1 | public function getDefaultSource(): ?string |
|
| 293 | |||
| 294 | 1 | public function getDefaultTaxRates(): array |
|
| 298 | |||
| 299 | 1 | public function getDiscount(): ?Discount |
|
| 303 | |||
| 304 | 1 | public function hasDiscount(): bool |
|
| 308 | |||
| 309 | 1 | public function hasActiveDiscountForCurrentPeriod(): bool |
|
| 328 | |||
| 329 | 1 | public function getEndedAt(): ?\DateTimeImmutable |
|
| 333 | |||
| 334 | 1 | public function getItems(): ItemCollection |
|
| 338 | |||
| 339 | 1 | public function getLatestInvoiceId(): ?string |
|
| 343 | |||
| 344 | 1 | public function getPendingSetupIntentId(): ?string |
|
| 348 | |||
| 349 | public function getPlan(): Plan |
||
| 353 | |||
| 354 | 1 | public function getQuantity(): int |
|
| 358 | |||
| 359 | 1 | public function getStartAt(): \DateTimeImmutable |
|
| 363 | |||
| 364 | 1 | public function getStatus(): string |
|
| 368 | |||
| 369 | 1 | public function getTrialEndAt(): ?\DateTimeImmutable |
|
| 373 | |||
| 374 | 1 | public function getTrialStartAt(): ?\DateTimeImmutable |
|
| 378 | } |
||
| 379 |
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..