Complex classes like XmlElement 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 XmlElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class XmlElement |
||
41 | { |
||
42 | use Accessors; |
||
43 | |||
44 | /** |
||
45 | * Settings for tiding up XML output |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | public static $tidy = [ |
||
50 | 'indent' => true, |
||
51 | 'input-xml' => true, |
||
52 | 'output-xml' => true, |
||
53 | 'drop-empty-paras' => false, |
||
54 | 'wrap' => 0 |
||
55 | ]; |
||
56 | |||
57 | /** @var string */ |
||
58 | private $_localName; |
||
59 | /** @var null|string|false */ |
||
60 | private $_prefix = null; |
||
61 | |||
62 | /** @var array */ |
||
63 | private $_namespaces = []; |
||
64 | /** @var array */ |
||
65 | private $_attributes = []; |
||
66 | |||
67 | /** |
||
68 | * @var XmlElement |
||
69 | */ |
||
70 | private $_parent; |
||
71 | |||
72 | /** |
||
73 | * @var XmlElement[] |
||
74 | */ |
||
75 | private $_children = []; |
||
76 | |||
77 | /** |
||
78 | * Initializes element with given name and URI |
||
79 | * |
||
80 | * @param string $name Element name, including prefix if needed |
||
81 | * @param string $uri Namespace URI of element |
||
82 | */ |
||
83 | 19 | protected function init(string $name, string $uri = null) |
|
94 | |||
95 | /** |
||
96 | * XmlElement constructor |
||
97 | * |
||
98 | * @param string $name Element name, including prefix if needed |
||
99 | * @param string $uri Namespace URI of element |
||
100 | */ |
||
101 | 15 | public function __construct(string $name, string $uri = null) |
|
105 | |||
106 | /** |
||
107 | * Elements named constructor, same for every subclass. |
||
108 | * It's used for factory creation. |
||
109 | * |
||
110 | * @param string $name Element name, including prefix if needed |
||
111 | * @param string $uri Namespace URI of element |
||
112 | * |
||
113 | * @return XmlElement |
||
114 | */ |
||
115 | 4 | public static function plain(string $name, string $uri = null) |
|
123 | |||
124 | /** |
||
125 | * @see $innerXml |
||
126 | * @return string |
||
127 | */ |
||
128 | 2 | public function getInnerXml() |
|
140 | |||
141 | /** |
||
142 | * Returns XML representation of element |
||
143 | * |
||
144 | * @param bool $clean Result will be cleaned if set to true |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | 2 | public function xml(bool $clean = true): string |
|
169 | |||
170 | /** |
||
171 | * Looks up prefix associated with given URI |
||
172 | * |
||
173 | * @param string|null $uri |
||
174 | * @return string|false |
||
175 | */ |
||
176 | 9 | public function lookupPrefix(string $uri = null) |
|
180 | |||
181 | /** |
||
182 | * Looks up URI associated with given prefix |
||
183 | * |
||
184 | * @param string|null $prefix |
||
185 | * @return string|false |
||
186 | */ |
||
187 | 13 | public function lookupUri(string $prefix = null) |
|
191 | |||
192 | /** |
||
193 | * Returns element's namespaces |
||
194 | * |
||
195 | * @param bool $parent Include namespaces from parent? |
||
196 | * @return array |
||
197 | */ |
||
198 | 16 | public function getNamespaces($parent = true): array |
|
210 | |||
211 | /** |
||
212 | * Sets XML attribute of element |
||
213 | * |
||
214 | * For `http://www.w3.org/2000/xmlns/` URI it acts like `setNamespace($value, $attribute)` |
||
215 | * |
||
216 | * @param string $attribute Attribute name, optionally with prefix |
||
217 | * @param mixed $value Attribute value |
||
218 | * @param string|null $uri XML Namespace URI of attribute, prefix will be automatically looked up |
||
219 | */ |
||
220 | 5 | public function setAttribute(string $attribute, $value, string $uri = null) |
|
229 | |||
230 | /** |
||
231 | 3 | * Returns value of specified attribute. |
|
232 | 3 | * |
|
233 | * For `http://www.w3.org/2000/xmlns/` URI it acts like `lookupUri($attribute)` |
||
234 | * |
||
235 | * @param string $attribute Attribute name, optionally with prefix |
||
236 | * @param string|null $uri XML Namespace URI of attribute, prefix will be automatically looked up |
||
237 | * @return bool|mixed |
||
238 | */ |
||
239 | public function getAttribute(string $attribute, string $uri = null) |
||
247 | |||
248 | /** |
||
249 | 2 | * Checks if attribute exists |
|
250 | 1 | * |
|
251 | * @param string $attribute Attribute name, optionally with prefix |
||
252 | * @param string|null $uri XML Namespace URI of attribute, prefix will be automatically looked up |
||
253 | 2 | * |
|
254 | * @return bool |
||
255 | */ |
||
256 | public function hasAttribute(string $attribute, string $uri = null) |
||
260 | 1 | ||
261 | /** |
||
262 | 1 | * Returns element's parent |
|
263 | * @return XmlElement|null |
||
264 | */ |
||
265 | public function getParent() |
||
269 | 8 | ||
270 | /** |
||
271 | 8 | * Sets element's parent |
|
272 | 1 | * @param XmlElement $parent |
|
273 | 1 | */ |
|
274 | protected function setParent(XmlElement $parent) |
||
286 | |||
287 | /** |
||
288 | * Appends child to element |
||
289 | 10 | * |
|
290 | * @param XmlElement|string $element |
||
291 | 10 | * |
|
292 | 1 | * @return XmlElement|string Same as $element |
|
293 | 1 | */ |
|
294 | 1 | public function append($element) |
|
309 | |||
310 | 13 | /** |
|
311 | * Returns namespace URI associated with element |
||
312 | 13 | * |
|
313 | * @return false|string |
||
314 | */ |
||
315 | public function getNamespace() |
||
319 | |||
320 | /** |
||
321 | * Adds namespace to element, and associates it with prefix. |
||
322 | 13 | * |
|
323 | * @param string $uri Namespace URI |
||
324 | 13 | * @param string|bool|null $prefix Prefix which will be used for namespace, false for using element's prefix |
|
325 | 10 | * and null for no prefix |
|
326 | */ |
||
327 | public function setNamespace(string $uri, $prefix = false) |
||
335 | |||
336 | 5 | public function getName() |
|
340 | |||
341 | 13 | public function getChildren() |
|
345 | |||
346 | 8 | public function getPrefix() |
|
350 | |||
351 | 6 | public function getLocalName() |
|
355 | |||
356 | public function getAttributes() |
||
360 | |||
361 | /** |
||
362 | * Returns one element at specified index (for default the first one). |
||
363 | * |
||
364 | * @param string $name Requested element tag name |
||
365 | 1 | * @param string $uri Requested element namespace |
|
366 | * @param int $index Index of element to retrieve |
||
367 | 1 | * |
|
368 | * @return XmlElement|false Retrieved element |
||
369 | */ |
||
370 | public function element(string $name, string $uri = null, int $index = 0) |
||
374 | |||
375 | /** |
||
376 | * Retrieves array of matching elements |
||
377 | * |
||
378 | 2 | * @param string $name Requested element tag name |
|
379 | * @param string|null $uri Requested element namespace |
||
380 | 2 | * |
|
381 | 2 | * @return XmlElement[] Found Elements |
|
382 | 1 | */ |
|
383 | public function elements($name, $uri = null) : array |
||
392 | |||
393 | /** |
||
394 | * Filters element with given predicate |
||
395 | 2 | * |
|
396 | * @param callable|string $predicate Predicate or class name |
||
397 | 2 | * |
|
398 | * @return XmlElement[] |
||
399 | */ |
||
400 | public function all($predicate) |
||
404 | 1 | ||
405 | /** |
||
406 | 1 | * @param string|null $query |
|
407 | * @return XPathQuery |
||
408 | */ |
||
409 | public function query(string $query = null) |
||
413 | |||
414 | 2 | /** |
|
415 | * Helper for retrieving all arguments (including namespaces) |
||
416 | 2 | * |
|
417 | 2 | * @return array |
|
418 | 1 | */ |
|
419 | 2 | private function attributes(): array |
|
431 | |||
432 | /** |
||
433 | * Prefixes $name with attribute associated with $uri |
||
434 | * |
||
435 | 3 | * @param string $name Name to prefix |
|
436 | * @param string $uri Namespace URI |
||
437 | 3 | * |
|
438 | 1 | * @return string |
|
439 | */ |
||
440 | protected function _prefix(string $name, string $uri = null): string |
||
452 | |||
453 | public function __toString() |
||
457 | 19 | ||
458 | 19 | /** |
|
459 | 2 | * Splits name into local-name and prefix |
|
460 | 2 | * |
|
461 | * @param $name |
||
462 | * @return array [$name, $prefix] |
||
463 | 19 | */ |
|
464 | public static function resolve($name) |
||
474 | } |
||
475 |