Complex classes like AbstractNode 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 AbstractNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class AbstractNode |
||
20 | { |
||
21 | private static $count = 0; |
||
22 | /** |
||
23 | * Contains the tag name/type |
||
24 | * |
||
25 | * @var \PHPHtmlParser\Dom\Tag |
||
26 | */ |
||
27 | protected $tag; |
||
28 | |||
29 | /** |
||
30 | * Contains a list of attributes on this tag. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $attr = []; |
||
35 | |||
36 | /** |
||
37 | * Contains the parent Node. |
||
38 | * |
||
39 | * @var InnerNode |
||
40 | */ |
||
41 | protected $parent = null; |
||
42 | |||
43 | /** |
||
44 | * The unique id of the class. Given by PHP. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $id; |
||
49 | |||
50 | /** |
||
51 | * The encoding class used to encode strings. |
||
52 | * |
||
53 | * @var mixed |
||
54 | */ |
||
55 | protected $encode; |
||
56 | |||
57 | /** |
||
58 | * Creates a unique id for this node. |
||
59 | */ |
||
60 | public function __construct() |
||
65 | |||
66 | /** |
||
67 | * Magic get method for attributes and certain methods. |
||
68 | * |
||
69 | * @param string $key |
||
70 | * @return mixed |
||
71 | */ |
||
72 | public function __get($key) |
||
93 | |||
94 | /** |
||
95 | * Attempts to clear out any object references. |
||
96 | */ |
||
97 | public function __destruct() |
||
104 | |||
105 | /** |
||
106 | * Simply calls the outer text method. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function __toString() |
||
114 | |||
115 | public function resetCount() |
||
119 | |||
120 | /** |
||
121 | * Returns the id of this object. |
||
122 | */ |
||
123 | public function id() |
||
127 | |||
128 | /** |
||
129 | * Returns the parent of node. |
||
130 | * |
||
131 | * @return AbstractNode |
||
132 | */ |
||
133 | public function getParent() |
||
137 | |||
138 | /** |
||
139 | * Sets the parent node. |
||
140 | * |
||
141 | * @param InnerNode $parent |
||
142 | * @return $this |
||
143 | * @throws CircularException |
||
144 | */ |
||
145 | public function setParent(InnerNode $parent) |
||
167 | |||
168 | /** |
||
169 | * Removes this node and all its children from the |
||
170 | * DOM tree. |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | public function delete() |
||
182 | |||
183 | /** |
||
184 | * Sets the encoding class to this node. |
||
185 | * |
||
186 | * @param Encode $encode |
||
187 | * @return void |
||
188 | */ |
||
189 | public function propagateEncoding(Encode $encode) |
||
194 | |||
195 | /** |
||
196 | * Checks if the given node id is an ancestor of |
||
197 | * the current node. |
||
198 | * |
||
199 | * @param int $id |
||
200 | * @return bool |
||
201 | */ |
||
202 | public function isAncestor($id) |
||
210 | |||
211 | /** |
||
212 | * Attempts to get an ancestor node by the given id. |
||
213 | * |
||
214 | * @param int $id |
||
215 | * @return null|AbstractNode |
||
216 | */ |
||
217 | public function getAncestor($id) |
||
229 | |||
230 | public function hasNextSibling() |
||
238 | |||
239 | /** |
||
240 | * Attempts to get the next sibling. |
||
241 | * |
||
242 | * @return AbstractNode |
||
243 | * @throws ParentNotFoundException |
||
244 | */ |
||
245 | public function nextSibling() |
||
253 | |||
254 | /** |
||
255 | * Attempts to get the previous sibling |
||
256 | * |
||
257 | * @return AbstractNode |
||
258 | * @throws ParentNotFoundException |
||
259 | */ |
||
260 | public function previousSibling() |
||
268 | |||
269 | /** |
||
270 | * Gets the tag object of this node. |
||
271 | * |
||
272 | * @return Tag |
||
273 | */ |
||
274 | public function getTag() |
||
278 | |||
279 | /** |
||
280 | * A wrapper method that simply calls the getAttribute method |
||
281 | * on the tag of this node. |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getAttributes() |
||
294 | |||
295 | /** |
||
296 | * A wrapper method that simply calls the getAttribute method |
||
297 | * on the tag of this node. |
||
298 | * |
||
299 | * @param string $key |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function getAttribute($key) |
||
311 | |||
312 | /** |
||
313 | * A wrapper method that simply calls the setAttribute method |
||
314 | * on the tag of this node. |
||
315 | * |
||
316 | * @param string $key |
||
317 | * @param string $value |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function setAttribute($key, $value) |
||
326 | |||
327 | /** |
||
328 | * A wrapper method that simply calls the removeAttribute method |
||
329 | * on the tag of this node. |
||
330 | * |
||
331 | * @param string $key |
||
332 | * @return void |
||
333 | */ |
||
334 | public function removeAttribute($key) |
||
338 | |||
339 | /** |
||
340 | * A wrapper method that simply calls the removeAllAttributes |
||
341 | * method on the tag of this node. |
||
342 | * |
||
343 | * @return void |
||
344 | */ |
||
345 | public function removeAllAttributes() |
||
349 | |||
350 | /** |
||
351 | * Function to locate a specific ancestor tag in the path to the root. |
||
352 | * |
||
353 | * @param string $tag |
||
354 | * @return AbstractNode |
||
355 | * @throws ParentNotFoundException |
||
356 | */ |
||
357 | public function ancestorByTag($tag) |
||
372 | |||
373 | /** |
||
374 | * Find elements by css selector |
||
375 | * |
||
376 | * @param string $selector |
||
377 | * @param int $nth |
||
378 | * @return array|AbstractNode |
||
379 | */ |
||
380 | public function find($selector, $nth = null) |
||
396 | |||
397 | /** |
||
398 | * Find node by id |
||
399 | * |
||
400 | * @param $id |
||
401 | * @return bool|AbstractNode |
||
402 | */ |
||
403 | public function findById($id) |
||
409 | |||
410 | |||
411 | /** |
||
412 | * Gets the inner html of this node. |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | abstract public function innerHtml(); |
||
417 | |||
418 | /** |
||
419 | * Gets the html of this node, including it's own |
||
420 | * tag. |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | abstract public function outerHtml(); |
||
425 | |||
426 | /** |
||
427 | * Gets the text of this node (if there is any text). |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | abstract public function text(); |
||
432 | |||
433 | /** |
||
434 | * Call this when something in the node tree has changed. Like a child has been added |
||
435 | * or a parent has been changed. |
||
436 | * |
||
437 | * @return void |
||
438 | */ |
||
439 | abstract protected function clear(); |
||
440 | |||
441 | /** |
||
442 | * Check is node type textNode |
||
443 | * |
||
444 | * @return boolean |
||
445 | */ |
||
446 | public function isTextNode() { |
||
450 | } |
||
451 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.