Complex classes like EntryTransactionDetail 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 EntryTransactionDetail, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class EntryTransactionDetail |
||
15 | { |
||
16 | /** |
||
17 | * @param DTO\EntryTransactionDetail $detail |
||
18 | * @param SimpleXMLElement $xmlDetail |
||
19 | */ |
||
20 | 22 | public function addReferences(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
21 | 11 | { |
|
22 | 22 | if (false === isset($xmlDetail->Refs)) { |
|
23 | 8 | return; |
|
24 | } |
||
25 | |||
26 | 17 | $refs = $xmlDetail->Refs; |
|
|
|||
27 | 17 | $reference = new DTO\Reference(); |
|
28 | |||
29 | 17 | $reference->setMessageId(isset($refs->MsgId) ? (string) $refs->MsgId : null); |
|
30 | 17 | $reference->setAccountServiceReference(isset($refs->AcctSvcrRef) ? (string) $refs->AcctSvcrRef : null); |
|
31 | 17 | $reference->setPaymentInformationId(isset($refs->PmtInfId) ? (string) $refs->PmtInfId : null); |
|
32 | 17 | $reference->setInstructionId(isset($refs->InstrId) ? (string) $refs->InstrId : null); |
|
33 | 17 | $reference->setEndToEndId(isset($refs->EndToEndId) ? (string) $refs->EndToEndId : null); |
|
34 | 17 | $reference->setTransactionId(isset($refs->TxId) ? (string) $refs->TxId : null); |
|
35 | 17 | $reference->setMandateId(isset($refs->MndtId) ? (string) $refs->MndtId : null); |
|
36 | 17 | $reference->setChequeNumber(isset($refs->ChqNb) ? (string) $refs->ChqNb : null); |
|
37 | 17 | $reference->setClearingSystemReference(isset($refs->ClrSysRef) ? (string) $refs->ClrSysRef : null); |
|
38 | 17 | $reference->setAccountOwnerTransactionId(isset($refs->AcctOwnrTxId) ? (string) $refs->AcctOwnrTxId : null); |
|
39 | 18 | $reference->setAccountServicerTransactionId(isset($refs->AcctSvcrTxId) ? (string) $refs->AcctSvcrTxId : null); |
|
40 | 17 | $reference->setMarketInfrastructureTransactionId(isset($refs->MktInfrstrctrTxId) ? (string) $refs->MktInfrstrctrTxId : null); |
|
41 | 17 | $reference->setProcessingId(isset($refs->PrcgId) ? (string) $refs->PrcgId : null); |
|
42 | |||
43 | 17 | foreach ($refs->Prtry as $xmlProprietary) { |
|
44 | 8 | $type = isset($xmlProprietary->Tp) ? (string) $xmlProprietary->Tp : null; |
|
45 | 16 | $subReference = isset($xmlProprietary->Ref) ? (string) $xmlProprietary->Ref : null; |
|
46 | |||
47 | 8 | $reference->addProprietary(new DTO\ProprietaryReference($type, $subReference)); |
|
48 | 17 | } |
|
49 | |||
50 | 17 | $detail->addReference($reference); |
|
51 | 17 | } |
|
52 | |||
53 | /** |
||
54 | * @param DTO\EntryTransactionDetail $detail |
||
55 | * @param SimpleXMLElement $xmlDetail |
||
56 | */ |
||
57 | 22 | public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
58 | { |
||
59 | 22 | if (false === isset($xmlDetail->RltdPties)) { |
|
60 | 1 | return; |
|
61 | } |
||
62 | |||
63 | 21 | foreach ($xmlDetail->RltdPties as $xmlRelatedParty) { |
|
64 | 21 | if (isset($xmlRelatedParty->Cdtr)) { |
|
65 | 21 | $xmlRelatedPartyType = $xmlRelatedParty->Cdtr; |
|
66 | 21 | $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct; |
|
67 | 21 | $relatedPartyType = $creditor = new DTO\Creditor((string) $xmlRelatedPartyType->Nm); |
|
68 | |||
69 | 21 | $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount); |
|
70 | 21 | } |
|
71 | |||
72 | 21 | if (isset($xmlRelatedParty->Dbtr)) { |
|
73 | 18 | $xmlRelatedPartyType = $xmlRelatedParty->Dbtr; |
|
74 | 18 | $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct; |
|
75 | 18 | $relatedPartyType = $debtor = new DTO\Debtor((string) $xmlRelatedPartyType->Nm); |
|
76 | |||
77 | 18 | $this->addRelatedParty($detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount); |
|
78 | 18 | } |
|
79 | 21 | } |
|
80 | 21 | } |
|
81 | |||
82 | /** |
||
83 | * @param DTO\EntryTransactionDetail $detail |
||
84 | * @param $xmlRelatedPartyType |
||
85 | * @param $relatedPartyType |
||
86 | * @param $xmlRelatedPartyTypeAccount |
||
87 | * @return DTO\RelatedParty |
||
88 | */ |
||
89 | 21 | protected function addRelatedParty(DTO\EntryTransactionDetail $detail, $xmlRelatedPartyType, $relatedPartyType, $xmlRelatedPartyTypeAccount) |
|
90 | { |
||
91 | 21 | if (isset($xmlRelatedPartyType->PstlAdr)) { |
|
92 | 21 | $relatedPartyType->setAddress(DTOFactory\Address::createFromXml($xmlRelatedPartyType->PstlAdr)); |
|
93 | 21 | } |
|
94 | |||
95 | 21 | $relatedParty = new DTO\RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount)); |
|
96 | |||
97 | 21 | $detail->addRelatedParty($relatedParty); |
|
98 | |||
99 | 21 | return $relatedParty; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param DTO\EntryTransactionDetail $detail |
||
104 | * @param SimpleXMLElement $xmlDetail |
||
105 | */ |
||
106 | 20 | public function addRelatedAgents(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
107 | { |
||
108 | 20 | if (false === isset($xmlDetail->RltdAgts)) { |
|
109 | 12 | return; |
|
110 | } |
||
111 | |||
112 | 16 | foreach ($xmlDetail->RltdAgts as $xmlRelatedAgent) { |
|
113 | 16 | if (isset($xmlRelatedAgent->CdtrAgt)) { |
|
114 | 16 | $agent = new DTO\CreditorAgent((string)$xmlRelatedAgent->CdtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->CdtrAgt->FinInstnId->BIC); |
|
115 | 16 | $relatedAgent = new DTO\RelatedAgent($agent); |
|
116 | 16 | $detail->addRelatedAgent($relatedAgent); |
|
117 | 16 | } |
|
118 | |||
119 | 16 | if (isset($xmlRelatedAgent->DbtrAgt)) { |
|
120 | 16 | $agent = new DTO\DebtorAgent((string)$xmlRelatedAgent->DbtrAgt->FinInstnId->Nm, (string)$xmlRelatedAgent->DbtrAgt->FinInstnId->BIC); |
|
121 | 16 | $relatedAgent = new DTO\RelatedAgent($agent); |
|
122 | 16 | $detail->addRelatedAgent($relatedAgent); |
|
123 | 16 | } |
|
124 | 16 | } |
|
125 | 16 | } |
|
126 | |||
127 | /** |
||
128 | * @param DTO\EntryTransactionDetail $detail |
||
129 | * @param SimpleXMLElement $xmlDetail |
||
130 | */ |
||
131 | 23 | public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
132 | { |
||
133 | 23 | if (false === isset($xmlDetail->RmtInf)) { |
|
134 | 11 | return; |
|
135 | } |
||
136 | |||
137 | 18 | if (isset($xmlDetail->RmtInf->Ustrd)) { |
|
138 | 9 | $remittanceInformation = DTO\RemittanceInformation::fromUnstructured( |
|
139 | 9 | (string)$xmlDetail->RmtInf->Ustrd |
|
140 | 9 | ); |
|
141 | 9 | $detail->setRemittanceInformation($remittanceInformation); |
|
142 | |||
143 | 9 | return; |
|
144 | } |
||
145 | |||
146 | 9 | if (isset($xmlDetail->RmtInf->Strd) |
|
147 | 9 | && isset($xmlDetail->RmtInf->Strd->CdtrRefInf) |
|
148 | 9 | && isset($xmlDetail->RmtInf->Strd->CdtrRefInf->Ref) |
|
149 | 9 | ) { |
|
150 | 9 | $creditorReferenceInformation = DTO\CreditorReferenceInformation::fromUnstructured( |
|
151 | 9 | (string)$xmlDetail->RmtInf->Strd->CdtrRefInf->Ref |
|
152 | 9 | ); |
|
153 | 9 | $remittanceInformation = new DTO\RemittanceInformation(); |
|
154 | 9 | $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation); |
|
155 | 9 | $detail->setRemittanceInformation($remittanceInformation); |
|
156 | 9 | } |
|
157 | 9 | } |
|
158 | |||
159 | /** |
||
160 | * @param DTO\EntryTransactionDetail $detail |
||
161 | * @param SimpleXMLElement $xmlDetail |
||
162 | */ |
||
163 | 22 | public function addRelatedDates(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
164 | { |
||
165 | 22 | if (false === isset($xmlDetail->RltdDts)) { |
|
166 | 21 | return; |
|
167 | } |
||
168 | |||
169 | 1 | if (isset($xmlDetail->RltdDts->AccptncDtTm)) { |
|
170 | 1 | $RelatedDates = DTO\RelatedDates::fromUnstructured( |
|
171 | 1 | new DateTimeImmutable( (string) $xmlDetail->RltdDts->AccptncDtTm ) |
|
172 | 1 | ); |
|
173 | 1 | $detail->setRelatedDates($RelatedDates); |
|
174 | 1 | return; |
|
175 | } |
||
176 | } |
||
177 | |||
178 | |||
179 | |||
180 | /** |
||
181 | * @param DTO\EntryTransactionDetail $detail |
||
182 | * @param SimpleXMLElement $xmlDetail |
||
183 | */ |
||
184 | 22 | public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
185 | { |
||
186 | 22 | if (isset($xmlDetail->RtrInf) && isset($xmlDetail->RtrInf->Rsn->Cd)) { |
|
187 | 1 | $remittanceInformation = DTO\ReturnInformation::fromUnstructured( |
|
188 | 1 | (string)$xmlDetail->RtrInf->Rsn->Cd, |
|
189 | 1 | (string)$xmlDetail->RtrInf->AddtlInf |
|
190 | 1 | ); |
|
191 | 1 | $detail->setReturnInformation($remittanceInformation); |
|
192 | 1 | } |
|
193 | 22 | } |
|
194 | |||
195 | /** |
||
196 | * @param DTO\EntryTransactionDetail $detail |
||
197 | * @param SimpleXMLElement $xmlDetail |
||
198 | */ |
||
199 | 22 | public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
200 | { |
||
201 | 22 | if (isset($xmlDetail->AddtlTxInf)) { |
|
202 | 1 | $additionalInformation = new DTO\AdditionalTransactionInformation( |
|
203 | 1 | (string) $xmlDetail->AddtlTxInf |
|
204 | 1 | ); |
|
205 | 1 | $detail->setAdditionalTransactionInformation($additionalInformation); |
|
206 | 1 | } |
|
207 | 22 | } |
|
208 | |||
209 | /** |
||
210 | * @param DTO\EntryTransactionDetail $detail |
||
211 | * @param SimpleXMLElement $xmlDetail |
||
212 | */ |
||
213 | 20 | public function addBankTransactionCode(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
214 | { |
||
215 | 20 | $bankTransactionCode = new DTO\BankTransactionCode(); |
|
216 | |||
217 | 20 | if (isset($xmlDetail->BkTxCd)) { |
|
218 | 18 | $bankTransactionCode = new DTO\BankTransactionCode(); |
|
219 | |||
220 | 18 | if (isset($xmlDetail->BkTxCd->Prtry)) { |
|
221 | 18 | $proprietaryBankTransactionCode = new DTO\ProprietaryBankTransactionCode( |
|
222 | 18 | (string)$xmlDetail->BkTxCd->Prtry->Cd, |
|
223 | 18 | (string)$xmlDetail->BkTxCd->Prtry->Issr |
|
224 | 18 | ); |
|
225 | |||
226 | 18 | $bankTransactionCode->setProprietary($proprietaryBankTransactionCode); |
|
227 | 18 | } |
|
228 | 18 | } |
|
229 | |||
230 | 20 | $detail->setBankTransactionCode($bankTransactionCode); |
|
231 | 20 | } |
|
232 | |||
233 | /** |
||
234 | * @param DTO\EntryTransactionDetail $detail |
||
235 | * @param SimpleXMLElement $xmlDetail |
||
236 | */ |
||
237 | 22 | public function addCharges(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
238 | { |
||
239 | 22 | if (isset($xmlDetail->Chrgs)) { |
|
240 | 4 | $charges = new DTO\Charges(); |
|
241 | |||
242 | 4 | if (isset($xmlDetail->Chrgs->TtlChrgsAndTaxAmt) && (string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt) { |
|
243 | 4 | $amount = StringToUnits::convert((string) $xmlDetail->Chrgs->TtlChrgsAndTaxAmt); |
|
244 | 4 | $currency = (string)$xmlDetail->Chrgs->TtlChrgsAndTaxAmt['Ccy']; |
|
245 | |||
246 | 4 | $charges->setTotalChargesAndTaxAmount(new Money($amount, new Currency($currency))); |
|
247 | 4 | } |
|
248 | |||
249 | 4 | $chargesRecords = $xmlDetail->Chrgs->Rcrd; |
|
250 | 4 | if ($chargesRecords) { |
|
251 | 4 | foreach ($chargesRecords as $chargesRecord) { |
|
252 | |||
253 | 4 | $chargesDetail = new DTO\ChargesRecord(); |
|
254 | |||
255 | 4 | if(isset($chargesRecord->Amt) && (string) $chargesRecord->Amt) { |
|
256 | 4 | $amount = StringToUnits::convert((string) $chargesRecord->Amt); |
|
257 | 4 | $currency = (string)$chargesRecord->Amt['Ccy']; |
|
258 | |||
259 | 4 | if ((string) $chargesRecord->CdtDbtInd === 'DBIT') { |
|
260 | 4 | $amount = $amount * -1; |
|
261 | 4 | } |
|
262 | |||
263 | 4 | $chargesDetail->setAmount(new Money($amount, new Currency($currency))); |
|
264 | 4 | } |
|
265 | 4 | if (isset($chargesRecord->CdtDbtInd) && (string) $chargesRecord->CdtDbtInd === 'true') { |
|
266 | $chargesDetail->setChargesIncludedIndicator(true); |
||
267 | } |
||
268 | 4 | if (isset($chargesRecord->Tp->Prtry->Id) && (string) $chargesRecord->Tp->Prtry->Id) { |
|
269 | 4 | $chargesDetail->setIdentification((string) $chargesRecord->Tp->Prtry->Id); |
|
270 | 4 | } |
|
271 | 4 | $charges->addRecord($chargesDetail); |
|
272 | 4 | } |
|
273 | 4 | } |
|
274 | 4 | $detail->setCharges($charges); |
|
275 | 4 | } |
|
276 | 22 | } |
|
277 | |||
278 | /** |
||
279 | * @param DTO\EntryTransactionDetail $detail |
||
280 | * @param SimpleXMLElement $xmlDetail |
||
281 | * @param SimpleXMLElement $CdtDbtInd |
||
282 | */ |
||
283 | 20 | public function addAmountDetails(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd) |
|
284 | { |
||
285 | 20 | if (isset($xmlDetail->AmtDtls)) { |
|
286 | 6 | $amountDetails = new DTO\AmountDetails(); |
|
287 | |||
288 | 6 | if (isset($xmlDetail->AmtDtls->TxAmt) && isset($xmlDetail->AmtDtls->TxAmt->Amt)) { |
|
289 | 6 | $amount = StringToUnits::convert((string) $xmlDetail->AmtDtls->TxAmt->Amt); |
|
290 | |||
291 | 6 | if ((string) $CdtDbtInd === 'DBIT') { |
|
292 | 6 | $amount = $amount * -1; |
|
293 | 6 | } |
|
294 | |||
295 | 6 | $money = new Money( |
|
296 | 6 | $amount, |
|
297 | 6 | new Currency((string) $xmlDetail->AmtDtls->TxAmt->Amt['Ccy']) |
|
298 | 6 | ); |
|
299 | 6 | $amountDetails->setAmount($money); |
|
300 | 6 | } |
|
301 | 6 | $detail->setAmountDetails($amountDetails); |
|
302 | 6 | } |
|
303 | 20 | } |
|
304 | |||
305 | /** |
||
306 | * @param DTO\EntryTransactionDetail $detail |
||
307 | * @param SimpleXMLElement $xmlDetail |
||
308 | * @param SimpleXMLElement $CdtDbtInd |
||
309 | */ |
||
310 | 20 | public function addAmount(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail, $CdtDbtInd) |
|
311 | { |
||
312 | 20 | if (isset($xmlDetail->Amt)) { |
|
313 | 13 | $amountDetails = new DTO\Amount(); |
|
314 | |||
315 | 13 | $amount = StringToUnits::convert((string) $xmlDetail->Amt); |
|
316 | |||
317 | 13 | if ((string) $CdtDbtInd === 'DBIT') { |
|
318 | 6 | $amount = $amount * -1; |
|
319 | 6 | } |
|
320 | |||
321 | 13 | $money = new Money( |
|
322 | 13 | $amount, |
|
323 | 13 | new Currency((string) $xmlDetail->Amt['Ccy']) |
|
324 | 13 | ); |
|
325 | 13 | $amountDetails->setAmount($money); |
|
326 | |||
327 | 13 | $detail->setAmount($amountDetails); |
|
328 | 13 | } |
|
329 | 20 | } |
|
330 | |||
331 | /** |
||
332 | * @param SimpleXMLElement $xmlDetail |
||
333 | * |
||
334 | * @return DTO\Account|null |
||
335 | */ |
||
336 | abstract public function getRelatedPartyAccount(SimpleXMLElement $xmlRelatedPartyTypeAccount); |
||
337 | } |
||
338 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.