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 | 18 | 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 | * Get the HTML contents of the first element in the set of matched elements |
||
| 357 | * or set the HTML contents of every matched element. |
||
| 358 | * |
||
| 359 | * Deprecation warning: It will not be possible any more to use method html($html) as setter function in version 2.0. Use setInnerHtml($html) instead. |
||
| 360 | * |
||
| 361 | * @see HtmlPageCrawler::setInnerHtml() |
||
| 362 | * |
||
| 363 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|null $html The HTML content to set, or NULL to get the current content |
||
| 364 | * |
||
| 365 | * @return HtmlPageCrawler|string If no param is provided, returns the HTML content of the first element |
||
| 366 | * @api |
||
| 367 | */ |
||
| 368 | 1 | public function html($html = null) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Get the innerHTML contents of the first element |
||
| 381 | * |
||
| 382 | * @return string HTML code fragment |
||
| 383 | * @deprecated Method is deprecated and will be removed in 2.0, use html() instead |
||
| 384 | * @see html() |
||
| 385 | */ |
||
| 386 | 4 | public function getInnerHtml() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Set the HTML contents of each element |
||
| 403 | * |
||
| 404 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 405 | * @return HtmlPageCrawler $this for chaining |
||
| 406 | */ |
||
| 407 | 3 | public function setInnerHtml($content) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Insert every element in the set of matched elements after the target. |
||
| 424 | * |
||
| 425 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 426 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 427 | * @api |
||
| 428 | */ |
||
| 429 | 2 | public function insertAfter($element) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Insert every element in the set of matched elements before the target. |
||
| 452 | * |
||
| 453 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 454 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 455 | * @api |
||
| 456 | */ |
||
| 457 | 2 | public function insertBefore($element) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
| 477 | * |
||
| 478 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 479 | * @return HtmlPageCrawler $this for chaining |
||
| 480 | * @api |
||
| 481 | */ |
||
| 482 | 2 | public function prepend($content) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Insert every element in the set of matched elements to the beginning of the target. |
||
| 507 | * |
||
| 508 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 509 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
| 510 | * @api |
||
| 511 | */ |
||
| 512 | 1 | public function prependTo($element) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Remove the set of matched elements from the DOM. |
||
| 537 | * |
||
| 538 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
| 539 | * but leaves them in the DOM) |
||
| 540 | * |
||
| 541 | * @api |
||
| 542 | */ |
||
| 543 | 2 | public function remove() |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Remove an attribute from each element in the set of matched elements. |
||
| 558 | * |
||
| 559 | * Alias for removeAttribute for compatibility with jQuery |
||
| 560 | * |
||
| 561 | * @param string $name |
||
| 562 | * @return HtmlPageCrawler |
||
| 563 | * @api |
||
| 564 | */ |
||
| 565 | 1 | public function removeAttr($name) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Remove an attribute from each element in the set of matched elements. |
||
| 572 | * |
||
| 573 | * @param string $name |
||
| 574 | * @return HtmlPageCrawler |
||
| 575 | */ |
||
| 576 | 1 | public function removeAttribute($name) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Remove a class from each element in the list |
||
| 591 | * |
||
| 592 | * @param string $name |
||
| 593 | * @return HtmlPageCrawler $this for chaining |
||
| 594 | * @api |
||
| 595 | */ |
||
| 596 | 2 | public function removeClass($name) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Replace each target element with the set of matched elements. |
||
| 616 | * |
||
| 617 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 618 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 619 | * @api |
||
| 620 | */ |
||
| 621 | 2 | public function replaceAll($element) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
| 645 | * |
||
| 646 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 647 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 648 | * @api |
||
| 649 | */ |
||
| 650 | 2 | public function replaceWith($content) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Get the combined text contents of each element in the set of matched elements, including their descendants, |
||
| 676 | * or set the text contents of the matched elements. |
||
| 677 | * |
||
| 678 | * ATTENTION: Contrary to the parent Crawler class, which returns the text from the first element only, |
||
| 679 | * this functions returns the combined text of all elements (as jQuery does). If this is not what you need you |
||
| 680 | * must call ->first() before calling ->text(), e.g. |
||
| 681 | * |
||
| 682 | * in Symfony\DOMCrawler\Crawler: $c->filter('p')->text() returns the text of the first paragraph only |
||
| 683 | * in HtmlPageCrawler you need to call: $c->filter('p')->first()->text() |
||
| 684 | * |
||
| 685 | * DEPRECATION WARNING: |
||
| 686 | * This function will be removed from here in 2.0, so calling text() then will call the parent implementation, i.e. |
||
| 687 | * it will return the text from the first node only, and an argument passed is treated as default value for the |
||
| 688 | * getter function from Symfony 4.3 onwards. It will not be possible to use this function as setter. |
||
| 689 | * |
||
| 690 | * Use getCombinedText() for a getter with the old behavior, and use setText() for setting text content. |
||
| 691 | * |
||
| 692 | * @see setText() |
||
| 693 | * @see getCombinedText() |
||
| 694 | * |
||
| 695 | * @param null|string $text |
||
| 696 | * @return string|HtmlPageCrawler |
||
| 697 | * @api |
||
| 698 | */ |
||
| 699 | 1 | public function text($text = null) |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Get the combined text contents of each element in the set of matched elements, including their descendants. |
||
| 721 | * This is what the jQuery text() function does, contrary to the Crawler::text() method that returns only |
||
| 722 | * the text of the first node. |
||
| 723 | * |
||
| 724 | * @return string |
||
| 725 | * @api |
||
| 726 | * @since 1.4 |
||
| 727 | */ |
||
| 728 | public function getCombinedText() |
||
| 737 | /** |
||
| 738 | * Set the text contents of the matched elements. |
||
| 739 | * |
||
| 740 | * @param string $text |
||
| 741 | * @return HtmlPageCrawler |
||
| 742 | * @api |
||
| 743 | * @since 1.4 |
||
| 744 | */ |
||
| 745 | public function setText($text) |
||
| 753 | |||
| 754 | |||
| 755 | /** |
||
| 756 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
| 757 | * |
||
| 758 | * @param string $classname One or more classnames separated by spaces |
||
| 759 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 760 | * @api |
||
| 761 | */ |
||
| 762 | 1 | public function toggleClass($classname) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
| 781 | * |
||
| 782 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 783 | * @api |
||
| 784 | */ |
||
| 785 | 1 | public function unwrap() |
|
| 795 | |||
| 796 | /** |
||
| 797 | * Remove the matched elements, but promote the children to take their place. |
||
| 798 | * |
||
| 799 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 800 | * @api |
||
| 801 | */ |
||
| 802 | 2 | public function unwrapInner() |
|
| 818 | |||
| 819 | |||
| 820 | /** |
||
| 821 | * Wrap an HTML structure around each element in the set of matched elements |
||
| 822 | * |
||
| 823 | * The HTML structure must contain only one root node, e.g.: |
||
| 824 | * Works: <div><div></div></div> |
||
| 825 | * Does not work: <div></div><div></div> |
||
| 826 | * |
||
| 827 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
| 828 | * @return HtmlPageCrawler $this for chaining |
||
| 829 | * @api |
||
| 830 | */ |
||
| 831 | 1 | public function wrap($wrappingElement) |
|
| 868 | |||
| 869 | /** |
||
| 870 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
| 871 | * |
||
| 872 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 873 | * @throws \LogicException |
||
| 874 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 875 | * @api |
||
| 876 | */ |
||
| 877 | 1 | public function wrapAll($content) |
|
| 915 | |||
| 916 | /** |
||
| 917 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
| 918 | * |
||
| 919 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 920 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 921 | * @api |
||
| 922 | */ |
||
| 923 | 1 | public function wrapInner($content) |
|
| 931 | |||
| 932 | /** |
||
| 933 | * Get the HTML code fragment of all elements and their contents. |
||
| 934 | * |
||
| 935 | * If the first node contains a complete HTML document return only |
||
| 936 | * the full code of this document. |
||
| 937 | * |
||
| 938 | * @return string HTML code (fragment) |
||
| 939 | * @api |
||
| 940 | */ |
||
| 941 | 8 | public function saveHTML() |
|
| 955 | |||
| 956 | 4 | public function __toString() |
|
| 960 | |||
| 961 | /** |
||
| 962 | * checks whether the first node contains a complete html document |
||
| 963 | * (as opposed to a document fragment) |
||
| 964 | * |
||
| 965 | * @return boolean |
||
| 966 | */ |
||
| 967 | 8 | public function isHtmlDocument() |
|
| 980 | |||
| 981 | /** |
||
| 982 | * get ownerDocument of the first element |
||
| 983 | * |
||
| 984 | * @return \DOMDocument|null |
||
| 985 | */ |
||
| 986 | 1 | public function getDOMDocument() |
|
| 997 | |||
| 998 | /** |
||
| 999 | * Filters the list of nodes with a CSS selector. |
||
| 1000 | * |
||
| 1001 | * @param string $selector |
||
| 1002 | * @return HtmlPageCrawler |
||
| 1003 | */ |
||
| 1004 | 8 | public function filter($selector) |
|
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Filters the list of nodes with an XPath expression. |
||
| 1011 | * |
||
| 1012 | * @param string $xpath An XPath expression |
||
| 1013 | * |
||
| 1014 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
| 1015 | * |
||
| 1016 | * @api |
||
| 1017 | */ |
||
| 1018 | 2 | public function filterXPath($xpath) |
|
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
| 1025 | * |
||
| 1026 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
| 1027 | * |
||
| 1028 | * |
||
| 1029 | * @param string $content A string to parse as HTML/XML |
||
| 1030 | * @param null|string $type The content type of the string |
||
| 1031 | * |
||
| 1032 | * @return null|void |
||
| 1033 | */ |
||
| 1034 | 17 | public function addContent($content, $type = null) |
|
| 1046 | |||
| 1047 | 15 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
| 1059 | |||
| 1060 | /** |
||
| 1061 | * returns the first node |
||
| 1062 | * deprecated, use getNode(0) instead |
||
| 1063 | * |
||
| 1064 | * @return \DOMNode|null |
||
| 1065 | * @deprecated |
||
| 1066 | * @see Crawler::getNode |
||
| 1067 | */ |
||
| 1068 | 1 | public function getFirstNode() |
|
| 1072 | |||
| 1073 | /** |
||
| 1074 | * @param int $position |
||
| 1075 | * |
||
| 1076 | * overridden from Crawler because it is not public in Symfony 2.3 |
||
| 1077 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1078 | * |
||
| 1079 | * @return \DOMElement|null |
||
| 1080 | */ |
||
| 1081 | 14 | public function getNode($position) |
|
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Returns the node name of the first node of the list. |
||
| 1088 | * |
||
| 1089 | * in Crawler (parent), this function will be available starting with 2.6.0, |
||
| 1090 | * therefore this method be removed from here as soon as we don't need to keep compatibility |
||
| 1091 | * with Symfony < 2.6 |
||
| 1092 | * |
||
| 1093 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1094 | * |
||
| 1095 | * @return string The node name |
||
| 1096 | * |
||
| 1097 | * @throws \InvalidArgumentException When current node is empty |
||
| 1098 | */ |
||
| 1099 | 1 | public function nodeName() |
|
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Adds a node to the current list of nodes. |
||
| 1109 | * |
||
| 1110 | * This method uses the appropriate specialized add*() method based |
||
| 1111 | * on the type of the argument. |
||
| 1112 | * |
||
| 1113 | * Overwritten from parent to allow Crawler to be added |
||
| 1114 | * |
||
| 1115 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
| 1116 | * |
||
| 1117 | * @api |
||
| 1118 | */ |
||
| 1119 | 29 | public function add($node) |
|
| 1129 | |||
| 1130 | /** |
||
| 1131 | * @param \DOMNode $newnode |
||
| 1132 | * @param \DOMNode $referencenode |
||
| 1133 | * @param int $clone |
||
| 1134 | * @return \DOMNode |
||
| 1135 | */ |
||
| 1136 | 6 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
| 1146 | |||
| 1147 | /** |
||
| 1148 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
| 1149 | * |
||
| 1150 | * @return bool |
||
| 1151 | */ |
||
| 1152 | 1 | public function isDisconnected() |
|
| 1157 | |||
| 1158 | 1 | public function __get($name) |
|
| 1167 | } |
||
| 1168 |
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.