Complex classes like HtmlPageCrawler 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 HtmlPageCrawler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class HtmlPageCrawler extends Crawler |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * the (internal) root element name used when importing html fragments |
||
| 19 | * */ |
||
| 20 | const FRAGMENT_ROOT_TAGNAME = '_root'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get an HtmlPageCrawler object from a HTML string, DOMNode, DOMNodeList or HtmlPageCrawler |
||
| 24 | * |
||
| 25 | * This is the equivalent to jQuery's $() function when used for wrapping DOMNodes or creating DOMElements from HTML code. |
||
| 26 | * |
||
| 27 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|array $content |
||
| 28 | * @return HtmlPageCrawler |
||
| 29 | * @api |
||
| 30 | */ |
||
| 31 | 17 | public static function create($content) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Adds the specified class(es) to each element in the set of matched elements. |
||
| 42 | * |
||
| 43 | * @param string $name One or more space-separated classes to be added to the class attribute of each matched element. |
||
| 44 | * @return HtmlPageCrawler $this for chaining |
||
| 45 | * @api |
||
| 46 | */ |
||
| 47 | 1 | public function addClass($name) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Insert content, specified by the parameter, after each element in the set of matched elements. |
||
| 71 | * |
||
| 72 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 73 | * @return HtmlPageCrawler $this for chaining |
||
| 74 | * @api |
||
| 75 | */ |
||
| 76 | 3 | public function after($content) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Insert HTML content as child nodes of each element after existing children |
||
| 101 | * |
||
| 102 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment or DOMNode to append |
||
| 103 | * @return HtmlPageCrawler $this for chaining |
||
| 104 | * @api |
||
| 105 | */ |
||
| 106 | 2 | public function append($content) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Insert every element in the set of matched elements to the end of the target. |
||
| 133 | * |
||
| 134 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 135 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 136 | * @api |
||
| 137 | */ |
||
| 138 | 2 | public function appendTo($element) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the attribute value of the first node of the list, or sets an attribute on each element |
||
| 158 | * |
||
| 159 | * @see HtmlPageCrawler::getAttribute() |
||
| 160 | * @see HtmlPageCrawler::setAttribute |
||
| 161 | * |
||
| 162 | * @param string $name |
||
| 163 | * @param null|string $value |
||
| 164 | * @return null|string|HtmlPageCrawler |
||
| 165 | * @api |
||
| 166 | */ |
||
| 167 | 2 | public function attr($name, $value = null) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Sets an attribute on each element |
||
| 178 | * |
||
| 179 | * @param string $name |
||
| 180 | * @param string $value |
||
| 181 | * @return HtmlPageCrawler $this for chaining |
||
| 182 | */ |
||
| 183 | 3 | public function setAttribute($name, $value) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the attribute value of the first node of the list. |
||
| 196 | * |
||
| 197 | * @param string $name The attribute name |
||
| 198 | * @return string|null The attribute value or null if the attribute does not exist |
||
| 199 | * @throws \InvalidArgumentException When current node is empty |
||
| 200 | * |
||
| 201 | */ |
||
| 202 | 2 | public function getAttribute($name) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Insert content, specified by the parameter, before each element in the set of matched elements. |
||
| 213 | * |
||
| 214 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 215 | * @return HtmlPageCrawler $this for chaining |
||
| 216 | * @api |
||
| 217 | */ |
||
| 218 | 2 | public function before($content) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Create a deep copy of the set of matched elements. |
||
| 240 | * |
||
| 241 | * Equivalent to clone() in jQuery (clone is not a valid PHP function name) |
||
| 242 | * |
||
| 243 | * @return HtmlPageCrawler |
||
| 244 | * @api |
||
| 245 | */ |
||
| 246 | 1 | public function makeClone() |
|
| 250 | |||
| 251 | 1 | public function __clone() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get one CSS style property of the first element or set it for all elements in the list |
||
| 264 | * |
||
| 265 | * Function is here for compatibility with jQuery; it is the same as getStyle() and setStyle() |
||
| 266 | * |
||
| 267 | * @see HtmlPageCrawler::getStyle() |
||
| 268 | * @see HtmlPageCrawler::setStyle() |
||
| 269 | * |
||
| 270 | * @param string $key The name of the style property |
||
| 271 | * @param null|string $value The CSS value to set, or NULL to get the current value |
||
| 272 | * @return HtmlPageCrawler|string If no param is provided, returns the CSS styles of the first element |
||
| 273 | * @api |
||
| 274 | */ |
||
| 275 | 1 | public function css($key, $value = null) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * get one CSS style property of the first element |
||
| 286 | * |
||
| 287 | * @param string $key name of the property |
||
| 288 | * @return string|null value of the property |
||
| 289 | */ |
||
| 290 | 1 | public function getStyle($key) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * set one CSS style property for all elements in the list |
||
| 298 | * |
||
| 299 | * @param string $key name of the property |
||
| 300 | * @param string $value value of the property |
||
| 301 | * @return HtmlPageCrawler $this for chaining |
||
| 302 | */ |
||
| 303 | 1 | public function setStyle($key, $value) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Removes all child nodes and text from all nodes in set |
||
| 322 | * |
||
| 323 | * Equivalent to jQuery's empty() function which is not a valid function name in PHP |
||
| 324 | * @return HtmlPageCrawler $this |
||
| 325 | * @api |
||
| 326 | */ |
||
| 327 | 1 | public function makeEmpty() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Determine whether any of the matched elements are assigned the given class. |
||
| 337 | * |
||
| 338 | * @param string $name |
||
| 339 | * @return bool |
||
| 340 | * @api |
||
| 341 | */ |
||
| 342 | 2 | public function hasClass($name) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Set the HTML contents of each element |
||
| 357 | * |
||
| 358 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 359 | * @return HtmlPageCrawler $this for chaining |
||
| 360 | */ |
||
| 361 | 3 | public function setInnerHtml($content) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Insert every element in the set of matched elements after the target. |
||
| 378 | * |
||
| 379 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 380 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 381 | * @api |
||
| 382 | */ |
||
| 383 | 2 | public function insertAfter($element) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Insert every element in the set of matched elements before the target. |
||
| 406 | * |
||
| 407 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 408 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 409 | * @api |
||
| 410 | */ |
||
| 411 | 2 | public function insertBefore($element) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
| 431 | * |
||
| 432 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 433 | * @return HtmlPageCrawler $this for chaining |
||
| 434 | * @api |
||
| 435 | */ |
||
| 436 | 2 | public function prepend($content) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Insert every element in the set of matched elements to the beginning of the target. |
||
| 461 | * |
||
| 462 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 463 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
| 464 | * @api |
||
| 465 | */ |
||
| 466 | 1 | public function prependTo($element) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Remove the set of matched elements from the DOM. |
||
| 491 | * |
||
| 492 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
| 493 | * but leaves them in the DOM) |
||
| 494 | * |
||
| 495 | * @api |
||
| 496 | */ |
||
| 497 | 2 | public function remove() |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Remove an attribute from each element in the set of matched elements. |
||
| 512 | * |
||
| 513 | * Alias for removeAttribute for compatibility with jQuery |
||
| 514 | * |
||
| 515 | * @param string $name |
||
| 516 | * @return HtmlPageCrawler |
||
| 517 | * @api |
||
| 518 | */ |
||
| 519 | 1 | public function removeAttr($name) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Remove an attribute from each element in the set of matched elements. |
||
| 526 | * |
||
| 527 | * @param string $name |
||
| 528 | * @return HtmlPageCrawler |
||
| 529 | */ |
||
| 530 | 1 | public function removeAttribute($name) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Remove a class from each element in the list |
||
| 545 | * |
||
| 546 | * @param string $name |
||
| 547 | * @return HtmlPageCrawler $this for chaining |
||
| 548 | * @api |
||
| 549 | */ |
||
| 550 | 2 | public function removeClass($name) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Replace each target element with the set of matched elements. |
||
| 570 | * |
||
| 571 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 572 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 573 | * @api |
||
| 574 | */ |
||
| 575 | 2 | public function replaceAll($element) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
| 599 | * |
||
| 600 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 601 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 602 | * @api |
||
| 603 | */ |
||
| 604 | 2 | public function replaceWith($content) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Get the combined text contents of each element in the set of matched elements, including their descendants. |
||
| 630 | * This is what the jQuery text() function does, contrary to the Crawler::text() method that returns only |
||
| 631 | * the text of the first node. |
||
| 632 | * |
||
| 633 | * @return string |
||
| 634 | * @api |
||
| 635 | */ |
||
| 636 | 1 | public function getCombinedText() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Set the text contents of the matched elements. |
||
| 648 | * |
||
| 649 | * @param string $text |
||
| 650 | * @return HtmlPageCrawler |
||
| 651 | * @api |
||
| 652 | */ |
||
| 653 | 1 | public function setText($text) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
| 664 | * |
||
| 665 | * @param string $classname One or more classnames separated by spaces |
||
| 666 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 667 | * @api |
||
| 668 | */ |
||
| 669 | 1 | public function toggleClass($classname) |
|
| 685 | |||
| 686 | /** |
||
| 687 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
| 688 | * |
||
| 689 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 690 | * @api |
||
| 691 | */ |
||
| 692 | 1 | public function unwrap() |
|
| 702 | |||
| 703 | /** |
||
| 704 | * Remove the matched elements, but promote the children to take their place. |
||
| 705 | * |
||
| 706 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 707 | * @api |
||
| 708 | */ |
||
| 709 | 2 | public function unwrapInner() |
|
| 725 | |||
| 726 | |||
| 727 | /** |
||
| 728 | * Wrap an HTML structure around each element in the set of matched elements |
||
| 729 | * |
||
| 730 | * The HTML structure must contain only one root node, e.g.: |
||
| 731 | * Works: <div><div></div></div> |
||
| 732 | * Does not work: <div></div><div></div> |
||
| 733 | * |
||
| 734 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
| 735 | * @return HtmlPageCrawler $this for chaining |
||
| 736 | * @api |
||
| 737 | */ |
||
| 738 | 1 | public function wrap($wrappingElement) |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
| 778 | * |
||
| 779 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 780 | * @throws \LogicException |
||
| 781 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 782 | * @api |
||
| 783 | */ |
||
| 784 | 1 | public function wrapAll($content) |
|
| 822 | |||
| 823 | /** |
||
| 824 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
| 825 | * |
||
| 826 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 827 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 828 | * @api |
||
| 829 | */ |
||
| 830 | 1 | public function wrapInner($content) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Get the HTML code fragment of all elements and their contents. |
||
| 841 | * |
||
| 842 | * If the first node contains a complete HTML document return only |
||
| 843 | * the full code of this document. |
||
| 844 | * |
||
| 845 | * @return string HTML code (fragment) |
||
| 846 | * @api |
||
| 847 | */ |
||
| 848 | 8 | public function saveHTML() |
|
| 862 | |||
| 863 | 4 | public function __toString() |
|
| 867 | |||
| 868 | /** |
||
| 869 | * checks whether the first node contains a complete html document |
||
| 870 | * (as opposed to a document fragment) |
||
| 871 | * |
||
| 872 | * @return boolean |
||
| 873 | */ |
||
| 874 | 8 | public function isHtmlDocument() |
|
| 887 | |||
| 888 | /** |
||
| 889 | * get ownerDocument of the first element |
||
| 890 | * |
||
| 891 | * @return \DOMDocument|null |
||
| 892 | */ |
||
| 893 | 1 | public function getDOMDocument() |
|
| 904 | |||
| 905 | /** |
||
| 906 | * Filters the list of nodes with a CSS selector. |
||
| 907 | * |
||
| 908 | * @param string $selector |
||
| 909 | * @return HtmlPageCrawler |
||
| 910 | */ |
||
| 911 | 8 | public function filter($selector) |
|
| 915 | |||
| 916 | /** |
||
| 917 | * Filters the list of nodes with an XPath expression. |
||
| 918 | * |
||
| 919 | * @param string $xpath An XPath expression |
||
| 920 | * |
||
| 921 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
| 922 | * |
||
| 923 | * @api |
||
| 924 | */ |
||
| 925 | 2 | public function filterXPath($xpath) |
|
| 929 | |||
| 930 | /** |
||
| 931 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
| 932 | * |
||
| 933 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
| 934 | * |
||
| 935 | * |
||
| 936 | * @param string $content A string to parse as HTML/XML |
||
| 937 | * @param null|string $type The content type of the string |
||
| 938 | * |
||
| 939 | * @return null|void |
||
| 940 | */ |
||
| 941 | 17 | public function addContent($content, $type = null) |
|
| 953 | |||
| 954 | 15 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
| 966 | |||
| 967 | /** |
||
| 968 | * returns the first node |
||
| 969 | * deprecated, use getNode(0) instead |
||
| 970 | * |
||
| 971 | * @return \DOMNode|null |
||
| 972 | * @deprecated |
||
| 973 | * @see Crawler::getNode |
||
| 974 | */ |
||
| 975 | 1 | public function getFirstNode() |
|
| 979 | |||
| 980 | /** |
||
| 981 | * @param int $position |
||
| 982 | * |
||
| 983 | * overridden from Crawler because it is not public in Symfony 2.3 |
||
| 984 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 985 | * |
||
| 986 | * @return \DOMElement|null |
||
| 987 | */ |
||
| 988 | 13 | public function getNode($position) |
|
| 992 | |||
| 993 | /** |
||
| 994 | * Returns the node name of the first node of the list. |
||
| 995 | * |
||
| 996 | * in Crawler (parent), this function will be available starting with 2.6.0, |
||
| 997 | * therefore this method be removed from here as soon as we don't need to keep compatibility |
||
| 998 | * with Symfony < 2.6 |
||
| 999 | * |
||
| 1000 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1001 | * |
||
| 1002 | * @return string The node name |
||
| 1003 | * |
||
| 1004 | * @throws \InvalidArgumentException When current node is empty |
||
| 1005 | */ |
||
| 1006 | 1 | public function nodeName() |
|
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Adds a node to the current list of nodes. |
||
| 1016 | * |
||
| 1017 | * This method uses the appropriate specialized add*() method based |
||
| 1018 | * on the type of the argument. |
||
| 1019 | * |
||
| 1020 | * Overwritten from parent to allow Crawler to be added |
||
| 1021 | * |
||
| 1022 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
| 1023 | * |
||
| 1024 | * @api |
||
| 1025 | */ |
||
| 1026 | 28 | public function add($node) |
|
| 1036 | |||
| 1037 | /** |
||
| 1038 | * @param \DOMNode $newnode |
||
| 1039 | * @param \DOMNode $referencenode |
||
| 1040 | * @param int $clone |
||
| 1041 | * @return \DOMNode |
||
| 1042 | */ |
||
| 1043 | 6 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
| 1056 | * |
||
| 1057 | * @return bool |
||
| 1058 | */ |
||
| 1059 | 1 | public function isDisconnected() |
|
| 1064 | |||
| 1065 | 1 | public function __get($name) |
|
| 1074 | } |
||
| 1075 |
Really long classes often contain too much logic and violate the single responsibility principle.
We suggest to take a look at the “Code” section for options on how to refactor this code.