| Total Complexity | 58 |
| Total Lines | 332 |
| 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 ElectronicInvoiceWrite |
||
| 32 | { |
||
| 33 | |||
| 34 | use TraitVersions; |
||
| 35 | |||
| 36 | protected \XMLWriter $objXmlWriter; |
||
| 37 | |||
| 38 | private function setDocumentTag(array $arrayDocumentData): void |
||
| 39 | { |
||
| 40 | $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']); |
||
| 41 | foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) { |
||
| 42 | if ($key === '') { |
||
| 43 | $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']); |
||
| 44 | $this->objXmlWriter->writeAttributeNS(null, 'xmlns', null, $strValue); |
||
| 45 | } else { |
||
| 46 | $this->objXmlWriter->writeAttributeNS('xmlns', $key, null, $value); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | if (array_key_exists('SchemaLocation', $arrayDocumentData)) { |
||
| 50 | $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | private function setElementComment(string $strKey): void |
||
| 55 | { |
||
| 56 | if (array_key_exists($strKey, $this->arraySettings['Comments'])) { |
||
| 57 | $elementComment = $this->arraySettings['Comments'][$strKey]; |
||
| 58 | if (is_array($elementComment)) { |
||
| 59 | foreach ($elementComment as $value) { |
||
| 60 | $this->objXmlWriter->writeComment($value); |
||
| 61 | } |
||
| 62 | } else { |
||
| 63 | $this->objXmlWriter->writeComment($elementComment); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | private function setElementsOrdered(array $arrayInput): void |
||
| 69 | { |
||
| 70 | $this->setElementComment($arrayInput['commentParentKey']); |
||
| 71 | $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']); |
||
| 72 | $this->setExtraElement($arrayInput, 'Start'); |
||
| 73 | $arrayCustomOrder = $this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']]; |
||
| 74 | foreach ($arrayCustomOrder as $value) { // get the values in expected order |
||
| 75 | if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional |
||
| 76 | $key = implode('_', [$arrayInput['commentParentKey'], $value]); |
||
| 77 | $matches = []; |
||
| 78 | preg_match('/^.*(Amount|Quantity)$/', $value, $matches, PREG_OFFSET_CAPTURE); |
||
| 79 | $strCategory = $this->setCategorizedVerifications([ |
||
| 80 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 81 | 'data' => $arrayInput['data'][$value], |
||
| 82 | 'matches' => $matches, |
||
| 83 | 'tag' => $value, |
||
| 84 | ]); |
||
| 85 | switch ($strCategory) { |
||
| 86 | case 'ArrayElementsOrdered': |
||
| 87 | foreach ($arrayInput['data'][$value] as $value2) { |
||
| 88 | $this->setElementsOrdered([ |
||
| 89 | 'commentParentKey' => $key, |
||
| 90 | 'data' => $value2, |
||
| 91 | 'tag' => $value, |
||
| 92 | ]); |
||
| 93 | } |
||
| 94 | break; |
||
| 95 | case 'ElementsOrdered': |
||
| 96 | $this->setElementsOrdered([ |
||
| 97 | 'commentParentKey' => $key, |
||
| 98 | 'data' => $arrayInput['data'][$value], |
||
| 99 | 'tag' => $value, |
||
| 100 | ]); |
||
| 101 | break; |
||
| 102 | case 'MultipleElementsOrdered': |
||
| 103 | $this->setMultipleElementsOrdered([ |
||
| 104 | 'commentParentKey' => $key, |
||
| 105 | 'data' => $arrayInput['data'][$value], |
||
| 106 | 'tag' => $value, |
||
| 107 | ]); |
||
| 108 | break; |
||
| 109 | case 'SingleElementWithAttribute': |
||
| 110 | $this->setSingleElementWithAttribute([ |
||
| 111 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 112 | 'data' => $arrayInput['data'][$value], |
||
| 113 | 'tag' => $value, |
||
| 114 | ]); |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | $this->setExtraElement($arrayInput, 'End'); |
||
| 120 | $this->objXmlWriter->endElement(); // $key |
||
| 121 | } |
||
| 122 | |||
| 123 | private function setExtraElement(array $arrayInput, string $strType): void |
||
| 124 | { |
||
| 125 | if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) { |
||
| 126 | switch ($strType) { |
||
| 127 | case 'End': |
||
| 128 | $this->objXmlWriter->endElement(); |
||
| 129 | break; |
||
| 130 | case 'Start': |
||
| 131 | $this->objXmlWriter->startElement('cac:Party'); |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | private function setHeaderCommonBasicComponents(array $arrayElementWithData): void |
||
| 138 | { |
||
| 139 | $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header_CBC']; |
||
| 140 | foreach ($arrayCustomOrdered as $value) { |
||
| 141 | if (array_key_exists($value, $arrayElementWithData)) { |
||
| 142 | $this->setElementComment($value); |
||
| 143 | $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | private function setMultipleElementsOrdered(array $arrayData): void |
||
| 149 | { |
||
| 150 | foreach ($arrayData['data'] as $value) { |
||
| 151 | $strCommentParentKey = $this->setManageComment($arrayData['commentParentKey'], $value); |
||
| 152 | $this->setElementsOrdered([ |
||
| 153 | 'commentParentKey' => $strCommentParentKey, |
||
| 154 | 'data' => $value, |
||
| 155 | 'tag' => $arrayData['tag'], |
||
| 156 | ]); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | private function setPrepareXml(string $strFile, int $intIdent = 4): void |
||
| 161 | { |
||
| 162 | $this->objXmlWriter = new \XMLWriter(); |
||
| 163 | $this->objXmlWriter->openURI($strFile); |
||
| 164 | $this->objXmlWriter->setIndent(true); |
||
| 165 | $this->objXmlWriter->setIndentString(str_repeat(' ', $intIdent)); |
||
| 166 | $this->objXmlWriter->startDocument('1.0', 'UTF-8'); |
||
| 167 | } |
||
| 168 | |||
| 169 | private function setProduceMiddleXml(array $arrayData): void |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | private function setSingleComment(array $arrayInput): void |
||
| 203 | { |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | private function setSingleElementWithAttribute(array $arrayInput): void |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | public function writeElectronicInvoice(string $strFile, array $inData, array $arrayFeatures): void |
||
| 249 | } |
||
| 250 | } |
||
| 251 |