| Total Complexity | 48 |
| Total Lines | 253 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ElectornicInvoiceWrite 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.
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 ElectornicInvoiceWrite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ElectornicInvoiceWrite |
||
| 32 | { |
||
| 33 | |||
| 34 | use TraitVersions; |
||
|
|
|||
| 35 | |||
| 36 | protected \XMLWriter $objXmlWriter; |
||
| 37 | |||
| 38 | private function loadSettingsAndManageDefaults(array $arrayData, bool $bolComments, bool $bolSchemaLctn): array |
||
| 52 | } |
||
| 53 | |||
| 54 | private function setDocumentTag(array $arrayDocumentData): void |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | private function setElementComment(string $strKey): void |
||
| 71 | { |
||
| 72 | if (array_key_exists($strKey, $this->arraySettings['Comments'])) { |
||
| 73 | $elementComment = $this->arraySettings['Comments'][$strKey]; |
||
| 74 | if (is_array($elementComment)) { |
||
| 75 | foreach ($elementComment as $value) { |
||
| 76 | $this->objXmlWriter->writeComment($value); |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | $this->objXmlWriter->writeComment($elementComment); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | private function setElementsOrdered(array $arrayInput): void |
||
| 85 | { |
||
| 86 | $this->setElementComment($arrayInput['commentParentKey']); |
||
| 87 | $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']); |
||
| 88 | $this->setExtraElement($arrayInput, 'Start'); |
||
| 89 | $arrayCustomOrder = $this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']]; |
||
| 90 | foreach ($arrayCustomOrder as $value) { // get the values in expected order |
||
| 91 | if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional |
||
| 92 | $key = implode('_', [$arrayInput['commentParentKey'], $value]); |
||
| 93 | $matches = []; |
||
| 94 | preg_match('/^.*(Amount|Quantity)$/', $value, $matches, PREG_OFFSET_CAPTURE); |
||
| 95 | if (in_array($value, ['AdditionalItemProperty', 'CommodityClassification', 'StandardItemIdentification', 'TaxSubtotal'])) { |
||
| 96 | $this->setMultipleElementsOrdered([ |
||
| 97 | 'commentParentKey' => $key, |
||
| 98 | 'data' => $arrayInput['data'][$value], |
||
| 99 | 'tag' => $value, |
||
| 100 | ]); |
||
| 101 | } elseif (($matches !== []) || !is_array($arrayInput['data'][$value]) || in_array($arrayInput['commentParentKey'], ['AccountingCustomerParty_PartyIdentification', 'AccountingSupplierParty_PartyIdentification', 'Lines_Item_SellersItemIdentification', 'Lines_Item_StandardItemIdentification', 'Lines_Item_CommodityClassification'])) { |
||
| 102 | $this->setSingleElementWithAttribute([ |
||
| 103 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 104 | 'data' => $arrayInput['data'][$value], |
||
| 105 | 'tag' => $value, |
||
| 106 | ]); |
||
| 107 | } elseif (is_array($arrayInput['data'][$value])) { |
||
| 108 | $this->setElementsOrdered([ |
||
| 109 | 'commentParentKey' => $key, |
||
| 110 | 'data' => $arrayInput['data'][$value], |
||
| 111 | 'tag' => $value, |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | $this->setExtraElement($arrayInput, 'End'); |
||
| 117 | $this->objXmlWriter->endElement(); // $key |
||
| 118 | } |
||
| 119 | |||
| 120 | private function setExtraElement(array $arrayInput, string $strType): void |
||
| 121 | { |
||
| 122 | if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) { |
||
| 123 | switch ($strType) { |
||
| 124 | case 'End': |
||
| 125 | $this->objXmlWriter->endElement(); |
||
| 126 | break; |
||
| 127 | case 'Start': |
||
| 128 | $this->objXmlWriter->startElement('cac:Party'); |
||
| 129 | break; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | private function setHeaderCommonBasicComponents(array $arrayElementWithData): void |
||
| 135 | { |
||
| 136 | $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header_CBC']; |
||
| 137 | foreach ($arrayCustomOrdered as $value) { |
||
| 138 | if (array_key_exists($value, $arrayElementWithData)) { |
||
| 139 | $this->setElementComment($value); |
||
| 140 | $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | private function setManageComment(string $strCommentParentKey, array $arrayIn): string |
||
| 146 | { |
||
| 147 | if (str_starts_with($strCommentParentKey, 'AllowanceCharge')) { |
||
| 148 | $arrayCommentPieces = explode('_', $strCommentParentKey); |
||
| 149 | array_splice($arrayCommentPieces, 0, 1, 'AllowanceCharge~ChargeIndicator' |
||
| 150 | . ucfirst($arrayIn['ChargeIndicator'])); // carefully manage a child to decide on comment tag |
||
| 151 | $strCommentParentKey = implode('_', $arrayCommentPieces); |
||
| 152 | } |
||
| 153 | return $strCommentParentKey; |
||
| 154 | } |
||
| 155 | |||
| 156 | private function setMultipleElementsOrdered(array $arrayData): void |
||
| 157 | { |
||
| 158 | foreach ($arrayData['data'] as $value) { |
||
| 159 | $strCommentParentKey = $this->setManageComment($arrayData['commentParentKey'], $value); |
||
| 160 | $this->setElementsOrdered([ |
||
| 161 | 'commentParentKey' => $strCommentParentKey, |
||
| 162 | 'data' => $value, |
||
| 163 | 'tag' => $arrayData['tag'], |
||
| 164 | ]); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | private function setPrepareXml(string $strFile): void |
||
| 175 | } |
||
| 176 | |||
| 177 | private function setProduceMiddleXml(array $arrayData): void |
||
| 178 | { |
||
| 179 | $arrayAggregates = $arrayData['Header']['CommonAggregateComponents-2']; |
||
| 180 | $arrayOptionalElementsHeader = [ |
||
| 181 | 'InvoicePeriod' => 'Single', |
||
| 182 | 'OrderReference' => 'Single', |
||
| 183 | 'BillingReference' => 'Single', |
||
| 184 | 'DespatchDocumentReference' => 'Single', |
||
| 185 | 'ReceiptDocumentReference' => 'Single', |
||
| 186 | 'OriginatorDocumentReference' => 'Single', |
||
| 187 | 'ContractDocumentReference' => 'Single', |
||
| 188 | 'ProjectReference' => 'Single', |
||
| 189 | 'AdditionalDocumentReference' => 'Multiple', |
||
| 190 | 'AccountingSupplierParty' => 'SingleCompany', |
||
| 191 | 'AccountingCustomerParty' => 'SingleCompany', |
||
| 192 | 'PayeeParty' => 'Single', |
||
| 193 | 'TaxRepresentativeParty' => 'Single', |
||
| 194 | 'Delivery' => 'Single', |
||
| 195 | 'PaymentMeans' => 'Multiple', |
||
| 196 | 'PaymentTerms' => 'Single', |
||
| 197 | 'AllowanceCharge' => 'Multiple', |
||
| 198 | 'TaxTotal' => 'Single', |
||
| 199 | 'LegalMonetaryTotal' => 'Single', |
||
| 200 | ]; |
||
| 201 | foreach ($arrayOptionalElementsHeader as $key => $strLogicType) { |
||
| 202 | if (array_key_exists($key, $arrayAggregates)) { |
||
| 203 | switch ($strLogicType) { |
||
| 204 | case 'Multiple': |
||
| 205 | $this->setMultipleElementsOrdered([ |
||
| 206 | 'commentParentKey' => $key, |
||
| 207 | 'data' => $arrayAggregates[$key], |
||
| 208 | 'tag' => $key, |
||
| 209 | ]); |
||
| 210 | break; |
||
| 211 | case 'Single': |
||
| 212 | $this->setElementsOrdered([ |
||
| 213 | 'commentParentKey' => $key, |
||
| 214 | 'data' => $arrayAggregates[$key], |
||
| 215 | 'tag' => $key, |
||
| 216 | ]); |
||
| 217 | break; |
||
| 218 | case 'SingleCompany': |
||
| 219 | $this->setElementsOrdered([ |
||
| 220 | 'commentParentKey' => $key, |
||
| 221 | 'data' => $arrayAggregates[$key]['Party'], |
||
| 222 | 'tag' => $key, |
||
| 223 | ]); |
||
| 224 | break; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | private function setSingleComment(array $arrayInput): void |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | private function setSingleElementWithAttribute(array $arrayInput): void |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | public function writeElectronicInvoice(string $strFile, array $inData, bool $bolCmnts, bool $bolScLc = false): void |
||
| 270 | { |
||
| 284 | } |
||
| 285 | } |
||
| 286 |