Complex classes like XMLNode 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 XMLNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class XMLNode |
||
14 | { |
||
15 | /** |
||
16 | * DOM Document |
||
17 | * |
||
18 | * @var \DOMDocument |
||
19 | */ |
||
20 | protected $document; |
||
21 | |||
22 | /** |
||
23 | * DOM Node |
||
24 | * |
||
25 | * @var \DOMNode|null |
||
26 | */ |
||
27 | private $node; |
||
28 | |||
29 | /** |
||
30 | * XMLNode constructor |
||
31 | * |
||
32 | * @param \DOMDocument $document |
||
33 | * @param \DOMNode|null $element |
||
34 | */ |
||
35 | 64 | protected function __construct(DOMDocument $document, ?DOMNode $element) |
|
40 | |||
41 | /** |
||
42 | * Add element to XML node |
||
43 | * |
||
44 | * @param string $name |
||
45 | * @param array<string,string> $attributes |
||
46 | * |
||
47 | * @return \Inspirum\XML\Services\XMLNode |
||
48 | */ |
||
49 | 34 | public function addElement(string $name, array $attributes = []): XMLNode |
|
53 | |||
54 | /** |
||
55 | * Add text element |
||
56 | * |
||
57 | * @param string $name |
||
58 | * @param mixed $value |
||
59 | * @param array<string,string> $attributes |
||
60 | * @param bool $forcedEscape |
||
61 | * |
||
62 | * @return \Inspirum\XML\Services\XMLNode |
||
63 | */ |
||
64 | 38 | public function addTextElement(string $name, $value, array $attributes = [], bool $forcedEscape = false): XMLNode |
|
72 | |||
73 | /** |
||
74 | * Add XML data |
||
75 | * |
||
76 | * @param string $content |
||
77 | * |
||
78 | * @return \Inspirum\XML\Services\XMLNode|null |
||
79 | */ |
||
80 | 4 | public function addXMLData(string $content): ?XMLNode |
|
92 | |||
93 | /** |
||
94 | * Create new (unconnected) element |
||
95 | * |
||
96 | * @param string $name |
||
97 | * @param array<string,string> $attributes |
||
98 | * |
||
99 | * @return \Inspirum\XML\Services\XMLNode |
||
100 | */ |
||
101 | 2 | public function createElement(string $name, array $attributes = []): XMLNode |
|
105 | |||
106 | /** |
||
107 | * Create new (unconnected) text element |
||
108 | * |
||
109 | * @param string $name |
||
110 | * @param mixed $value |
||
111 | * @param array<string,string> $attributes |
||
112 | * @param bool $forcedEscape |
||
113 | * |
||
114 | * @return \Inspirum\XML\Services\XMLNode |
||
115 | */ |
||
116 | 10 | public function createTextElement(string $name, $value, array $attributes = [], bool $forcedEscape = false): XMLNode |
|
122 | |||
123 | /** |
||
124 | * Append node to parent node. |
||
125 | * |
||
126 | * @param \Inspirum\XML\Services\XMLNode $element |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | 6 | public function append(XMLNode $element): void |
|
136 | |||
137 | /** |
||
138 | * Create new DOM element. |
||
139 | * |
||
140 | * @param string $name |
||
141 | * @param string|float|int|bool|null $value |
||
142 | * @param array<string,string> $attributes |
||
143 | * @param bool $forcedEscape |
||
144 | * |
||
145 | * @return \DOMElement |
||
146 | */ |
||
147 | 48 | private function createFullDOMElement( |
|
165 | |||
166 | /** |
||
167 | * Create new DOM fragment element |
||
168 | * |
||
169 | * @param string $content |
||
170 | * |
||
171 | * @return \DOMDocumentFragment |
||
172 | */ |
||
173 | 3 | private function createDOMFragment(string $content): DOMDocumentFragment |
|
180 | |||
181 | /** |
||
182 | * Create new DOM element with namespace if exists |
||
183 | * |
||
184 | * @param string $name |
||
185 | * @param string|null $value |
||
186 | * |
||
187 | * @return \DOMElement |
||
188 | */ |
||
189 | 45 | private function createDOMElementNS(string $name, $value = null): DOMElement |
|
200 | |||
201 | /** |
||
202 | * Set node value to element |
||
203 | * |
||
204 | * @param \DOMElement $element |
||
205 | * @param mixed $value |
||
206 | * @param bool $forcedEscape |
||
207 | * |
||
208 | * @return void |
||
209 | */ |
||
210 | 42 | private function setDOMElementValue(DOMElement $element, $value, bool $forcedEscape = false): void |
|
228 | |||
229 | /** |
||
230 | * Create new DOM attribute with namespace if exists |
||
231 | * |
||
232 | * @param \DOMElement $element |
||
233 | * @param string $name |
||
234 | * @param string|float|int $value |
||
235 | * |
||
236 | * @return void |
||
237 | */ |
||
238 | 25 | private function setDOMAttributeNS(DOMElement $element, string $name, $value): void |
|
251 | |||
252 | /** |
||
253 | * Append child to parent node. |
||
254 | * |
||
255 | * @param \DOMNode $element |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | 38 | private function appendChild(DOMNode $element): void |
|
264 | |||
265 | /** |
||
266 | * Register xmlns namespace URLs |
||
267 | * |
||
268 | * @param array<string,string> $attributes |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | 48 | private function registerNamespaces(array $attributes): void |
|
282 | |||
283 | /** |
||
284 | * Return valid XML string. |
||
285 | * |
||
286 | * @param bool $formatOutput |
||
287 | * |
||
288 | * @return string |
||
289 | * |
||
290 | * @throws \DOMException |
||
291 | */ |
||
292 | 25 | public function toString(bool $formatOutput = false): string |
|
310 | |||
311 | /** |
||
312 | * Convert to array |
||
313 | * |
||
314 | * @param \Inspirum\XML\Model\Values\Config|null $options |
||
315 | * |
||
316 | * @return array<int|string,mixed> |
||
317 | */ |
||
318 | 12 | public function toArray(Config $options = null): array |
|
328 | |||
329 | /** |
||
330 | * Get node text content |
||
331 | * |
||
332 | * @return string|null |
||
333 | */ |
||
334 | 7 | public function getTextContent(): ?string |
|
342 | |||
343 | /** |
||
344 | * Convert node to array |
||
345 | * |
||
346 | * @param \DOMNode $node |
||
347 | * @param \Inspirum\XML\Model\Values\Config $options |
||
348 | * |
||
349 | * @return array<int|string,mixed>|string|null |
||
350 | */ |
||
351 | 12 | private function nodeToArray(DOMNode $node, Config $options) |
|
394 | |||
395 | /** |
||
396 | * Remove unnecessary data |
||
397 | * |
||
398 | * @param array<int|string,mixed> $result |
||
399 | * @param \Inspirum\XML\Model\Values\Config $options |
||
400 | * @param \DOMNode $node |
||
401 | * |
||
402 | * @return array<int|string,mixed> |
||
403 | */ |
||
404 | 10 | private function simplifyArray(array $result, Config $options, DOMNode $node): array |
|
427 | |||
428 | /** |
||
429 | * Register custom error handler to throw Exception on warning message |
||
430 | * |
||
431 | * @param callable $callback |
||
432 | * |
||
433 | * @return mixed |
||
434 | * |
||
435 | * @throws \DOMException |
||
436 | */ |
||
437 | 35 | protected function withErrorHandler(callable $callback) |
|
451 | |||
452 | /** |
||
453 | * Convert to string |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | 1 | public function __toString() |
|
461 | } |
||
462 |