| Total Complexity | 42 |
| Total Lines | 214 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | private function setElementComment(string $strKey): void |
||
| 74 | { |
||
| 75 | if (array_key_exists($strKey, $this->arraySettings['Comments'])) { |
||
| 76 | $elementComment = $this->arraySettings['Comments'][$strKey]; |
||
| 77 | if (is_array($elementComment)) { |
||
| 78 | foreach ($elementComment as $value) { |
||
| 79 | $this->objXmlWriter->writeComment($value); |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | $this->objXmlWriter->writeComment($elementComment); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | private function setElementsOrdered(array $arrayInput): void |
||
| 88 | { |
||
| 89 | $this->setElementComment($arrayInput['commentParentKey']); |
||
| 90 | $this->objXmlWriter->startElement('cac:' . $arrayInput['tag']); |
||
| 91 | $this->setExtraElement($arrayInput, 'Start'); |
||
| 92 | foreach ($this->arraySettings['CustomOrder'][$arrayInput['commentParentKey']] as $value) { |
||
| 93 | if (array_key_exists($value, $arrayInput['data'])) { // because certain value are optional |
||
| 94 | $key = implode('_', [$arrayInput['commentParentKey'], $value]); |
||
| 95 | $matches = []; |
||
| 96 | preg_match('/^.*(Amount|Quantity)$/', $value, $matches, PREG_OFFSET_CAPTURE); |
||
| 97 | $strCategory = $this->setCategorizedVerifications([ |
||
| 98 | 'commentParentKey' => $arrayInput['commentParentKey'], |
||
| 99 | 'data' => $arrayInput['data'][$value], |
||
| 100 | 'matches' => $matches, |
||
| 101 | 'tag' => $value, |
||
| 102 | ]); |
||
| 103 | $this->setDecisionElements($arrayInput, $key, $value, $strCategory); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | $this->setExtraElement($arrayInput, 'End'); |
||
| 107 | $this->objXmlWriter->endElement(); // $arrayInput['tag'] |
||
| 108 | } |
||
| 109 | |||
| 110 | private function setExtraElement(array $arrayInput, string $strType): void |
||
| 111 | { |
||
| 112 | if (in_array($arrayInput['tag'], ['AccountingCustomerParty', 'AccountingSupplierParty'])) { |
||
| 113 | switch ($strType) { |
||
| 114 | case 'End': |
||
| 115 | $this->objXmlWriter->endElement(); |
||
| 116 | break; |
||
| 117 | case 'Start': |
||
| 118 | $this->objXmlWriter->startElement('cac:Party'); |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | private function setHeaderCommonBasicComponents(array $arrayElementWithData): void |
||
| 125 | { |
||
| 126 | $arrayCustomOrdered = $this->arraySettings['CustomOrder']['Header_CBC']; |
||
| 127 | foreach ($arrayCustomOrdered as $value) { |
||
| 128 | if (array_key_exists($value, $arrayElementWithData)) { |
||
| 129 | $this->setElementComment($value); |
||
| 130 | $this->objXmlWriter->writeElement('cbc:' . $value, $arrayElementWithData[$value]); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | private function setMultipleElementsOrdered(array $arrayData): void |
||
| 136 | { |
||
| 137 | foreach ($arrayData['data'] as $value) { |
||
| 138 | $strCommentParentKey = $this->setManageComment($arrayData['commentParentKey'], $value); |
||
| 139 | $this->setElementsOrdered([ |
||
| 140 | 'commentParentKey' => $strCommentParentKey, |
||
| 141 | 'data' => $value, |
||
| 142 | 'tag' => $arrayData['tag'], |
||
| 143 | ]); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | private function setPrepareXml(string $strFile, int $intIdent = 4, array $arrayDocumentData): void |
||
| 148 | { |
||
| 149 | $this->objXmlWriter = new \XMLWriter(); |
||
| 150 | $this->objXmlWriter->openURI($strFile); |
||
| 151 | $this->objXmlWriter->setIndent(true); |
||
| 152 | $this->objXmlWriter->setIndentString(str_repeat(' ', $intIdent)); |
||
| 153 | $this->objXmlWriter->startDocument('1.0', 'UTF-8'); |
||
| 154 | $this->objXmlWriter->startElement($arrayDocumentData['DocumentTagName']); |
||
| 155 | foreach ($arrayDocumentData['DocumentNameSpaces'] as $key => $value) { |
||
| 156 | if ($key === '') { |
||
| 157 | $strValue = sprintf($value, $arrayDocumentData['DocumentTagName']); |
||
| 158 | $this->objXmlWriter->writeAttributeNS(null, 'xmlns', null, $strValue); |
||
| 159 | } else { |
||
| 160 | $this->objXmlWriter->writeAttributeNS('xmlns', $key, null, $value); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | if (array_key_exists('SchemaLocation', $arrayDocumentData)) { |
||
| 164 | $this->objXmlWriter->writeAttribute('xsi:schemaLocation', $arrayDocumentData['SchemaLocation']); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | private function setProduceMiddleXml(array $arrayData): void |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | private function setSingleComment(array $arrayInput): void |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | private function setSingleElementWithAttribute(array $arrayInput): void |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | public function writeElectronicInvoice(string $strFile, array $inData, array $arrayFeatures): void |
||
| 245 | } |
||
| 246 | } |
||
| 247 |