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 |
||
42 | class XmlElement implements ContainerInterface |
||
43 | { |
||
44 | use Accessors; |
||
45 | |||
46 | /** |
||
47 | * Settings for tiding up XML output |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | public static $tidy = [ |
||
52 | 'indent' => true, |
||
53 | 'input-xml' => true, |
||
54 | 'output-xml' => true, |
||
55 | 'drop-empty-paras' => false, |
||
56 | 'wrap' => 0 |
||
57 | ]; |
||
58 | |||
59 | /** @var string */ |
||
60 | private $_localName; |
||
61 | /** @var null|string|false */ |
||
62 | private $_prefix = null; |
||
63 | |||
64 | /** @var array */ |
||
65 | private $_namespaces = []; |
||
66 | /** @var array */ |
||
67 | private $_attributes = []; |
||
68 | |||
69 | /** |
||
70 | * @var XmlElement |
||
71 | */ |
||
72 | private $_parent; |
||
73 | |||
74 | /** |
||
75 | * @var XmlElement[] |
||
76 | */ |
||
77 | private $_children = []; |
||
78 | |||
79 | /** |
||
80 | * Initializes element with given name and URI |
||
81 | * |
||
82 | * @param string $name Element name, including prefix if needed |
||
83 | * @param string $uri Namespace URI of element |
||
84 | */ |
||
85 | 23 | protected function init(string $name, string $uri = null) |
|
96 | |||
97 | /** |
||
98 | * XmlElement constructor |
||
99 | * |
||
100 | * @param string $name Element name, including prefix if needed |
||
101 | * @param string $uri Namespace URI of element |
||
102 | * @param array $options { |
||
103 | * @var mixed $content Content of element |
||
104 | * @var array $attributes Element attributes |
||
105 | * } |
||
106 | */ |
||
107 | 19 | public function __construct(string $name, string $uri = null, array $options = []) |
|
113 | |||
114 | /** |
||
115 | * Elements named constructor, same for every subclass. |
||
116 | * It's used for factory creation. |
||
117 | * |
||
118 | * @param string $name Element name, including prefix if needed |
||
119 | * @param string $uri Namespace URI of element |
||
120 | * |
||
121 | * @return static |
||
122 | */ |
||
123 | 4 | public static function plain(string $name, string $uri = null) |
|
131 | |||
132 | /** |
||
133 | * @see $innerXml |
||
134 | * @return string |
||
135 | */ |
||
136 | 3 | public function getInnerXml() |
|
148 | |||
149 | public function setInnerXml($value) |
||
155 | |||
156 | public function getContent() |
||
160 | |||
161 | 1 | public function setContent($value) |
|
166 | |||
167 | /** |
||
168 | * Returns XML representation of element |
||
169 | * |
||
170 | * @param bool $clean Result will be cleaned if set to true |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 3 | public function xml(bool $clean = true): string |
|
195 | |||
196 | /** |
||
197 | * Looks up prefix associated with given URI |
||
198 | * |
||
199 | * @param string|null $uri |
||
200 | * @return string|false |
||
201 | */ |
||
202 | 12 | public function lookupPrefix(string $uri = null) |
|
206 | |||
207 | /** |
||
208 | * Looks up URI associated with given prefix |
||
209 | * |
||
210 | * @param string|null $prefix |
||
211 | * @return string|false |
||
212 | */ |
||
213 | 18 | public function lookupUri(string $prefix = null) |
|
217 | |||
218 | /** |
||
219 | * Returns element's namespaces |
||
220 | * |
||
221 | * @param bool $parent Include namespaces from parent? |
||
222 | * @return array |
||
223 | */ |
||
224 | 20 | public function getNamespaces($parent = true): array |
|
236 | |||
237 | /** |
||
238 | * Sets XML attribute of element |
||
239 | * |
||
240 | * @param string $attribute Attribute name, optionally with prefix |
||
241 | * @param mixed $value Attribute value |
||
242 | * @param string|null $uri XML Namespace URI of attribute, prefix will be automatically looked up |
||
243 | */ |
||
244 | 5 | public function setAttribute(string $attribute, $value, string $uri = null) |
|
255 | |||
256 | /** |
||
257 | * Returns value of specified attribute. |
||
258 | * |
||
259 | * @param string $attribute Attribute name, optionally with prefix |
||
260 | * @param string|null $uri XML Namespace URI of attribute, prefix will be automatically looked up |
||
261 | * @return bool|mixed |
||
262 | */ |
||
263 | 3 | public function getAttribute(string $attribute, string $uri = null) |
|
267 | |||
268 | /** |
||
269 | * Checks if attribute exists |
||
270 | * |
||
271 | * @param string $attribute Attribute name, optionally with prefix |
||
272 | * @param string|null $uri XML Namespace URI of attribute, prefix will be automatically looked up |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | 3 | public function hasAttribute(string $attribute, string $uri = null) |
|
280 | |||
281 | /** |
||
282 | * Returns all element's parents in order, oldest ancestor is the last element in returned array. |
||
283 | * @return XmlElement[] |
||
284 | */ |
||
285 | public function getParents() |
||
289 | |||
290 | /** |
||
291 | * Returns element's parent |
||
292 | * @return XmlElement|null |
||
293 | */ |
||
294 | 2 | public function getParent() |
|
298 | |||
299 | /** |
||
300 | * Sets element's parent |
||
301 | * @param XmlElement $parent |
||
302 | */ |
||
303 | 11 | protected function setParent(XmlElement $parent) |
|
315 | |||
316 | /** |
||
317 | * Appends child to element |
||
318 | * |
||
319 | * @param XmlElement|string $element |
||
320 | * |
||
321 | * @return XmlElement|string Same as $element |
||
322 | */ |
||
323 | 15 | public function append($element) |
|
349 | |||
350 | /** |
||
351 | * Returns namespace URI associated with element |
||
352 | * |
||
353 | * @return false|string |
||
354 | */ |
||
355 | 18 | public function getNamespace() |
|
359 | |||
360 | /** |
||
361 | * Adds namespace to element, and associates it with prefix. |
||
362 | * |
||
363 | * @param string $uri Namespace URI |
||
364 | * @param string|bool|null $prefix Prefix which will be used for namespace, false for using element's prefix |
||
365 | * and null for no prefix |
||
366 | */ |
||
367 | 16 | public function setNamespace(string $uri, $prefix = false) |
|
375 | |||
376 | 7 | public function getName() |
|
380 | |||
381 | 7 | public function getChildren() |
|
385 | |||
386 | 18 | public function getPrefix() |
|
390 | |||
391 | 12 | public function getLocalName() |
|
395 | |||
396 | 7 | public function getAttributes() |
|
400 | |||
401 | 1 | protected function setAttributes(array $attributes) |
|
409 | |||
410 | /** |
||
411 | * Returns one element at specified index (for default the first one). |
||
412 | * |
||
413 | * @param string $name Requested element tag name |
||
414 | * @param string $uri Requested element namespace |
||
415 | * @param int $index Index of element to retrieve |
||
416 | * |
||
417 | * @return XmlElement|false Retrieved element |
||
418 | */ |
||
419 | 1 | public function element(string $name, string $uri = null, int $index = 0) |
|
423 | |||
424 | /** |
||
425 | * Retrieves array of matching elements |
||
426 | * |
||
427 | * @param string $name Requested element tag name |
||
428 | * @param string|null $uri Requested element namespace |
||
429 | * |
||
430 | * @return XmlElement[] Found Elements |
||
431 | */ |
||
432 | 2 | public function elements($name, $uri = null) : array |
|
441 | |||
442 | /** |
||
443 | * Filters element with given predicate |
||
444 | * |
||
445 | * @param callable|string $predicate Predicate or class name |
||
446 | * |
||
447 | * @return XmlElement[] |
||
448 | */ |
||
449 | 2 | public function all($predicate) |
|
453 | |||
454 | /** |
||
455 | * Iterates over matching elements |
||
456 | * |
||
457 | * @param callable|string $predicate Predicate or class name |
||
458 | * |
||
459 | * @return XmlElement|false |
||
460 | */ |
||
461 | 2 | public function get($predicate) |
|
472 | |||
473 | 1 | public function has($predicate) |
|
477 | |||
478 | /** |
||
479 | * @param string|null $query |
||
480 | * @return XPathQuery |
||
481 | */ |
||
482 | 1 | public function query(string $query = null) |
|
486 | |||
487 | /** |
||
488 | * Helper for retrieving all arguments (including namespaces) |
||
489 | * |
||
490 | * @return array |
||
491 | */ |
||
492 | 3 | private function attributes(): array |
|
504 | |||
505 | /** |
||
506 | * Prefixes $name with attribute associated with $uri |
||
507 | * |
||
508 | * @param string $name Name to prefix |
||
509 | * @param string $uri Namespace URI |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | 5 | protected function _prefix(string $name, string $uri = null): string |
|
525 | |||
526 | 2 | public function __toString() |
|
530 | |||
531 | /** |
||
532 | * Splits name into local-name and prefix |
||
533 | * |
||
534 | * @param $name |
||
535 | * @return array [$name, $prefix] |
||
536 | */ |
||
537 | 23 | public static function resolve($name) |
|
547 | } |
||
548 |