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 $collectionMethod; |
||
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 | public function getBilling(): string |
||
250 | |||
251 | 1 | public function getCollectionMethod(): string |
|
255 | |||
256 | 1 | public function getBillingCycleAnchor() |
|
260 | |||
261 | 1 | public function willBeCanceledAtPeriodEnd(): bool |
|
265 | |||
266 | 1 | public function getCanceledAt(): ?\DateTimeImmutable |
|
270 | |||
271 | 1 | public function getCreatedAt(): \DateTimeImmutable |
|
275 | |||
276 | 1 | public function getCurrentPeriodEndAt(): \DateTimeImmutable |
|
280 | |||
281 | 1 | public function getCurrentPeriodStartAt(): \DateTimeImmutable |
|
285 | |||
286 | 1 | public function getCustomer(): string |
|
290 | |||
291 | 1 | public function getDaysUntilDue(): ?int |
|
295 | |||
296 | 1 | public function getDefaultSource(): ?string |
|
300 | |||
301 | 1 | public function getDefaultTaxRates(): array |
|
305 | |||
306 | 1 | public function getDiscount(): ?Discount |
|
310 | |||
311 | 1 | public function hasDiscount(): bool |
|
315 | |||
316 | 1 | public function hasActiveDiscountForCurrentPeriod(): bool |
|
335 | |||
336 | 1 | public function getEndedAt(): ?\DateTimeImmutable |
|
340 | |||
341 | 1 | public function getItems(): ItemCollection |
|
345 | |||
346 | 1 | public function getLatestInvoiceId(): ?string |
|
350 | |||
351 | 1 | public function getPendingSetupIntentId(): ?string |
|
355 | |||
356 | public function getPlan(): Plan |
||
360 | |||
361 | 1 | public function getQuantity(): int |
|
365 | |||
366 | 1 | public function getStartAt(): \DateTimeImmutable |
|
370 | |||
371 | 1 | public function getStatus(): string |
|
375 | |||
376 | 1 | public function getTrialEndAt(): ?\DateTimeImmutable |
|
380 | |||
381 | 1 | public function getTrialStartAt(): ?\DateTimeImmutable |
|
385 | } |
||
386 |
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..