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 |
||
38 | class XmlElement |
||
39 | { |
||
40 | use Accessors; |
||
41 | |||
42 | public static $tidy = [ |
||
43 | 'indent' => true, |
||
44 | 'input-xml' => true, |
||
45 | 'output-xml' => true, |
||
46 | 'drop-empty-paras' => false, |
||
47 | 'wrap' => 0 |
||
48 | ]; |
||
49 | |||
50 | private $_localName; |
||
51 | private $_prefix = null; |
||
52 | |||
53 | private $_namespaces = [ |
||
54 | ]; |
||
55 | private $_attributes = []; |
||
56 | |||
57 | /** |
||
58 | * @var XmlElement |
||
59 | */ |
||
60 | private $_parent; |
||
61 | |||
62 | /** |
||
63 | * @var XmlElement[] |
||
64 | */ |
||
65 | private $_children = []; |
||
66 | |||
67 | /** |
||
68 | * XmlElement constructor. |
||
69 | * @param string $name |
||
70 | * @param string $uri |
||
71 | */ |
||
72 | 15 | public function __construct(string $name, string $uri = null) |
|
83 | |||
84 | 1 | public function __toString() |
|
88 | |||
89 | 2 | public function xml($clean = true): string |
|
110 | |||
111 | 2 | public function getInnerXml() |
|
123 | |||
124 | 5 | public function setAttribute(string $attribute, $value, string $uri = null) |
|
137 | |||
138 | 2 | public function getAttribute(string $attribute, string $uri = null) |
|
146 | |||
147 | 1 | public function getParent() |
|
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | 16 | public function getNamespaces($parent = true): array |
|
167 | |||
168 | 2 | private function attributes(): array |
|
180 | |||
181 | 13 | public function lookupUri(string $prefix = null) |
|
185 | |||
186 | 9 | public function lookupPrefix(string $uri = null) |
|
190 | |||
191 | /** |
||
192 | * @param string $uri |
||
193 | * @param string|false $prefix |
||
194 | */ |
||
195 | 13 | public function setNamespace(string $uri, $prefix = false) |
|
203 | |||
204 | 13 | public function getNamespace() |
|
208 | |||
209 | 5 | public function getChildren() |
|
213 | |||
214 | 10 | public function append($element) |
|
229 | |||
230 | 6 | public function getName() |
|
234 | |||
235 | 13 | public function getPrefix() |
|
239 | |||
240 | 8 | public function getLocalName() |
|
244 | |||
245 | 8 | protected function setParent(XmlElement $parent) |
|
261 | |||
262 | /** |
||
263 | * Retrieves array of matching elements |
||
264 | * |
||
265 | * @param string $name Requested element tag name |
||
266 | * @param null $uri Requested element namespace |
||
267 | * |
||
268 | * @return XmlElement[] Found Elements |
||
269 | */ |
||
270 | 2 | public function elements($name, $uri = null) : array |
|
279 | |||
280 | 6 | public function getAttributes() |
|
284 | |||
285 | /** |
||
286 | * Returns one element at specified index (for default the first one). |
||
287 | * |
||
288 | * @param string $name Requested element tag name |
||
289 | * @param string $uri Requested element namespace |
||
290 | * @param int $index Index of element to retrieve |
||
291 | * |
||
292 | * @return XmlElement|false Retrieved element |
||
293 | */ |
||
294 | 1 | public function element(string $name, string $uri = null, int $index = 0) |
|
298 | |||
299 | 2 | public function all($predicate) { |
|
303 | |||
304 | /** |
||
305 | * @param string|null $query |
||
306 | * @return XPathQuery |
||
307 | */ |
||
308 | 1 | public function query(string $query = null) |
|
312 | |||
313 | protected function init() { } |
||
314 | |||
315 | 4 | public static function plain(string $name, string $uri = null) |
|
333 | |||
334 | /** |
||
335 | * @param string $name |
||
336 | * @param string $uri |
||
337 | * @return string |
||
338 | */ |
||
339 | 3 | protected function _prefix(string $name, string $uri): string |
|
347 | |||
348 | 19 | public static function resolve($name) |
|
358 | } |
||
359 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.