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 |
||
18 | abstract class AbstractNode |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Contains the tag name/type |
||
23 | * |
||
24 | * @var \PHPHtmlParser\Dom\Tag |
||
25 | */ |
||
26 | protected $tag; |
||
27 | |||
28 | /** |
||
29 | * Contains a list of attributes on this tag. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $attr = []; |
||
34 | |||
35 | /** |
||
36 | * Contains the parent Node. |
||
37 | * |
||
38 | * @var InnerNode |
||
39 | */ |
||
40 | protected $parent = null; |
||
41 | |||
42 | /** |
||
43 | * The unique id of the class. Given by PHP. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $id; |
||
48 | |||
49 | /** |
||
50 | * The encoding class used to encode strings. |
||
51 | * |
||
52 | * @var mixed |
||
53 | */ |
||
54 | protected $encode; |
||
55 | |||
56 | /** |
||
57 | * Creates a unique spl hash for this node. |
||
58 | */ |
||
59 | public function __construct() |
||
63 | |||
64 | /** |
||
65 | * Magic get method for attributes and certain methods. |
||
66 | * |
||
67 | * @param string $key |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function __get($key) |
||
90 | |||
91 | /** |
||
92 | * Attempts to clear out any object references. |
||
93 | */ |
||
94 | public function __destruct() |
||
101 | |||
102 | /** |
||
103 | * Simply calls the outer text method. |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function __toString() |
||
111 | |||
112 | /** |
||
113 | * Returns the id of this object. |
||
114 | */ |
||
115 | public function id() |
||
119 | |||
120 | /** |
||
121 | * Returns the parent of node. |
||
122 | * |
||
123 | * @return AbstractNode |
||
124 | */ |
||
125 | public function getParent() |
||
129 | |||
130 | /** |
||
131 | * Sets the parent node. |
||
132 | * |
||
133 | * @param InnerNode $parent |
||
134 | * @return $this |
||
135 | * @throws CircularException |
||
136 | */ |
||
137 | public function setParent(InnerNode $parent) |
||
159 | |||
160 | /** |
||
161 | * Removes this node and all its children from the |
||
162 | * DOM tree. |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | public function delete() |
||
174 | |||
175 | /** |
||
176 | * Sets the encoding class to this node. |
||
177 | * |
||
178 | * @param Encode $encode |
||
179 | * @return void |
||
180 | */ |
||
181 | public function propagateEncoding(Encode $encode) |
||
186 | |||
187 | /** |
||
188 | * Checks if the given node id is an ancestor of |
||
189 | * the current node. |
||
190 | * |
||
191 | * @param int $id |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function isAncestor($id) |
||
202 | |||
203 | /** |
||
204 | * Attempts to get an ancestor node by the given id. |
||
205 | * |
||
206 | * @param int $id |
||
207 | * @return null|AbstractNode |
||
208 | */ |
||
209 | public function getAncestor($id) |
||
221 | |||
222 | /** |
||
223 | * Attempts to get the next sibling. |
||
224 | * |
||
225 | * @return AbstractNode |
||
226 | * @throws ParentNotFoundException |
||
227 | */ |
||
228 | public function nextSibling() |
||
236 | |||
237 | /** |
||
238 | * Attempts to get the previous sibling |
||
239 | * |
||
240 | * @return AbstractNode |
||
241 | * @throws ParentNotFoundException |
||
242 | */ |
||
243 | public function previousSibling() |
||
251 | |||
252 | /** |
||
253 | * Gets the tag object of this node. |
||
254 | * |
||
255 | * @return Tag |
||
256 | */ |
||
257 | public function getTag() |
||
261 | |||
262 | /** |
||
263 | * A wrapper method that simply calls the getAttribute method |
||
264 | * on the tag of this node. |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function getAttributes() |
||
277 | |||
278 | /** |
||
279 | * A wrapper method that simply calls the getAttribute method |
||
280 | * on the tag of this node. |
||
281 | * |
||
282 | * @param string $key |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function getAttribute($key) |
||
294 | |||
295 | /** |
||
296 | * A wrapper method that simply calls the setAttribute method |
||
297 | * on the tag of this node. |
||
298 | * |
||
299 | * @param string $key |
||
300 | * @param string $value |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function setAttribute($key, $value) |
||
309 | |||
310 | /** |
||
311 | * A wrapper method that simply calls the removeAttribute method |
||
312 | * on the tag of this node. |
||
313 | * |
||
314 | * @param string $key |
||
315 | * @return void |
||
316 | */ |
||
317 | public function removeAttribute($key) |
||
321 | |||
322 | /** |
||
323 | * A wrapper method that simply calls the removeAllAttributes |
||
324 | * method on the tag of this node. |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | public function removeAllAttributes() |
||
332 | |||
333 | /** |
||
334 | * Function to locate a specific ancestor tag in the path to the root. |
||
335 | * |
||
336 | * @param string $tag |
||
337 | * @return AbstractNode |
||
338 | * @throws ParentNotFoundException |
||
339 | */ |
||
340 | public function ancestorByTag($tag) |
||
355 | |||
356 | /** |
||
357 | * Find elements by css selector |
||
358 | * |
||
359 | * @param string $selector |
||
360 | * @param int $nth |
||
361 | * @return array|AbstractNode |
||
362 | */ |
||
363 | public function find($selector, $nth = null) |
||
379 | |||
380 | /** |
||
381 | * Function to try a few tricks to determine the displayed size of an img on the page. |
||
382 | * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types. |
||
383 | * |
||
384 | * Future enhancement: |
||
385 | * Look in the tag to see if there is a class or id specified that has a height or width attribute to it. |
||
386 | * |
||
387 | * Far future enhancement |
||
388 | * Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width |
||
389 | * Note that in this case, the class or id will have the img sub-selector for it to apply to the image. |
||
390 | * |
||
391 | * ridiculously far future development |
||
392 | * If the class or id is specified in a SEPARATE css file that's not on the page, go get it and do what we were just doing for the ones on the page. |
||
393 | * |
||
394 | * @author John Schlick |
||
395 | * @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out. |
||
396 | */ |
||
397 | public function get_display_size() |
||
436 | |||
437 | /** |
||
438 | * If there is a length in the style attributes use it. |
||
439 | * |
||
440 | * @param array $attributes |
||
441 | * @param int $length |
||
442 | * @param string $key |
||
443 | * @return int |
||
444 | */ |
||
445 | protected function getLength(array $attributes, $length, $key) |
||
460 | |||
461 | /** |
||
462 | * Gets the inner html of this node. |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | abstract public function innerHtml(); |
||
467 | |||
468 | /** |
||
469 | * Gets the html of this node, including it's own |
||
470 | * tag. |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | abstract public function outerHtml(); |
||
475 | |||
476 | /** |
||
477 | * Gets the text of this node (if there is any text). |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | abstract public function text(); |
||
482 | |||
483 | /** |
||
484 | * Call this when something in the node tree has changed. Like a child has been added |
||
485 | * or a parent has been changed. |
||
486 | * |
||
487 | * @return void |
||
488 | */ |
||
489 | abstract protected function clear(); |
||
490 | } |
||
491 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: