Complex classes like Invoice 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 Invoice, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class Invoice implements CreatableFromArray, ContainsMetadata |
||
| 23 | { |
||
| 24 | use LivemodeTrait, MetadataTrait; |
||
| 25 | |||
| 26 | const BILLING_CHARGE_AUTOMATICALLY = 'charge_automatically'; |
||
| 27 | const BILLING_SEND_INVOICE = 'send_invoice'; |
||
| 28 | |||
| 29 | const BILLING_REASON_SUBSCRIPTION_CYCLE = 'subscription_cycle'; |
||
| 30 | const BILLING_REASON_SUBSCRIPTION_CREATE = 'subscription_create'; |
||
| 31 | const BILLING_REASON_SUBSCRIPTION_UPDATE = 'subscription_update'; |
||
| 32 | const BILLING_REASON_SUBSCRIPTION = 'subscription'; |
||
| 33 | const BILLING_REASON_MANUAL = 'manual'; |
||
| 34 | const BILLING_REASON_UPCOMING = 'upcoming'; |
||
| 35 | const BILLING_REASON_SUBSCRIPTION_THRESHOLD = 'subscription_threshold'; |
||
| 36 | |||
| 37 | const STATUS_DRAFT = 'draft'; |
||
| 38 | const STATUS_OPEN = 'open'; |
||
| 39 | const STATUS_PAID = 'paid'; |
||
| 40 | const STATUS_UNCOLLECTIBLE = 'uncollectible'; |
||
| 41 | const STATUS_VOID = 'void'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ?string |
||
| 45 | */ |
||
| 46 | private $id; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var Money |
||
| 50 | */ |
||
| 51 | private $amountDue; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Money |
||
| 55 | */ |
||
| 56 | private $amountPaid; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Money |
||
| 60 | */ |
||
| 61 | private $amountRemaining; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ?Money |
||
| 65 | */ |
||
| 66 | private $applicationFeeAmount; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $attemptCount; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | private $attempted; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | private $autoAdvance; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $billing; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | private $billingReason; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var ?string |
||
| 95 | */ |
||
| 96 | private $charge; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var \DateTimeImmutable |
||
| 100 | */ |
||
| 101 | private $createdAt; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var Currency |
||
| 105 | */ |
||
| 106 | private $currency; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | private $customFields; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var string |
||
| 115 | */ |
||
| 116 | private $customer; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var ?string |
||
| 120 | */ |
||
| 121 | private $defaultSource; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var string |
||
| 125 | */ |
||
| 126 | private $description; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var ?Discount |
||
| 130 | */ |
||
| 131 | private $discount; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var ?\DateTimeImmutable |
||
| 135 | */ |
||
| 136 | private $dueAt; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var ?Money |
||
| 140 | */ |
||
| 141 | private $endingBalance; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var ?string |
||
| 145 | */ |
||
| 146 | private $footer; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var ?string |
||
| 150 | */ |
||
| 151 | private $hostedInvoiceUrl; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var ?string |
||
| 155 | */ |
||
| 156 | private $invoicePdf; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var LineItemCollection |
||
| 160 | */ |
||
| 161 | private $lines; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @var ?\DateTimeImmutable |
||
| 165 | */ |
||
| 166 | private $nextPaymentAttempt; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @var string |
||
| 170 | */ |
||
| 171 | private $number; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var bool |
||
| 175 | */ |
||
| 176 | private $paid; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var \DateTimeImmutable |
||
| 180 | */ |
||
| 181 | private $periodEndAt; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var \DateTimeImmutable |
||
| 185 | */ |
||
| 186 | private $periodStartAt; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var ?string |
||
| 190 | */ |
||
| 191 | private $receiptNumber; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var Money |
||
| 195 | */ |
||
| 196 | private $startingBalance; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @var ?string |
||
| 200 | */ |
||
| 201 | private $statementDescriptor; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @var string |
||
| 205 | */ |
||
| 206 | private $status; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var string |
||
| 210 | */ |
||
| 211 | private $subscription; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @var ?\DateTimeImmutable |
||
| 215 | */ |
||
| 216 | private $subscriptionProrationAt; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @var Money |
||
| 220 | */ |
||
| 221 | private $subtotal; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @var Money |
||
| 225 | */ |
||
| 226 | private $tax; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @var float |
||
| 230 | */ |
||
| 231 | private $taxPercent; |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @var ?ThresholdReason |
||
| 235 | */ |
||
| 236 | private $thresholdReason; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @var Money |
||
| 240 | */ |
||
| 241 | private $total; |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @var ?\DateTimeImmutable |
||
| 245 | */ |
||
| 246 | private $webhooksDeliveredAt; |
||
| 247 | |||
| 248 | 12 | public static function createFromArray(array $data): self |
|
| 306 | |||
| 307 | 1 | public function getId(): ?string |
|
| 311 | |||
| 312 | 1 | public function getAmountDue(): Money |
|
| 316 | |||
| 317 | 1 | public function getAmountPaid(): Money |
|
| 321 | |||
| 322 | 1 | public function getAmountRemaining(): Money |
|
| 326 | |||
| 327 | 1 | public function getApplicationFeeAmount(): ?Money |
|
| 331 | |||
| 332 | 1 | public function getAttemptCount(): int |
|
| 336 | |||
| 337 | 1 | public function isAttempted(): bool |
|
| 341 | |||
| 342 | 1 | public function isAutoAdvance(): bool |
|
| 346 | |||
| 347 | 1 | public function getBilling(): string |
|
| 351 | |||
| 352 | 1 | public function getBillingReason(): string |
|
| 356 | |||
| 357 | 1 | public function getCharge(): ?string |
|
| 361 | |||
| 362 | 1 | public function getCreatedAt(): \DateTimeImmutable |
|
| 366 | |||
| 367 | 1 | public function getCurrency(): Currency |
|
| 371 | |||
| 372 | 1 | public function getCustomFields(): array |
|
| 376 | |||
| 377 | 1 | public function getCustomer(): string |
|
| 381 | |||
| 382 | 1 | public function getDefaultSource(): ?string |
|
| 386 | |||
| 387 | 1 | public function getDescription(): ?string |
|
| 391 | |||
| 392 | 1 | public function getDiscount(): ?Discount |
|
| 396 | |||
| 397 | 1 | public function getDueAt(): ?\DateTimeImmutable |
|
| 401 | |||
| 402 | 1 | public function getEndingBalance(): ?Money |
|
| 406 | |||
| 407 | 1 | public function getFooter(): ?string |
|
| 411 | |||
| 412 | 1 | public function getHostedInvoiceUrl(): ?string |
|
| 416 | |||
| 417 | 1 | public function getInvoicePdf(): ?string |
|
| 421 | |||
| 422 | 1 | public function getLines(): LineItemCollection |
|
| 426 | |||
| 427 | 1 | public function getNextPaymentAttempt(): ?\DateTimeImmutable |
|
| 431 | |||
| 432 | 1 | public function getNumber(): string |
|
| 436 | |||
| 437 | 1 | public function isPaid(): bool |
|
| 441 | |||
| 442 | 1 | public function getPeriodEndAt(): \DateTimeImmutable |
|
| 446 | |||
| 447 | 1 | public function getPeriodStartAt(): \DateTimeImmutable |
|
| 451 | |||
| 452 | 1 | public function getReceiptNumber(): ?string |
|
| 456 | |||
| 457 | 1 | public function getStartingBalance(): Money |
|
| 461 | |||
| 462 | 1 | public function getStatementDescriptor(): ?string |
|
| 466 | |||
| 467 | 1 | public function getStatus(): string |
|
| 471 | |||
| 472 | 1 | public function getSubscription(): ?string |
|
| 476 | |||
| 477 | 1 | public function getSubscriptionProrationAt(): ?\DateTimeImmutable |
|
| 481 | |||
| 482 | 1 | public function getSubtotal(): Money |
|
| 486 | |||
| 487 | 1 | public function getTax(): Money |
|
| 491 | |||
| 492 | 1 | public function getTaxPercent(): float |
|
| 496 | |||
| 497 | 1 | public function getThresholdReason(): ?ThresholdReason |
|
| 501 | |||
| 502 | 1 | public function getTotal(): Money |
|
| 506 | |||
| 507 | 1 | public function getWebhooksDeliveredAt(): ?\DateTimeImmutable |
|
| 511 | } |
||
| 512 |
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..