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) |
|
| 336 | |||
| 337 | 14 | protected function appendChild($element) { |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Returns namespace URI associated with element |
||
| 356 | * |
||
| 357 | * @return false|string |
||
| 358 | */ |
||
| 359 | 18 | public function getNamespace() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Adds namespace to element, and associates it with prefix. |
||
| 366 | * |
||
| 367 | * @param string $uri Namespace URI |
||
| 368 | * @param string|bool|null $prefix Prefix which will be used for namespace, false for using element's prefix |
||
| 369 | * and null for no prefix |
||
| 370 | */ |
||
| 371 | 16 | public function setNamespace(string $uri, $prefix = false) |
|
| 379 | |||
| 380 | 7 | public function getName() |
|
| 384 | |||
| 385 | 7 | public function getChildren() |
|
| 389 | |||
| 390 | 18 | public function getPrefix() |
|
| 394 | |||
| 395 | 12 | public function getLocalName() |
|
| 399 | |||
| 400 | 7 | public function getAttributes() |
|
| 404 | |||
| 405 | 1 | protected function setAttributes(array $attributes) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Returns one element at specified index (for default the first one). |
||
| 416 | * |
||
| 417 | * @param string $name Requested element tag name |
||
| 418 | * @param string $uri Requested element namespace |
||
| 419 | * @param int $index Index of element to retrieve |
||
| 420 | * |
||
| 421 | * @return XmlElement|false Retrieved element |
||
| 422 | */ |
||
| 423 | 1 | public function element(string $name, string $uri = null, int $index = 0) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Retrieves array of matching elements |
||
| 430 | * |
||
| 431 | * @param string $name Requested element tag name |
||
| 432 | * @param string|null $uri Requested element namespace |
||
| 433 | * |
||
| 434 | * @return XmlElement[] Found Elements |
||
| 435 | */ |
||
| 436 | 2 | public function elements($name, $uri = null) : array |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Filters element with given predicate |
||
| 448 | * |
||
| 449 | * @param callable|string $predicate Predicate or class name |
||
| 450 | * |
||
| 451 | * @return XmlElement[] |
||
| 452 | */ |
||
| 453 | 2 | public function all($predicate) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Iterates over matching elements |
||
| 460 | * |
||
| 461 | * @param callable|string $predicate Predicate or class name |
||
| 462 | * |
||
| 463 | * @return XmlElement|false |
||
| 464 | */ |
||
| 465 | 2 | public function get($predicate) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Checks if any element matching predicate exists |
||
| 479 | * |
||
| 480 | * @param callable|string $predicate Predicate or class name |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | 1 | public function has($predicate) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * @param string|null $query |
||
| 491 | * @return XPathQuery |
||
| 492 | */ |
||
| 493 | 1 | public function query(string $query = null) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Helper for retrieving all arguments (including namespaces) |
||
| 500 | * |
||
| 501 | * @return array |
||
| 502 | */ |
||
| 503 | 3 | private function attributes(): array |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Prefixes $name with attribute associated with $uri |
||
| 518 | * |
||
| 519 | * @param string $name Name to prefix |
||
| 520 | * @param string $uri Namespace URI |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | 5 | protected function _prefix(string $name, string $uri = null): string |
|
| 536 | |||
| 537 | 2 | public function __toString() |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Splits name into local-name and prefix |
||
| 544 | * |
||
| 545 | * @param $name |
||
| 546 | * @return array [$name, $prefix] |
||
| 547 | */ |
||
| 548 | 23 | public static function resolve($name) |
|
| 558 | } |
||
| 559 |