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) |
||
91 | |||
92 | /** |
||
93 | * Attempts to clear out any object references. |
||
94 | */ |
||
95 | public function __destruct() |
||
102 | |||
103 | /** |
||
104 | * Simply calls the outer text method. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function __toString() |
||
112 | |||
113 | /** |
||
114 | * Returns the id of this object. |
||
115 | */ |
||
116 | public function id() |
||
120 | |||
121 | /** |
||
122 | * Returns the parent of node. |
||
123 | * |
||
124 | * @return AbstractNode |
||
125 | */ |
||
126 | public function getParent() |
||
130 | |||
131 | /** |
||
132 | * Sets the parent node. |
||
133 | * |
||
134 | * @param InnerNode $parent |
||
135 | * @return $this |
||
136 | * @throws CircularException |
||
137 | */ |
||
138 | public function setParent(InnerNode $parent) |
||
160 | |||
161 | /** |
||
162 | * Removes this node and all its children from the |
||
163 | * DOM tree. |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public function delete() |
||
175 | |||
176 | /** |
||
177 | * Sets the encoding class to this node. |
||
178 | * |
||
179 | * @param Encode $encode |
||
180 | * @return void |
||
181 | */ |
||
182 | public function propagateEncoding(Encode $encode) |
||
187 | |||
188 | /** |
||
189 | * Checks if the given node id is an ancestor of |
||
190 | * the current node. |
||
191 | * |
||
192 | * @param int $id |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isAncestor($id) |
||
203 | |||
204 | /** |
||
205 | * Attempts to get an ancestor node by the given id. |
||
206 | * |
||
207 | * @param int $id |
||
208 | * @return null|AbstractNode |
||
209 | */ |
||
210 | public function getAncestor($id) |
||
222 | |||
223 | /** |
||
224 | * Attempts to get the next sibling. |
||
225 | * |
||
226 | * @return AbstractNode |
||
227 | * @throws ParentNotFoundException |
||
228 | */ |
||
229 | public function nextSibling() |
||
237 | |||
238 | /** |
||
239 | * Attempts to get the previous sibling |
||
240 | * |
||
241 | * @return AbstractNode |
||
242 | * @throws ParentNotFoundException |
||
243 | */ |
||
244 | public function previousSibling() |
||
252 | |||
253 | /** |
||
254 | * Gets the tag object of this node. |
||
255 | * |
||
256 | * @return Tag |
||
257 | */ |
||
258 | public function getTag() |
||
262 | |||
263 | /** |
||
264 | * A wrapper method that simply calls the getAttribute method |
||
265 | * on the tag of this node. |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public function getAttributes() |
||
278 | |||
279 | /** |
||
280 | * A wrapper method that simply calls the getAttribute method |
||
281 | * on the tag of this node. |
||
282 | * |
||
283 | * @param string $key |
||
284 | * @return mixed |
||
285 | */ |
||
286 | public function getAttribute($key) |
||
295 | |||
296 | /** |
||
297 | * A wrapper method that simply calls the hasAttribute method |
||
298 | * on the tag of this node. |
||
299 | * |
||
300 | * @param string $key |
||
301 | * @return bool |
||
302 | */ |
||
303 | public function hasAttribute($key) |
||
307 | |||
308 | /** |
||
309 | * A wrapper method that simply calls the setAttribute method |
||
310 | * on the tag of this node. |
||
311 | * |
||
312 | * @param string $key |
||
313 | * @param string $value |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function setAttribute($key, $value) |
||
322 | |||
323 | /** |
||
324 | * A wrapper method that simply calls the removeAttribute method |
||
325 | * on the tag of this node. |
||
326 | * |
||
327 | * @param string $key |
||
328 | * @return void |
||
329 | */ |
||
330 | public function removeAttribute($key) |
||
334 | |||
335 | /** |
||
336 | * A wrapper method that simply calls the removeAllAttributes |
||
337 | * method on the tag of this node. |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | public function removeAllAttributes() |
||
345 | |||
346 | /** |
||
347 | * Function to locate a specific ancestor tag in the path to the root. |
||
348 | * |
||
349 | * @param string $tag |
||
350 | * @return AbstractNode |
||
351 | * @throws ParentNotFoundException |
||
352 | */ |
||
353 | public function ancestorByTag($tag) |
||
368 | |||
369 | /** |
||
370 | * Find elements by css selector |
||
371 | * |
||
372 | * @param string $selector |
||
373 | * @param int $nth |
||
374 | * @return array|AbstractNode |
||
375 | */ |
||
376 | public function find($selector, $nth = null) |
||
392 | |||
393 | /** |
||
394 | * Gets the inner html of this node. |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | abstract public function innerHtml(); |
||
399 | |||
400 | /** |
||
401 | * Gets the html of this node, including it's own |
||
402 | * tag. |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | abstract public function outerHtml(); |
||
407 | |||
408 | /** |
||
409 | * Gets the text of this node (if there is any text). |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | abstract public function text(); |
||
414 | |||
415 | /** |
||
416 | * Call this when something in the node tree has changed. Like a child has been added |
||
417 | * or a parent has been changed. |
||
418 | * |
||
419 | * @return void |
||
420 | */ |
||
421 | abstract protected function clear(); |
||
422 | } |
||
423 |
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: