Complex classes like Transaction 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 Transaction, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Transaction implements HydratableInterface |
||
10 | { |
||
11 | use PaymentNatureServiceTrait; |
||
12 | use PaymentInfosTrait; |
||
13 | use CustomerInfoTrait; |
||
14 | use ReconciliationIdentifiersTrait; |
||
15 | use CreditCardExpiryTrait; |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $transactionId; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $paymentId; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $authType; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $cardStatus; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $creditCardToken; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $creditCardMaskedPan; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $threeDSecureResult; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $liableForChargeback; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $CVVCheckResult; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $blacklistToken; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $shopOrderId; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $shop; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | private $terminal; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | private $transactionStatus; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | private $reasonCode; |
||
91 | |||
92 | /** |
||
93 | * @var int |
||
94 | */ |
||
95 | private $merchantCurrency; |
||
96 | |||
97 | /** |
||
98 | * @var string |
||
99 | */ |
||
100 | private $merchantCurrencyAlpha; |
||
101 | |||
102 | /** |
||
103 | * @var int |
||
104 | */ |
||
105 | private $cardHolderCurrency; |
||
106 | |||
107 | /** |
||
108 | * @var string |
||
109 | */ |
||
110 | private $cardHolderCurrencyAlpha; |
||
111 | |||
112 | /** |
||
113 | * @var int |
||
114 | */ |
||
115 | private $reservedAmount; |
||
116 | |||
117 | /** |
||
118 | * @var int |
||
119 | */ |
||
120 | private $capturedAmount; |
||
121 | |||
122 | /** |
||
123 | * @var int |
||
124 | */ |
||
125 | private $refundedAmount; |
||
126 | |||
127 | /** |
||
128 | * @var int |
||
129 | */ |
||
130 | private $creditedAmount; |
||
131 | |||
132 | /** |
||
133 | * @var int |
||
134 | */ |
||
135 | private $recurringDefaultAmount; |
||
136 | |||
137 | /** |
||
138 | * @var int |
||
139 | */ |
||
140 | private $surchargeAmount; |
||
141 | |||
142 | /** |
||
143 | * @var \DateTimeImmutable |
||
144 | */ |
||
145 | private $createdDate; |
||
146 | |||
147 | /** |
||
148 | * @var \DateTimeImmutable |
||
149 | */ |
||
150 | private $updatedDate; |
||
151 | |||
152 | /** |
||
153 | * @var string |
||
154 | */ |
||
155 | private $paymentNature; |
||
156 | |||
157 | /** |
||
158 | * @var string |
||
159 | */ |
||
160 | private $paymentSchemeName; |
||
161 | |||
162 | /** |
||
163 | * @var string |
||
164 | */ |
||
165 | private $addressVerification; |
||
166 | |||
167 | /** |
||
168 | * @var string |
||
169 | */ |
||
170 | private $addressVerificationDescription; |
||
171 | |||
172 | /** |
||
173 | * @var float |
||
174 | */ |
||
175 | private $fraudRiskScore; |
||
176 | |||
177 | /** |
||
178 | * @var string |
||
179 | */ |
||
180 | private $fraudExplanation; |
||
181 | |||
182 | 27 | public function hydrateXml(\SimpleXMLElement $xml) |
|
265 | |||
266 | /** |
||
267 | * @return int |
||
268 | */ |
||
269 | 3 | public function getTransactionId() : ?int |
|
273 | |||
274 | /** |
||
275 | * @return string |
||
276 | */ |
||
277 | 3 | public function getPaymentId() : ?string |
|
281 | |||
282 | /** |
||
283 | * @return string |
||
284 | */ |
||
285 | 6 | public function getAuthType(): ?string |
|
289 | |||
290 | /** |
||
291 | * @return string |
||
292 | */ |
||
293 | 3 | public function getCardStatus() : ?string |
|
297 | |||
298 | /** |
||
299 | * @return string |
||
300 | */ |
||
301 | 3 | public function getCreditCardToken() : ?string |
|
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | 3 | public function getCreditCardMaskedPan() : ?string |
|
313 | |||
314 | /** |
||
315 | * @return string |
||
316 | */ |
||
317 | 3 | public function getThreeDSecureResult() : ?string |
|
321 | |||
322 | /** |
||
323 | * @return string |
||
324 | */ |
||
325 | 3 | public function getLiableForChargeback() : ?string |
|
329 | |||
330 | /** |
||
331 | * @return string |
||
332 | */ |
||
333 | 6 | public function getCVVCheckResult(): ?string |
|
337 | |||
338 | /** |
||
339 | * @return string |
||
340 | */ |
||
341 | 3 | public function getBlacklistToken() : ?string |
|
345 | |||
346 | /** |
||
347 | * @return string |
||
348 | */ |
||
349 | 3 | public function getShopOrderId() : ?string |
|
353 | |||
354 | /** |
||
355 | * @return string |
||
356 | */ |
||
357 | 3 | public function getShop() : ?string |
|
361 | |||
362 | /** |
||
363 | * @return string |
||
364 | */ |
||
365 | 3 | public function getTerminal() : ?string |
|
369 | |||
370 | /** |
||
371 | * @return string |
||
372 | */ |
||
373 | 3 | public function getTransactionStatus() : ?string |
|
377 | |||
378 | /** |
||
379 | * @return string |
||
380 | */ |
||
381 | 3 | public function getReasonCode() : ?string |
|
385 | |||
386 | /** |
||
387 | * @return int |
||
388 | */ |
||
389 | 3 | public function getMerchantCurrency() : ?int |
|
393 | |||
394 | /** |
||
395 | * @return string |
||
396 | */ |
||
397 | 3 | public function getMerchantCurrencyAlpha() : ?string |
|
401 | |||
402 | /** |
||
403 | * @return int |
||
404 | */ |
||
405 | 3 | public function getCardHolderCurrency() : ?int |
|
409 | |||
410 | /** |
||
411 | * @return string |
||
412 | */ |
||
413 | 3 | public function getCardHolderCurrencyAlpha() : ?string |
|
417 | |||
418 | /** |
||
419 | * @return Money |
||
420 | */ |
||
421 | 3 | public function getReservedAmount() : ?Money |
|
425 | |||
426 | /** |
||
427 | * @return Money |
||
428 | */ |
||
429 | 3 | public function getCapturedAmount() : ?Money |
|
433 | |||
434 | /** |
||
435 | * @return Money |
||
436 | */ |
||
437 | 3 | public function getRefundedAmount() : ?Money |
|
441 | |||
442 | /** |
||
443 | * @return Money |
||
444 | */ |
||
445 | 6 | public function getCreditedAmount(): ?Money |
|
449 | |||
450 | /** |
||
451 | * @return Money |
||
452 | */ |
||
453 | 3 | public function getRecurringDefaultAmount() : ?Money |
|
457 | |||
458 | /** |
||
459 | * @return Money |
||
460 | */ |
||
461 | 6 | public function getSurchargeAmount(): ?Money |
|
465 | |||
466 | /** |
||
467 | * @return \DateTimeImmutable |
||
468 | */ |
||
469 | 3 | public function getCreatedDate() : ?\DateTimeImmutable |
|
473 | |||
474 | /** |
||
475 | * @return \DateTimeImmutable |
||
476 | */ |
||
477 | 3 | public function getUpdatedDate() : ?\DateTimeImmutable |
|
481 | |||
482 | /** |
||
483 | * @return string |
||
484 | */ |
||
485 | 3 | public function getPaymentNature() : ?string |
|
489 | |||
490 | /** |
||
491 | * @return float |
||
492 | */ |
||
493 | 3 | public function getFraudRiskScore() : ?float |
|
497 | |||
498 | /** |
||
499 | * @return string |
||
500 | */ |
||
501 | 3 | public function getFraudExplanation() : ?string |
|
505 | } |
||
506 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.