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 |
||
| 20 | abstract class AbstractNode |
||
| 21 | { |
||
| 22 | private static $count = 0; |
||
| 23 | /** |
||
| 24 | * Contains the tag name/type |
||
| 25 | * |
||
| 26 | * @var \PHPHtmlParser\Dom\Tag |
||
| 27 | */ |
||
| 28 | protected $tag; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Contains a list of attributes on this tag. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $attr = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Contains the parent Node. |
||
| 39 | * |
||
| 40 | * @var InnerNode |
||
| 41 | */ |
||
| 42 | protected $parent = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The unique id of the class. Given by PHP. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $id; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The encoding class used to encode strings. |
||
| 53 | * |
||
| 54 | * @var mixed |
||
| 55 | */ |
||
| 56 | protected $encode; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Creates a unique id for this node. |
||
| 60 | */ |
||
| 61 | 453 | public function __construct() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Magic get method for attributes and certain methods. |
||
| 69 | * |
||
| 70 | * @param string $key |
||
| 71 | * @return mixed |
||
| 72 | */ |
||
| 73 | 171 | public function __get(string $key) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Attempts to clear out any object references. |
||
| 97 | */ |
||
| 98 | 126 | public function __destruct() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Simply calls the outer text method. |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | 6 | public function __toString() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Reset node counter |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | 144 | public static function resetCount() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Returns the id of this object. |
||
| 128 | * |
||
| 129 | * @return int |
||
| 130 | */ |
||
| 131 | 387 | public function id(): int |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the parent of node. |
||
| 138 | * |
||
| 139 | * @return AbstractNode |
||
| 140 | */ |
||
| 141 | 198 | public function getParent() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Sets the parent node. |
||
| 148 | * |
||
| 149 | * @param InnerNode $parent |
||
| 150 | * @return AbstractNode |
||
| 151 | * @throws CircularException |
||
| 152 | * @chainable |
||
| 153 | */ |
||
| 154 | 381 | public function setParent(InnerNode $parent): AbstractNode |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Removes this node and all its children from the |
||
| 176 | * DOM tree. |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | 3 | public function delete() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Sets the encoding class to this node. |
||
| 191 | * |
||
| 192 | * @param Encode $encode |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | 186 | public function propagateEncoding(Encode $encode) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Checks if the given node id is an ancestor of |
||
| 203 | * the current node. |
||
| 204 | * |
||
| 205 | * @param int $id |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | 384 | public function isAncestor(int $id): Bool |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Attempts to get an ancestor node by the given id. |
||
| 219 | * |
||
| 220 | * @param int $id |
||
| 221 | * @return null|AbstractNode |
||
| 222 | */ |
||
| 223 | 387 | public function getAncestor(int $id) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Checks if the current node has a next sibling. |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 9 | public function hasNextSibling(): bool |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Attempts to get the next sibling. |
||
| 263 | * |
||
| 264 | * @return AbstractNode |
||
| 265 | * @throws ParentNotFoundException |
||
| 266 | */ |
||
| 267 | 39 | public function nextSibling(): AbstractNode |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Attempts to get the previous sibling |
||
| 278 | * |
||
| 279 | * @return AbstractNode |
||
| 280 | * @throws ParentNotFoundException |
||
| 281 | */ |
||
| 282 | 9 | public function previousSibling(): AbstractNode |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Gets the tag object of this node. |
||
| 293 | * |
||
| 294 | * @return Tag |
||
| 295 | */ |
||
| 296 | 258 | public function getTag(): Tag |
|
| 300 | |||
| 301 | /** |
||
| 302 | * A wrapper method that simply calls the getAttribute method |
||
| 303 | * on the tag of this node. |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | 6 | public function getAttributes(): array |
|
| 316 | |||
| 317 | /** |
||
| 318 | * A wrapper method that simply calls the getAttribute method |
||
| 319 | * on the tag of this node. |
||
| 320 | * |
||
| 321 | * @param string $key |
||
| 322 | * @return mixed |
||
| 323 | */ |
||
| 324 | 195 | public function getAttribute(string $key) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * A wrapper method that simply calls the hasAttribute method |
||
| 336 | * on the tag of this node. |
||
| 337 | * |
||
| 338 | * @param string $key |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | 84 | public function hasAttribute(string $key): bool |
|
| 345 | |||
| 346 | /** |
||
| 347 | * A wrapper method that simply calls the setAttribute method |
||
| 348 | * on the tag of this node. |
||
| 349 | * |
||
| 350 | * @param string $key |
||
| 351 | * @param string|null $value |
||
| 352 | * @return AbstractNode |
||
| 353 | * @chainable |
||
| 354 | */ |
||
| 355 | 21 | public function setAttribute(string $key, $value): AbstractNode |
|
| 364 | |||
| 365 | /** |
||
| 366 | * A wrapper method that simply calls the removeAttribute method |
||
| 367 | * on the tag of this node. |
||
| 368 | * |
||
| 369 | * @param string $key |
||
| 370 | * @return void |
||
| 371 | */ |
||
| 372 | 3 | public function removeAttribute(string $key): void |
|
| 379 | |||
| 380 | /** |
||
| 381 | * A wrapper method that simply calls the removeAllAttributes |
||
| 382 | * method on the tag of this node. |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | 3 | public function removeAllAttributes(): void |
|
| 393 | /** |
||
| 394 | * Function to locate a specific ancestor tag in the path to the root. |
||
| 395 | * |
||
| 396 | * @param string $tag |
||
| 397 | * @return AbstractNode |
||
| 398 | * @throws ParentNotFoundException |
||
| 399 | */ |
||
| 400 | 6 | public function ancestorByTag(string $tag): AbstractNode |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Find elements by css selector |
||
| 418 | * |
||
| 419 | * @param string $selector |
||
| 420 | * @param int $nth |
||
| 421 | * @return array|AbstractNode |
||
| 422 | */ |
||
| 423 | 186 | public function find(string $selector, int $nth = null) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Find node by id |
||
| 442 | * |
||
| 443 | * @param int $id |
||
| 444 | * @return bool|AbstractNode |
||
| 445 | */ |
||
| 446 | 9 | public function findById(int $id) |
|
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * Gets the inner html of this node. |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | abstract public function innerHtml(): string; |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Gets the html of this node, including it's own |
||
| 463 | * tag. |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | abstract public function outerHtml(): string; |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Gets the text of this node (if there is any text). |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | abstract public function text(): string; |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Call this when something in the node tree has changed. Like a child has been added |
||
| 478 | * or a parent has been changed. |
||
| 479 | * |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | abstract protected function clear(): void; |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Check is node type textNode |
||
| 486 | * |
||
| 487 | * @return boolean |
||
| 488 | */ |
||
| 489 | 9 | public function isTextNode(): bool |
|
| 494 | } |
||
| 495 |
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.