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 | /** |
||
| 116 | * Reset node counter |
||
| 117 | */ |
||
| 118 | public static function resetCount() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the id of this object. |
||
| 125 | */ |
||
| 126 | public function id() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the parent of node. |
||
| 133 | * |
||
| 134 | * @return AbstractNode |
||
| 135 | */ |
||
| 136 | public function getParent() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Sets the parent node. |
||
| 143 | * |
||
| 144 | * @param InnerNode $parent |
||
| 145 | * @return $this |
||
| 146 | * @throws CircularException |
||
| 147 | */ |
||
| 148 | public function setParent(InnerNode $parent) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Removes this node and all its children from the |
||
| 173 | * DOM tree. |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function delete() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Sets the encoding class to this node. |
||
| 188 | * |
||
| 189 | * @param Encode $encode |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | public function propagateEncoding(Encode $encode) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Checks if the given node id is an ancestor of |
||
| 200 | * the current node. |
||
| 201 | * |
||
| 202 | * @param int $id |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function isAncestor($id) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Attempts to get an ancestor node by the given id. |
||
| 216 | * |
||
| 217 | * @param int $id |
||
| 218 | * @return null|AbstractNode |
||
| 219 | */ |
||
| 220 | public function getAncestor($id) |
||
| 232 | |||
| 233 | public function hasNextSibling() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Attempts to get the next sibling. |
||
| 244 | * |
||
| 245 | * @return AbstractNode |
||
| 246 | * @throws ParentNotFoundException |
||
| 247 | */ |
||
| 248 | public function nextSibling() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Attempts to get the previous sibling |
||
| 259 | * |
||
| 260 | * @return AbstractNode |
||
| 261 | * @throws ParentNotFoundException |
||
| 262 | */ |
||
| 263 | public function previousSibling() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Gets the tag object of this node. |
||
| 274 | * |
||
| 275 | * @return Tag |
||
| 276 | */ |
||
| 277 | public function getTag() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * A wrapper method that simply calls the getAttribute method |
||
| 284 | * on the tag of this node. |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function getAttributes() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * A wrapper method that simply calls the getAttribute method |
||
| 300 | * on the tag of this node. |
||
| 301 | * |
||
| 302 | * @param string $key |
||
| 303 | * @return mixed |
||
| 304 | */ |
||
| 305 | public function getAttribute($key) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * A wrapper method that simply calls the setAttribute method |
||
| 317 | * on the tag of this node. |
||
| 318 | * |
||
| 319 | * @param string $key |
||
| 320 | * @param string $value |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | public function setAttribute($key, $value) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * A wrapper method that simply calls the removeAttribute method |
||
| 332 | * on the tag of this node. |
||
| 333 | * |
||
| 334 | * @param string $key |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | public function removeAttribute($key) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * A wrapper method that simply calls the removeAllAttributes |
||
| 344 | * method on the tag of this node. |
||
| 345 | * |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | public function removeAllAttributes() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Function to locate a specific ancestor tag in the path to the root. |
||
| 355 | * |
||
| 356 | * @param string $tag |
||
| 357 | * @return AbstractNode |
||
| 358 | * @throws ParentNotFoundException |
||
| 359 | */ |
||
| 360 | public function ancestorByTag($tag) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Find elements by css selector |
||
| 378 | * |
||
| 379 | * @param string $selector |
||
| 380 | * @param int $nth |
||
| 381 | * @return array|AbstractNode |
||
| 382 | */ |
||
| 383 | public function find($selector, $nth = null) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Find node by id |
||
| 402 | * |
||
| 403 | * @param $id |
||
| 404 | * @return bool|AbstractNode |
||
| 405 | */ |
||
| 406 | public function findById($id) |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * Gets the inner html of this node. |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | abstract public function innerHtml(); |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Gets the html of this node, including it's own |
||
| 423 | * tag. |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | abstract public function outerHtml(); |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Gets the text of this node (if there is any text). |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | abstract public function text(); |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Call this when something in the node tree has changed. Like a child has been added |
||
| 438 | * or a parent has been changed. |
||
| 439 | * |
||
| 440 | * @return void |
||
| 441 | */ |
||
| 442 | abstract protected function clear(); |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Check is node type textNode |
||
| 446 | * |
||
| 447 | * @return boolean |
||
| 448 | */ |
||
| 449 | public function isTextNode() { |
||
| 453 | } |
||
| 454 |
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.