| Total Complexity | 43 |
| Total Lines | 219 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ElectronicInvoiceWrite 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 ElectronicInvoiceWrite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ElectronicInvoiceWrite |
||
| 32 | { |
||
| 33 | use TraitVersions; |
||
| 34 | |||
| 35 | protected \XMLWriter $objXmlWriter; |
||
| 36 | |||
| 37 | private function setDecisionElements(array $arrayInput, string $strKey, string $strTag, string $strCategory): void |
||
| 38 | { |
||
| 39 | switch ($strCategory) { |
||
| 40 | case 'ArrayElementsOrdered': |
||
| 41 | foreach ($arrayInput['data'][$strTag] as $value2) { |
||
| 42 | $this->setElementsOrdered([ |
||
| 43 | 'commentParentKey' => $strKey, |
||
| 44 | 'data' => $value2, |
||
| 45 | 'tag' => $strTag, |
||
| 46 | ]); |
||
| 47 | } |
||
| 48 | break; |
||
| 49 | case 'ElementsOrdered': |
||
| 50 | $this->setElementsOrdered([ |
||
| 51 | 'commentParentKey' => $strKey, |
||
| 52 | 'data' => $arrayInput['data'][$strTag], |
||
| 53 | 'tag' => $strTag, |
||
| 54 | ]); |
||
| 55 | break; |
||
| 56 | case 'MultipleElementsOrdered': |
||
| 57 | $this->setMultipleElementsOrdered([ |
||
| 58 | 'commentParentKey' => $strKey, |
||
| 59 | 'data' => $arrayInput['data'][$strTag], |
||
| 60 | 'tag' => $strTag, |
||
| 61 | ]); |
||
| 62 | break; |
||
| 63 | case 'SingleElementWithAttribute': |
||
| 64 | $this->setSingleElementWithAttribute([ |
||
| 65 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 66 | 'data' => $arrayInput['data'][$strTag], |
||
| 67 | 'tag' => $strTag, |
||
| 68 | ]); |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | private function setDocumentTag(array $arrayDocumentData): void |
||
| 74 | { |
||
| 75 | $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']); |
||
| 76 | foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) { |
||
| 77 | if ($key === '') { |
||
| 78 | $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']); |
||
| 79 | $this->objXmlWriter->writeAttributeNS(null, 'xmlns', null, $strValue); |
||
| 80 | } else { |
||
| 81 | $this->objXmlWriter->writeAttributeNS('xmlns', $key, null, $value); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | if (array_key_exists('SchemaLocation', $arrayDocumentData)) { |
||
| 85 | $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | private function setElementComment(string $strKey): void |
||
| 90 | { |
||
| 91 | if (array_key_exists($strKey, $this->arraySettings['Comments'])) { |
||
| 92 | $elementComment = $this->arraySettings['Comments'][$strKey]; |
||
| 93 | if (is_array($elementComment)) { |
||
| 94 | foreach ($elementComment as $value) { |
||
| 95 | $this->objXmlWriter->writeComment($value); |
||
| 96 | } |
||
| 97 | } else { |
||
| 98 | $this->objXmlWriter->writeComment($elementComment); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | private function setElementsOrdered(array $arrayInput): void |
||
| 104 | { |
||
| 105 | $this->setElementComment($arrayInput['commentParentKey']); |
||
| 106 | $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']); |
||
| 107 | $this->setExtraElement($arrayInput, 'Start'); |
||
| 108 | foreach ($this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']] as $value) { |
||
| 109 | if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional |
||
| 110 | $key = implode('_', [$arrayInput['commentParentKey'], $value]); |
||
| 111 | $matches = []; |
||
| 112 | preg_match('/^.*(Amount|Quantity)$/', $value, $matches, PREG_OFFSET_CAPTURE); |
||
| 113 | $strCategory = $this->setCategorizedVerifications([ |
||
| 114 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 115 | 'data' => $arrayInput['data'][$value], |
||
| 116 | 'matches' => $matches, |
||
| 117 | 'tag' => $value, |
||
| 118 | ]); |
||
| 119 | $this->setDecisionElements($arrayInput, $key, $value, $strCategory); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | $this->setExtraElement($arrayInput, 'End'); |
||
| 123 | $this->objXmlWriter->endElement(); // $arrayInput['tag'] |
||
| 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 |
||
| 141 | { |
||
| 142 | $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header_CBC']; |
||
| 143 | foreach ($arrayCustomOrdered as $value) { |
||
| 144 | if (array_key_exists($value, $arrayElementWithData)) { |
||
| 145 | $this->setElementComment($value); |
||
| 146 | $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | private function setMultipleElementsOrdered(array $arrayData): void |
||
| 152 | { |
||
| 153 | foreach ($arrayData['data'] as $value) { |
||
| 154 | $strCommentParentKey = $this->setManageComment($arrayData['commentParentKey'], $value); |
||
| 155 | $this->setElementsOrdered([ |
||
| 156 | 'commentParentKey' => $strCommentParentKey, |
||
| 157 | 'data' => $value, |
||
| 158 | 'tag' => $arrayData['tag'], |
||
| 159 | ]); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | private function setPrepareXml(string $strFile, int $intIdent = 4): void |
||
| 164 | { |
||
| 165 | $this->objXmlWriter = new \XMLWriter(); |
||
| 166 | $this->objXmlWriter->openURI($strFile); |
||
| 167 | $this->objXmlWriter->setIndent(true); |
||
| 168 | $this->objXmlWriter->setIndentString(str_repeat(' ', $intIdent)); |
||
| 169 | $this->objXmlWriter->startDocument('1.0', 'UTF-8'); |
||
| 170 | } |
||
| 171 | |||
| 172 | private function setProduceMiddleXml(array $arrayData): void |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | private function setSingleComment(array $arrayInput): void |
||
| 204 | { |
||
| 205 | if (array_key_exists('commentParentKey', $arrayInput)) { |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | private function setSingleElementWithAttribute(array $arrayInput): void |
||
| 215 | { |
||
| 216 | $this->setSingleComment($arrayInput); |
||
| 217 | if (is_array($arrayInput['data']) && array_key_exists('value', $arrayInput['data'])) { |
||
| 218 | $this->objXmlWriter->startElement('cbc:' . $arrayInput['tag']); |
||
| 219 | foreach ($arrayInput['data'] as $key => $value) { |
||
| 220 | if ($key !== 'value') { // if is not value, must be an attribute |
||
| 221 | $this->objXmlWriter->writeAttribute($key, $value); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | $this->objXmlWriter->writeRaw($this->setNumericValue($arrayInput['tag'], $arrayInput['data'])); |
||
| 225 | $this->objXmlWriter->endElement(); |
||
| 226 | } else { |
||
| 227 | $this->objXmlWriter->writeElement('cbc:' . $arrayInput['tag'], $arrayInput['data']); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | public function writeElectronicInvoice(string $strFile, array $inData, array $arrayFeatures): void |
||
| 250 | } |
||
| 251 | } |
||
| 252 |