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 |
||
| 16 | abstract class AbstractNode |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Contains the tag name/type |
||
| 21 | * |
||
| 22 | * @var \PHPHtmlParser\Dom\Tag |
||
| 23 | */ |
||
| 24 | protected $tag; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Contains a list of attributes on this tag. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $attr = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Contains the parent Node. |
||
| 35 | * |
||
| 36 | * @var InnerNode |
||
| 37 | */ |
||
| 38 | protected $parent = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The unique id of the class. Given by PHP. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $id; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The encoding class used to encode strings. |
||
| 49 | * |
||
| 50 | * @var mixed |
||
| 51 | */ |
||
| 52 | protected $encode; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Creates a unique spl hash for this node. |
||
| 56 | */ |
||
| 57 | public function __construct() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Magic get method for attributes and certain methods. |
||
| 64 | * |
||
| 65 | * @param string $key |
||
| 66 | * @return mixed |
||
| 67 | */ |
||
| 68 | public function __get($key) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Attempts to clear out any object references. |
||
| 88 | */ |
||
| 89 | public function __destruct() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Simply calls the outer text method. |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function __toString() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns the id of this object. |
||
| 109 | */ |
||
| 110 | public function id() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the parent of node. |
||
| 117 | * |
||
| 118 | * @return AbstractNode |
||
| 119 | */ |
||
| 120 | public function getParent() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Sets the parent node. |
||
| 127 | * |
||
| 128 | * @param InnerNode $parent |
||
| 129 | * @return $this |
||
| 130 | * @throws CircularException |
||
| 131 | */ |
||
| 132 | public function setParent(InnerNode $parent) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the encoding class to this node. |
||
| 157 | * |
||
| 158 | * @param Encode $encode |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function propagateEncoding(Encode $encode) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Checks if the given node id is an ancestor of |
||
| 169 | * the current node. |
||
| 170 | * |
||
| 171 | * @param int $id |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function isAncestor($id) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Attempts to get an ancestor node by the given id. |
||
| 185 | * |
||
| 186 | * @param int $id |
||
| 187 | * @return null|AbstractNode |
||
| 188 | */ |
||
| 189 | public function getAncestor($id) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Shortcut to return the first child. |
||
| 204 | * |
||
| 205 | * @return AbstractNode |
||
| 206 | * @uses $this->getChild() |
||
| 207 | */ |
||
| 208 | public function firstChild() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Attempts to get the last child. |
||
| 218 | * |
||
| 219 | * @return AbstractNode |
||
| 220 | */ |
||
| 221 | public function lastChild() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Attempts to get the next sibling. |
||
| 231 | * |
||
| 232 | * @return AbstractNode |
||
| 233 | * @throws ParentNotFoundException |
||
| 234 | */ |
||
| 235 | public function nextSibling() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Attempts to get the previous sibling |
||
| 246 | * |
||
| 247 | * @return AbstractNode |
||
| 248 | * @throws ParentNotFoundException |
||
| 249 | */ |
||
| 250 | public function previousSibling() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets the tag object of this node. |
||
| 261 | * |
||
| 262 | * @return Tag |
||
| 263 | */ |
||
| 264 | public function getTag() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * A wrapper method that simply calls the getAttribute method |
||
| 271 | * on the tag of this node. |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getAttributes() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * A wrapper method that simply calls the getAttribute method |
||
| 287 | * on the tag of this node. |
||
| 288 | * |
||
| 289 | * @param string $key |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | public function getAttribute($key) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * A wrapper method that simply calls the setAttribute method |
||
| 304 | * on the tag of this node. |
||
| 305 | * |
||
| 306 | * @param string $key |
||
| 307 | * @param string $value |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function setAttribute($key, $value) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Function to locate a specific ancestor tag in the path to the root. |
||
| 319 | * |
||
| 320 | * @param string $tag |
||
| 321 | * @return AbstractNode |
||
| 322 | * @throws ParentNotFoundException |
||
| 323 | */ |
||
| 324 | public function ancestorByTag($tag) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Find elements by css selector |
||
| 342 | * |
||
| 343 | * @param string $selector |
||
| 344 | * @param int $nth |
||
| 345 | * @return array|AbstractNode |
||
| 346 | */ |
||
| 347 | public function find($selector, $nth = null) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Function to try a few tricks to determine the displayed size of an img on the page. |
||
| 366 | * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types. |
||
| 367 | * |
||
| 368 | * Future enhancement: |
||
| 369 | * Look in the tag to see if there is a class or id specified that has a height or width attribute to it. |
||
| 370 | * |
||
| 371 | * Far future enhancement |
||
| 372 | * 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 |
||
| 373 | * Note that in this case, the class or id will have the img sub-selector for it to apply to the image. |
||
| 374 | * |
||
| 375 | * ridiculously far future development |
||
| 376 | * 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. |
||
| 377 | * |
||
| 378 | * @author John Schlick |
||
| 379 | * @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out. |
||
| 380 | */ |
||
| 381 | public function get_display_size() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * If there is a length in the style attributes use it. |
||
| 423 | * |
||
| 424 | * @param array $attributes |
||
| 425 | * @param int $length |
||
| 426 | * @param string $key |
||
| 427 | * @return int |
||
| 428 | */ |
||
| 429 | protected function getLength(array $attributes, $length, $key) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Gets the inner html of this node. |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | abstract public function innerHtml(); |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Gets the html of this node, including it's own |
||
| 454 | * tag. |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | abstract public function outerHtml(); |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Gets the text of this node (if there is any text). |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | abstract public function text(); |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Call this when something in the node tree has changed. Like a child has been added |
||
| 469 | * or a parent has been changed. |
||
| 470 | * |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | abstract protected function clear(); |
||
| 474 | } |
||
| 475 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: