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 |
||
35 | class XmlElement |
||
36 | { |
||
37 | use Accessors; |
||
38 | |||
39 | public static $tidy = [ |
||
40 | 'indent' => true, |
||
41 | 'input-xml' => true, |
||
42 | 'output-xml' => true, |
||
43 | 'drop-empty-paras' => false, |
||
44 | 'wrap' => 0 |
||
45 | ]; |
||
46 | |||
47 | private $_localName; |
||
48 | private $_prefix = null; |
||
49 | |||
50 | private $_namespaces = [ |
||
51 | ]; |
||
52 | private $_attributes = []; |
||
53 | |||
54 | /** |
||
55 | * @var XmlElement |
||
56 | */ |
||
57 | private $_parent; |
||
58 | |||
59 | /** |
||
60 | * @var XmlElement[] |
||
61 | */ |
||
62 | private $_children = []; |
||
63 | |||
64 | public function __toString() |
||
68 | |||
69 | public function xml($clean = true): string |
||
98 | |||
99 | public function setAttribute(string $attribute, $value, string $uri = null) |
||
112 | |||
113 | public function getAttribute(string $attribute, string $uri = null) |
||
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getNamespaces($parent = true): array |
||
137 | |||
138 | public function attributes(): array |
||
150 | |||
151 | public function lookupUri(string $prefix = null) |
||
155 | |||
156 | public function lookupPrefix(string $uri = null) |
||
160 | |||
161 | public function setNamespace(string $uri, $prefix = false) |
||
169 | |||
170 | public function getNamespace() |
||
174 | |||
175 | public function getChildren() |
||
179 | |||
180 | public function append($element) |
||
195 | |||
196 | public function getName() |
||
200 | |||
201 | public function getPrefix() |
||
205 | |||
206 | public function getLocalName() |
||
210 | |||
211 | protected function setParent(XmlElement $parent) |
||
218 | |||
219 | /** |
||
220 | * Retrieves array of matching elements |
||
221 | * |
||
222 | * @param string $name Requested element tag name |
||
223 | * @param null $uri Requested element namespace |
||
224 | * |
||
225 | * @return XmlElement[] Found Elements |
||
226 | */ |
||
227 | public function elements($name, $uri = null) : array |
||
236 | |||
237 | /** |
||
238 | * Returns one element at specified index (for default the first one). |
||
239 | * |
||
240 | * @param string $name Requested element tag name |
||
241 | * @param string $uri Requested element namespace |
||
242 | * @param int $index Index of element to retrieve |
||
243 | * |
||
244 | * @return XmlElement|false Retrieved element |
||
245 | */ |
||
246 | public function element(string $name, string $uri = null, int $index = 0) |
||
250 | |||
251 | public function all($predicate) { |
||
255 | |||
256 | /** |
||
257 | * @param string|null $query |
||
258 | * @return XPathQuery |
||
259 | */ |
||
260 | public function query(string $query = null) |
||
264 | |||
265 | protected function init() { } |
||
266 | |||
267 | public static function plain(string $name, string $uri = null) |
||
285 | |||
286 | /** |
||
287 | * @param string $name |
||
288 | * @param string $uri |
||
289 | * @return string |
||
290 | */ |
||
291 | protected function _prefix(string $name, string $uri): string |
||
299 | |||
300 | public static function resolve($name) |
||
310 | } |
||
311 |
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.