Complex classes like FluidXml 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 FluidXml, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class FluidXml implements FluidInterface |
||
| 9 | { |
||
| 10 | use FluidAliasesTrait, |
||
| 11 | FluidSaveTrait, |
||
| 12 | NewableTrait, |
||
| 13 | ReservedCallTrait, // For compatibility with PHP 5.6. |
||
| 14 | ReservedCallStaticTrait; // For compatibility with PHP 5.6. |
||
| 15 | |||
| 16 | const ROOT_NODE = 'doc'; |
||
| 17 | |||
| 18 | private $defaults = [ 'root' => self::ROOT_NODE, |
||
| 19 | 'version' => '1.0', |
||
| 20 | 'encoding' => 'UTF-8', |
||
| 21 | 'stylesheet' => null ]; |
||
| 22 | |||
| 23 | private $document; |
||
| 24 | private $handler; |
||
| 25 | private $context; |
||
| 26 | private $contextEl; |
||
| 27 | |||
| 28 | 1 | public static function load($document) |
|
| 40 | |||
| 41 | 1 | public function __construct(...$arguments) |
|
| 67 | |||
| 68 | 1 | protected function mergeOptions(&$arguments) |
|
| 69 | { |
||
| 70 | 1 | $options = $this->defaults; |
|
| 71 | |||
| 72 | 1 | if (\count($arguments) > 0) { |
|
| 73 | // The root option can be specified as first argument |
||
| 74 | // because it is the most common. |
||
| 75 | 1 | $options['root'] = $arguments[0]; |
|
| 76 | } |
||
| 77 | |||
| 78 | 1 | if (\count($arguments) > 1) { |
|
| 79 | // Custom options can be specified only as second argument, |
||
| 80 | // to avoid confusion with array to XML construction style. |
||
| 81 | 1 | $options = \array_merge($options, $arguments[1]); |
|
| 82 | } |
||
| 83 | |||
| 84 | 1 | return $options; |
|
| 85 | } |
||
| 86 | |||
| 87 | 1 | private function newDom(&$options) |
|
| 88 | { |
||
| 89 | 1 | $dom = new \DOMDocument($options['version'], $options['encoding']); |
|
| 90 | 1 | $dom->formatOutput = true; |
|
| 91 | 1 | $dom->preserveWhiteSpace = false; |
|
| 92 | |||
| 93 | 1 | return $dom; |
|
| 94 | } |
||
| 95 | |||
| 96 | 1 | private function initStylesheet(&$options) |
|
| 115 | |||
| 116 | 1 | private function initRoot(&$options) |
|
| 124 | |||
| 125 | 1 | public function length() |
|
| 129 | |||
| 130 | 1 | public function dom() |
|
| 131 | { |
||
| 132 | 1 | return $this->document->dom; |
|
| 133 | } |
||
| 134 | |||
| 135 | // This method should be called 'array', |
||
| 136 | // but for compatibility with PHP 5.6 |
||
| 137 | // it is shadowed by the __call() method. |
||
| 138 | 1 | public function array_() |
|
| 139 | { |
||
| 140 | 1 | $el = $this->document->dom->documentElement; |
|
| 141 | |||
| 142 | 1 | if ($el === null) { |
|
| 143 | $el = $this->document->dom; |
||
| 144 | } |
||
| 145 | |||
| 146 | 1 | return [ $el ]; |
|
| 147 | } |
||
| 148 | |||
| 149 | 1 | public function __toString() |
|
| 153 | |||
| 154 | 1 | public function xml($strip = false) |
|
| 162 | |||
| 163 | 1 | public function html($strip = false) |
|
| 175 | |||
| 176 | 1 | public function namespaces() |
|
| 180 | |||
| 181 | // This method should be called 'namespace', |
||
| 182 | // but for compatibility with PHP 5.6 |
||
| 183 | // it is shadowed by the __call() method. |
||
| 184 | 1 | protected function namespace_(...$arguments) |
|
| 209 | |||
| 210 | public function query(...$query) { return $this->context()->query(...$query); } |
||
| 211 | public function times($times, callable $fn = null) { return $this->context()->times($times, $fn); } |
||
| 212 | public function each(callable $fn) { return $this->context()->each($fn); } |
||
| 213 | public function filter(callable $fn) { return $this->context()->filter($fn); } |
||
| 214 | public function setAttribute($name, $value = null) { $this->context()->setAttribute($name, $value); return $this; } |
||
| 215 | public function setText($text) { $this->context()->setText($text); return $this; } |
||
| 216 | public function addText($text) { $this->context()->addText($text); return $this; } |
||
| 217 | public function setCdata($text) { $this->context()->setCdata($text); return $this; } |
||
| 218 | public function addCdata($text) { $this->context()->addCdata($text); return $this; } |
||
| 219 | public function setComment($text) { $this->context()->setComment($text); return $this; } |
||
| 220 | public function addComment($text) { $this->context()->addComment($text); return $this; } |
||
| 221 | public function remove(...$query) { $this->context()->remove(...$query); return $this; } |
||
| 222 | |||
| 223 | 1 | public function addChild($child, ...$optionals) |
|
| 224 | { |
||
| 225 | return $this->chooseContext(function($cx) use ($child, &$optionals) { |
||
| 229 | |||
| 230 | 1 | public function prependSibling($sibling, ...$optionals) |
|
| 236 | |||
| 237 | public function appendSibling($sibling, ...$optionals) |
||
| 243 | |||
| 244 | 1 | protected function context() |
|
| 263 | |||
| 264 | 1 | protected function chooseContext(\Closure $fn) |
|
| 280 | } |
||
| 281 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.