| Total Complexity | 51 |
| Total Lines | 267 |
| 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 |
||
| 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, ['EmbeddedDocumentBinaryObject', 'EndpointID'])) { |
||
| 96 | $this->setSingleElementWithAttribute([ |
||
| 97 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 98 | 'data' => $arrayInput['data'][$value], |
||
| 99 | 'tag' => $value, |
||
| 100 | ]); |
||
| 101 | } elseif (in_array($value, ['AdditionalItemProperty', 'CommodityClassification', 'PartyTaxScheme', 'StandardItemIdentification', 'TaxSubtotal'])) { |
||
| 102 | $this->setMultipleElementsOrdered([ |
||
| 103 | 'commentParentKey' => $key, |
||
| 104 | 'data' => $arrayInput['data'][$value], |
||
| 105 | 'tag' => $value, |
||
| 106 | ]); |
||
| 107 | } elseif (($matches !== []) || !is_array($arrayInput['data'][$value]) || in_array($arrayInput['commentParentKey'], ['AccountingCustomerParty_PartyIdentification', 'AccountingSupplierParty_PartyIdentification', 'Lines_Item_SellersItemIdentification', 'Lines_Item_StandardItemIdentification', 'Lines_Item_CommodityClassification'])) { |
||
| 108 | $this->setSingleElementWithAttribute([ |
||
| 109 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 110 | 'data' => $arrayInput['data'][$value], |
||
| 111 | 'tag' => $value, |
||
| 112 | ]); |
||
| 113 | } elseif (is_array($arrayInput['data'][$value])) { |
||
| 114 | $this->setElementsOrdered([ |
||
| 115 | 'commentParentKey' => $key, |
||
| 116 | 'data' => $arrayInput['data'][$value], |
||
| 117 | 'tag' => $value, |
||
| 118 | ]); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | $this->setExtraElement($arrayInput, 'End'); |
||
| 123 | $this->objXmlWriter->endElement(); // $key |
||
| 124 | } |
||
| 125 | |||
| 126 | private function setExtraElement(array $arrayInput, string $strType): void |
||
| 127 | { |
||
| 128 | if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) { |
||
| 129 | switch ($strType) { |
||
| 130 | case 'End': |
||
| 131 | $this->objXmlWriter->endElement(); |
||
| 132 | break; |
||
| 133 | case 'Start': |
||
| 134 | $this->objXmlWriter->startElement('cac:Party'); |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | private function setHeaderCommonBasicComponents(array $arrayElementWithData): void |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | private function setManageComment(string $strCommentParentKey, array $arrayIn): string |
||
| 160 | } |
||
| 161 | |||
| 162 | private function setMultipleElementsOrdered(array $arrayData): void |
||
| 170 | ]); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | protected function setNumericValue(string $strTag, array $arrayDataIn): string|float |
||
| 175 | { |
||
| 176 | $sReturn = $arrayDataIn['value']; |
||
| 177 | $arrayRawTags = ['CreditedQuantity', 'EndpointID', 'InvoicedQuantity', 'PriceAmount']; |
||
| 178 | if (is_numeric($arrayDataIn['value']) && !in_array($strTag, $arrayRawTags)) { |
||
| 179 | $fmt = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL); |
||
| 180 | $fmt->setAttribute(\NumberFormatter::GROUPING_USED, 0); |
||
| 181 | $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); |
||
| 182 | // if contains currencyID consider 2 decimals as minimum |
||
| 183 | if (in_array('currencyID', array_keys($arrayDataIn))) { |
||
| 184 | $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2); |
||
| 185 | } |
||
| 186 | $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2); |
||
| 187 | $sReturn = $fmt->format($arrayDataIn['value']); |
||
| 188 | } |
||
| 189 | return $sReturn; |
||
| 190 | } |
||
| 191 | |||
| 192 | private function setPrepareXml(string $strFile): void |
||
| 199 | } |
||
| 200 | |||
| 201 | private function setProduceMiddleXml(array $arrayData): void |
||
| 202 | { |
||
| 203 | $arrayAggregates = $arrayData['Header']['CommonAggregateComponents-2']; |
||
| 204 | $arrayOptionalElementsHeader = [ |
||
| 205 | 'InvoicePeriod' => 'Single', |
||
| 206 | 'OrderReference' => 'Single', |
||
| 207 | 'BillingReference' => 'Single', |
||
| 208 | 'DespatchDocumentReference' => 'Single', |
||
| 209 | 'ReceiptDocumentReference' => 'Single', |
||
| 210 | 'OriginatorDocumentReference' => 'Single', |
||
| 211 | 'ContractDocumentReference' => 'Single', |
||
| 212 | 'AdditionalDocumentReference' => 'Multiple', |
||
| 213 | 'ProjectReference' => 'Single', |
||
| 214 | 'AccountingSupplierParty' => 'SingleCompany', |
||
| 215 | 'AccountingCustomerParty' => 'SingleCompany', |
||
| 216 | 'PayeeParty' => 'Single', |
||
| 217 | 'TaxRepresentativeParty' => 'Single', |
||
| 218 | 'Delivery' => 'Single', |
||
| 219 | 'PaymentMeans' => 'Multiple', |
||
| 220 | 'PaymentTerms' => 'Single', |
||
| 221 | 'DocumentReference' => 'Single', |
||
| 222 | 'AllowanceCharge' => 'Multiple', |
||
| 223 | 'TaxTotal' => 'Multiple', |
||
| 224 | 'LegalMonetaryTotal' => 'Single', |
||
| 225 | ]; |
||
| 226 | foreach ($arrayOptionalElementsHeader as $key => $strLogicType) { |
||
| 227 | if (array_key_exists($key, $arrayAggregates)) { |
||
| 228 | switch ($strLogicType) { |
||
| 229 | case 'Multiple': |
||
| 230 | $this->setMultipleElementsOrdered([ |
||
| 231 | 'commentParentKey' => $key, |
||
| 232 | 'data' => $arrayAggregates[$key], |
||
| 233 | 'tag' => $key, |
||
| 234 | ]); |
||
| 235 | break; |
||
| 236 | case 'Single': |
||
| 237 | $this->setElementsOrdered([ |
||
| 238 | 'commentParentKey' => $key, |
||
| 239 | 'data' => $arrayAggregates[$key], |
||
| 240 | 'tag' => $key, |
||
| 241 | ]); |
||
| 242 | break; |
||
| 243 | case 'SingleCompany': |
||
| 244 | $this->setElementsOrdered([ |
||
| 245 | 'commentParentKey' => $key, |
||
| 246 | 'data' => $arrayAggregates[$key]['Party'], |
||
| 247 | 'tag' => $key, |
||
| 248 | ]); |
||
| 249 | break; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | private function setSingleComment(array $arrayInput): void |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | private function setSingleElementWithAttribute(array $arrayInput): void |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | public function writeElectronicInvoice(string $strFile, array $inData, bool $bolCmnts, bool $bolScLc = false): void |
||
| 298 | } |
||
| 299 | } |
||
| 300 |