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_EXPIRED = 'incomplete_expired'; |
||
| 28 | const STATUS_INCOMPLETE = 'incomplete'; |
||
| 29 | const STATUS_TRIALING = 'trialing'; |
||
| 30 | const STATUS_ACTIVE = 'active'; |
||
| 31 | const STATUS_PAST_DUE = 'past_due'; |
||
| 32 | const STATUS_CANCELED = 'canceled'; |
||
| 33 | const STATUS_UNPAID = 'unpaid'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $id; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ?float |
||
| 42 | */ |
||
| 43 | private $applicationFeePercent; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $billing; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var mixed |
||
| 52 | */ |
||
| 53 | private $billingCycleAnchor; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $cancelAtPeriodEnd; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var ?\DateTimeImmutable |
||
| 62 | */ |
||
| 63 | private $canceledAt; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \DateTimeImmutable |
||
| 67 | */ |
||
| 68 | private $createdAt; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var \DateTimeImmutable |
||
| 72 | */ |
||
| 73 | private $currentPeriodEndAt; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var \DateTimeImmutable |
||
| 77 | */ |
||
| 78 | private $currentPeriodStartAt; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private $customer; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var ?int |
||
| 87 | */ |
||
| 88 | private $daysUntilDue; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var ?string |
||
| 92 | */ |
||
| 93 | private $defaultSource; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var ?Discount |
||
| 97 | */ |
||
| 98 | private $discount; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var ?\DateTimeImmutable |
||
| 102 | */ |
||
| 103 | private $endedAt; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var ItemCollection |
||
| 107 | */ |
||
| 108 | private $items; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var Plan |
||
| 112 | */ |
||
| 113 | private $plan; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var int |
||
| 117 | */ |
||
| 118 | private $quantity; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var \DateTimeImmutable |
||
| 122 | */ |
||
| 123 | private $startAt; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | private $status; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var ?float |
||
| 132 | */ |
||
| 133 | private $taxPercent; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var ?\DateTimeImmutable |
||
| 137 | */ |
||
| 138 | private $trialEndAt; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var ?\DateTimeImmutable |
||
| 142 | */ |
||
| 143 | private $trialStartAt; |
||
| 144 | |||
| 145 | 8 | public static function createFromArray(array $data): self |
|
| 175 | |||
| 176 | 1 | public function isBilledAutomatically(): bool |
|
| 180 | |||
| 181 | public function isIncomplete(): bool |
||
| 185 | |||
| 186 | public function isExpired(): bool |
||
| 190 | |||
| 191 | public function isTrialing(): bool |
||
| 195 | |||
| 196 | public function isActive(): bool |
||
| 200 | |||
| 201 | public function isPastDue(): bool |
||
| 205 | |||
| 206 | public function isCanceled(): bool |
||
| 210 | |||
| 211 | public function isUnpaid(): bool |
||
| 215 | |||
| 216 | public function hasDefaultSource(): bool |
||
| 220 | |||
| 221 | 1 | public function getId(): string |
|
| 225 | |||
| 226 | 1 | public function getApplicationFeePercent(): ?float |
|
| 230 | |||
| 231 | 1 | public function getBilling(): string |
|
| 235 | |||
| 236 | 1 | public function getBillingCycleAnchor() |
|
| 240 | |||
| 241 | 1 | public function willBeCanceledAtPeriodEnd(): bool |
|
| 245 | |||
| 246 | 1 | public function getCanceledAt(): ?\DateTimeImmutable |
|
| 250 | |||
| 251 | 1 | public function getCreatedAt(): \DateTimeImmutable |
|
| 255 | |||
| 256 | 1 | public function getCurrentPeriodEndAt(): \DateTimeImmutable |
|
| 260 | |||
| 261 | 1 | public function getCurrentPeriodStartAt(): \DateTimeImmutable |
|
| 265 | |||
| 266 | 1 | public function getCustomer(): string |
|
| 270 | |||
| 271 | 1 | public function getDaysUntilDue(): ?int |
|
| 275 | |||
| 276 | 1 | public function getDefaultSource(): ?string |
|
| 280 | |||
| 281 | 1 | public function getDiscount(): ?Discount |
|
| 285 | |||
| 286 | 1 | public function hasDiscount(): bool |
|
| 290 | |||
| 291 | 1 | public function hasActiveDiscountForCurrentPeriod(): bool |
|
| 310 | |||
| 311 | 1 | public function getEndedAt(): ?\DateTimeImmutable |
|
| 315 | |||
| 316 | 1 | public function getItems(): ItemCollection |
|
| 320 | |||
| 321 | public function getPlan(): Plan |
||
| 325 | |||
| 326 | 1 | public function getQuantity(): int |
|
| 330 | |||
| 331 | 1 | public function getStartAt(): \DateTimeImmutable |
|
| 335 | |||
| 336 | 1 | public function getStatus(): string |
|
| 340 | |||
| 341 | 1 | public function getTaxPercent(): ?float |
|
| 345 | |||
| 346 | 1 | public function getTrialEndAt(): ?\DateTimeImmutable |
|
| 350 | |||
| 351 | 1 | public function getTrialStartAt(): ?\DateTimeImmutable |
|
| 355 | } |
||
| 356 |
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..