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 |
||
9 | abstract class EntryTransactionDetail |
||
10 | { |
||
11 | /** |
||
12 | * @param DTO\EntryTransactionDetail $detail |
||
13 | * @param SimpleXMLElement $xmlDetail |
||
14 | */ |
||
15 | 15 | public function addReferences(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
47 | |||
48 | /** |
||
49 | * @param DTO\EntryTransactionDetail $detail |
||
50 | * @param SimpleXMLElement $xmlDetail |
||
51 | */ |
||
52 | 15 | public function addRelatedParties(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
53 | { |
||
54 | 15 | if (false === isset($xmlDetail->RltdPties)) { |
|
55 | 1 | return; |
|
56 | } |
||
57 | |||
58 | 14 | foreach ($xmlDetail->RltdPties as $xmlRelatedParty) { |
|
59 | 14 | if (isset($xmlRelatedParty->Cdtr)) { |
|
60 | 13 | $xmlRelatedPartyType = $xmlRelatedParty->Cdtr; |
|
61 | 13 | $xmlRelatedPartyTypeAccount = $xmlRelatedParty->CdtrAcct; |
|
62 | 13 | $relatedPartyType = $creditor = new DTO\Creditor((string) $xmlRelatedPartyType->Nm); |
|
63 | 14 | } elseif (isset($xmlRelatedParty->Dbtr)) { |
|
64 | 6 | $xmlRelatedPartyType = $xmlRelatedParty->Dbtr; |
|
65 | 6 | $xmlRelatedPartyTypeAccount = $xmlRelatedParty->DbtrAcct; |
|
66 | 6 | $relatedPartyType = $creditor = new DTO\Debtor((string) $xmlRelatedPartyType->Nm); |
|
67 | 6 | } else { |
|
68 | continue; |
||
69 | } |
||
70 | |||
71 | 14 | if (isset($xmlRelatedPartyType->PstlAdr)) { |
|
72 | 11 | $address = new DTO\Address(); |
|
73 | 11 | if (isset($xmlRelatedPartyType->PstlAdr->Ctry)) { |
|
74 | 11 | $address = $address->setCountry((string) $xmlRelatedPartyType->PstlAdr->Ctry); |
|
75 | 11 | } |
|
76 | 11 | if (isset($xmlRelatedPartyType->PstlAdr->CtrySubDvsn)) { |
|
77 | 1 | $address = $address->setCountrySubDivision((string) $xmlRelatedPartyType->PstlAdr->CtrySubDvsn); |
|
78 | 1 | } |
|
79 | 1 | if (isset($xmlRelatedPartyType->PstlAdr->Dept)) { |
|
80 | 1 | $address = $address->setDepartment((string) $xmlRelatedPartyType->PstlAdr->Dept); |
|
81 | } |
||
82 | 11 | if (isset($xmlRelatedPartyType->PstlAdr->SubDept)) { |
|
83 | 11 | $address = $address->setSubDepartment((string) $xmlRelatedPartyType->PstlAdr->SubDept); |
|
84 | } |
||
85 | 14 | if (isset($xmlRelatedPartyType->PstlAdr->StrtNm)) { |
|
86 | 14 | $address = $address->setStreetName((string) $xmlRelatedPartyType->PstlAdr->StrtNm); |
|
87 | 14 | } |
|
88 | 14 | if (isset($xmlRelatedPartyType->PstlAdr->BldgNb)) { |
|
89 | $address = $address->setBuildingNumber((string) $xmlRelatedPartyType->PstlAdr->BldgNb); |
||
90 | } |
||
91 | if (isset($xmlRelatedPartyType->PstlAdr->PstCd)) { |
||
92 | $address = $address->setPostCode((string) $xmlRelatedPartyType->PstlAdr->PstCd); |
||
93 | } |
||
94 | 16 | if (isset($xmlRelatedPartyType->PstlAdr->TwnNm)) { |
|
95 | $address = $address->setTownName((string) $xmlRelatedPartyType->PstlAdr->TwnNm); |
||
96 | 16 | } |
|
97 | 1 | if (isset($xmlRelatedPartyType->PstlAdr->AdrLine)) { |
|
98 | foreach ($xmlRelatedPartyType->PstlAdr->AdrLine as $line) { |
||
99 | $address = $address->addAddressLine((string)$line); |
||
100 | 15 | } |
|
101 | 6 | } |
|
102 | 6 | ||
103 | 6 | $relatedPartyType->setAddress($address); |
|
104 | 6 | } |
|
105 | |||
106 | 6 | $relatedParty = new DTO\RelatedParty($relatedPartyType, $this->getRelatedPartyAccount($xmlRelatedPartyTypeAccount)); |
|
107 | $detail->addRelatedParty($relatedParty); |
||
108 | } |
||
109 | 9 | } |
|
110 | 9 | ||
111 | 9 | /** |
|
112 | 9 | * @param DTO\EntryTransactionDetail $detail |
|
113 | 9 | * @param SimpleXMLElement $xmlDetail |
|
114 | 9 | */ |
|
115 | 9 | public function addRemittanceInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
116 | 9 | { |
|
117 | 9 | if (false === isset($xmlDetail->RmtInf)) { |
|
118 | 9 | return; |
|
119 | 9 | } |
|
120 | 9 | ||
121 | if (isset($xmlDetail->RmtInf->Ustrd)) { |
||
122 | $remittanceInformation = DTO\RemittanceInformation::fromUnstructured( |
||
123 | (string)$xmlDetail->RmtInf->Ustrd |
||
124 | ); |
||
125 | $detail->setRemittanceInformation($remittanceInformation); |
||
126 | 15 | ||
127 | return; |
||
128 | 15 | } |
|
129 | 1 | ||
130 | 1 | if (isset($xmlDetail->RmtInf->Strd) |
|
131 | 1 | && isset($xmlDetail->RmtInf->Strd->CdtrRefInf) |
|
132 | 1 | && isset($xmlDetail->RmtInf->Strd->CdtrRefInf->Ref) |
|
133 | 1 | ) { |
|
134 | 1 | $creditorReferenceInformation = DTO\CreditorReferenceInformation::fromUnstructured( |
|
135 | 15 | (string)$xmlDetail->RmtInf->Strd->CdtrRefInf->Ref |
|
136 | ); |
||
137 | $remittanceInformation = new DTO\RemittanceInformation(); |
||
138 | $remittanceInformation->setCreditorReferenceInformation($creditorReferenceInformation); |
||
139 | $detail->setRemittanceInformation($remittanceInformation); |
||
140 | } |
||
141 | 15 | } |
|
142 | |||
143 | 15 | /** |
|
144 | 1 | * @param DTO\EntryTransactionDetail $detail |
|
145 | 1 | * @param SimpleXMLElement $xmlDetail |
|
146 | 1 | */ |
|
147 | 1 | public function addReturnInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
|
148 | 1 | { |
|
149 | 15 | if (isset($xmlDetail->RtrInf) && isset($xmlDetail->RtrInf->Rsn->Cd)) { |
|
150 | $remittanceInformation = DTO\ReturnInformation::fromUnstructured( |
||
151 | (string)$xmlDetail->RtrInf->Rsn->Cd, |
||
152 | (string)$xmlDetail->RtrInf->AddtlInf |
||
153 | ); |
||
154 | $detail->setReturnInformation($remittanceInformation); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param DTO\EntryTransactionDetail $detail |
||
160 | * @param SimpleXMLElement $xmlDetail |
||
161 | */ |
||
162 | public function addAdditionalTransactionInformation(DTO\EntryTransactionDetail $detail, SimpleXMLElement $xmlDetail) |
||
163 | { |
||
164 | if (isset($xmlDetail->AddtlTxInf)) { |
||
165 | $additionalInformation = new DTO\AdditionalTransactionInformation( |
||
166 | (string) $xmlDetail->AddtlTxInf |
||
167 | ); |
||
168 | $detail->setAdditionalTransactionInformation($additionalInformation); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param SimpleXMLElement $xmlDetail |
||
174 | * |
||
175 | * @return DTO\Account|null |
||
176 | */ |
||
177 | abstract public function getRelatedPartyAccount(SimpleXMLElement $xmlRelatedPartyTypeAccount); |
||
178 | } |
||
179 |
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.