Total Complexity | 66 |
Total Lines | 268 |
Duplicated Lines | 0 % |
Coverage | 12.66% |
Changes | 11 | ||
Bugs | 0 | Features | 3 |
Complex classes like XmlSerializer 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 XmlSerializer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class XmlSerializer implements Serializer |
||
28 | { |
||
29 | /** @var string The key name of the node value */ |
||
30 | private string $val = '#'; |
||
31 | |||
32 | private string|null $root; |
||
1 ignored issue
–
show
|
|||
33 | 2 | private DOMDocument $document; |
|
34 | |||
35 | 2 | public function __construct(?string $root, string $nodeKey = '#') |
|
36 | 2 | { |
|
37 | $this->root = $root; |
||
38 | $nodeKey = \trim($nodeKey); |
||
39 | if ('@' === $nodeKey || empty($nodeKey)) { |
||
40 | throw new \InvalidArgumentException('Invalid node key identifier', self::E_INVALID_SERIALIZER); |
||
41 | } |
||
42 | $this->val = $nodeKey; |
||
43 | } |
||
44 | |||
45 | public function type(): string |
||
46 | { |
||
47 | return Serializer::XML; |
||
48 | } |
||
49 | |||
50 | final public function val(): string |
||
51 | { |
||
52 | return $this->val; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param iterable $data |
||
57 | * |
||
58 | * @return string XML |
||
59 | */ |
||
60 | public function serialize(mixed $data): ?string |
||
61 | 1 | { |
|
62 | $this->document = new DOMDocument('1.0', 'UTF-8'); |
||
63 | 1 | $this->document->formatOutput = false; |
|
64 | if (\is_iterable($data)) { |
||
65 | $root = $this->document->createElement($this->root); |
||
66 | 1 | $this->document->appendChild($root); |
|
67 | 1 | $this->document->createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi:' . $this->root); |
|
68 | 1 | $this->buildXml($root, $data); |
|
69 | } else { |
||
70 | $this->appendNode($this->document, $data, $this->root); |
||
71 | } |
||
72 | return $this->document->saveXML(); |
||
73 | } |
||
74 | 1 | ||
75 | /** |
||
76 | 1 | * Unserialize a proper XML document into array, scalar value or NULL. |
|
77 | * |
||
78 | * @param string $xml XML |
||
79 | * |
||
80 | * @return mixed scalar|array|null |
||
81 | */ |
||
82 | public function unserialize(string $xml): mixed |
||
83 | { |
||
84 | try { |
||
85 | $document = new DOMDocument('1.0', 'UTF-8'); |
||
86 | $document->preserveWhiteSpace = false; |
||
87 | $document->loadXML($xml); |
||
88 | if ($document->documentElement->hasChildNodes()) { |
||
89 | return $this->parseXml($document->documentElement); |
||
90 | } |
||
91 | return false === $document->documentElement->getAttributeNode('xmlns:xsi') |
||
92 | ? $this->parseXml($document->documentElement) |
||
93 | : []; |
||
94 | |||
95 | } catch (Throwable $e) { |
||
96 | \error_log(PHP_EOL . "[{$e->getLine()}]: " . $e->getMessage()); |
||
97 | return null; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | private function buildXml(DOMNode $parent, iterable $data): void |
||
102 | { |
||
103 | foreach ($data as $key => $data) { |
||
104 | $isKeyNumeric = \is_numeric($key); |
||
105 | if (0 === \strpos($key, '@') && $name = \substr($key, 1)) { |
||
106 | // a node attribute |
||
107 | $parent->setAttribute($name, $data); |
||
108 | } elseif ($this->val === $key) { |
||
109 | // the node value |
||
110 | $parent->nodeValue = $data; |
||
111 | } elseif (false === $isKeyNumeric && \is_array($data)) { |
||
112 | if (\ctype_digit(\join('', \array_keys($data)))) { |
||
113 | foreach ($data as $d) { |
||
114 | $this->appendNode($parent, $d, $key); |
||
115 | } |
||
116 | } else { |
||
117 | $this->appendNode($parent, $data, $key); |
||
118 | } |
||
119 | } elseif ($isKeyNumeric) { |
||
120 | $this->appendNode($parent, $data, 'item', $key); |
||
121 | } else { |
||
122 | $this->appendNode($parent, $data, $key); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | |||
127 | private function parseXml(DOMNode $node) |
||
128 | { |
||
129 | $attrs = $this->parseXmlAttributes($node); |
||
130 | $value = $this->parseXmlValue($node); |
||
131 | if (0 === \count($attrs)) { |
||
132 | return $value; |
||
133 | } |
||
134 | if (false === \is_array($value)) { |
||
135 | $attrs[$this->val] = $value; |
||
136 | return $this->getValueByType($attrs); |
||
137 | } |
||
138 | if (1 === \count($value) && \key($value)) { |
||
139 | $attrs[\key($value)] = \current($value); |
||
140 | } |
||
141 | foreach ($value as $k => $v) { |
||
142 | $attrs[$k] = $v; |
||
143 | } |
||
144 | return $attrs; |
||
145 | } |
||
146 | |||
147 | private function parseXmlAttributes(DOMNode $node): array |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param DOMNode $node |
||
162 | * |
||
163 | * @return array|string|null |
||
164 | * @throws \Exception |
||
165 | */ |
||
166 | private function parseXmlValue(DOMNode $node) |
||
167 | { |
||
168 | $value = []; |
||
169 | if ($node->hasChildNodes()) { |
||
170 | /** @var DOMNode $child */ |
||
171 | $child = $node->firstChild; |
||
172 | if ($child->nodeType === XML_TEXT_NODE) { |
||
173 | return $child->nodeValue; |
||
174 | } |
||
175 | if ($child->nodeType === XML_CDATA_SECTION_NODE) { |
||
176 | return $child->wholeText; |
||
177 | } |
||
178 | foreach ($node->childNodes as $child) { |
||
179 | if ($child->nodeType === XML_COMMENT_NODE) { |
||
180 | continue; |
||
181 | } |
||
182 | $v = $this->parseXml($child); |
||
183 | if ('item' === $child->nodeName && isset($v['@key'])) { |
||
184 | $k = $v['@key']; |
||
185 | $value[$k] = $this->getValueByType($v); |
||
186 | unset($value[$k]['@key']); |
||
187 | } else { |
||
188 | $value[$child->nodeName][] = $this->getValueByType($v); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | foreach ($value as $k => $v) { |
||
193 | if (\is_array($v) && 1 === \count($v)) { |
||
194 | $value[$k] = \current($v); |
||
195 | } |
||
196 | } |
||
197 | return $value ?: ''; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Creates an XML node in the document from the provided value |
||
202 | * according to the PHP type of the value. |
||
203 | * |
||
204 | * @param DOMNode $parent |
||
205 | * @param mixed $data |
||
206 | * @param string $name |
||
207 | * @param string|null $key |
||
208 | */ |
||
209 | private function appendNode(DOMNode $parent, $data, string $name, string $key = null): void |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Deserialize the XML document elements into strict PHP values |
||
244 | * in regard to the XSD type defined in the XML element (if any). |
||
245 | * |
||
246 | * IMPORTANT: When deserializing an XML document into values, |
||
247 | * if the XmlSerializer encounters an XML element that specifies xsi:nil="true", |
||
248 | * it assigns a NULL to the corresponding element and ignores any other attributes |
||
249 | * |
||
250 | * @param array|string $value |
||
251 | * @return array|string|null |
||
252 | * @throws \Exception |
||
253 | */ |
||
254 | private function getValueByType($value) |
||
295 | } |
||
296 | } |
||
297 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths