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 | * Function is here for compatibility with jQuery: When called with a parameter, it is |
||
| 360 | * equivalent to setInnerHtml(), without parameter it is the same as getInnerHtml() |
||
| 361 | * |
||
| 362 | * @see HtmlPageCrawler::setInnerHtml() |
||
| 363 | * @see HtmlPageCrawler::getInnerHtml() |
||
| 364 | * |
||
| 365 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|null $html The HTML content to set, or NULL to get the current content |
||
| 366 | * |
||
| 367 | * @return HtmlPageCrawler|string If no param is provided, returns the HTML content of the first element |
||
| 368 | * @api |
||
| 369 | */ |
||
| 370 | 1 | public function html($html = null) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Get the innerHTML contents of the first element |
||
| 382 | * |
||
| 383 | * @return string HTML code fragment |
||
| 384 | */ |
||
| 385 | 4 | public function getInnerHtml() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Set the HTML contents of each element |
||
| 401 | * |
||
| 402 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 403 | * @return HtmlPageCrawler $this for chaining |
||
| 404 | */ |
||
| 405 | 3 | public function setInnerHtml($content) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Insert every element in the set of matched elements after the target. |
||
| 422 | * |
||
| 423 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 424 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 425 | * @api |
||
| 426 | */ |
||
| 427 | 2 | public function insertAfter($element) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Insert every element in the set of matched elements before the target. |
||
| 450 | * |
||
| 451 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 452 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 453 | * @api |
||
| 454 | */ |
||
| 455 | 2 | public function insertBefore($element) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
| 475 | * |
||
| 476 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
| 477 | * @return HtmlPageCrawler $this for chaining |
||
| 478 | * @api |
||
| 479 | */ |
||
| 480 | 2 | public function prepend($content) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Insert every element in the set of matched elements to the beginning of the target. |
||
| 505 | * |
||
| 506 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 507 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
| 508 | * @api |
||
| 509 | */ |
||
| 510 | 1 | public function prependTo($element) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Remove the set of matched elements from the DOM. |
||
| 535 | * |
||
| 536 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
| 537 | * but leaves them in the DOM) |
||
| 538 | * |
||
| 539 | * @api |
||
| 540 | */ |
||
| 541 | 2 | public function remove() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Remove an attribute from each element in the set of matched elements. |
||
| 556 | * |
||
| 557 | * Alias for removeAttribute for compatibility with jQuery |
||
| 558 | * |
||
| 559 | * @param string $name |
||
| 560 | * @return HtmlPageCrawler |
||
| 561 | * @api |
||
| 562 | */ |
||
| 563 | 1 | public function removeAttr($name) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Remove an attribute from each element in the set of matched elements. |
||
| 570 | * |
||
| 571 | * @param string $name |
||
| 572 | * @return HtmlPageCrawler |
||
| 573 | */ |
||
| 574 | 1 | public function removeAttribute($name) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Remove a class from each element in the list |
||
| 589 | * |
||
| 590 | * @param string $name |
||
| 591 | * @return HtmlPageCrawler $this for chaining |
||
| 592 | * @api |
||
| 593 | */ |
||
| 594 | 2 | public function removeClass($name) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Replace each target element with the set of matched elements. |
||
| 614 | * |
||
| 615 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
| 616 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
| 617 | * @api |
||
| 618 | */ |
||
| 619 | 2 | public function replaceAll($element) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
| 643 | * |
||
| 644 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 645 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 646 | * @api |
||
| 647 | */ |
||
| 648 | 2 | public function replaceWith($content) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Get the combined text contents of each element in the set of matched elements, including their descendants, |
||
| 674 | * or set the text contents of the matched elements. |
||
| 675 | * |
||
| 676 | * ATTENTION: Contrary to the parent Crawler class, which returns the text from the first element only, |
||
| 677 | * this functions returns the combined text of all elements (as jQuery does). If this is not what you need you |
||
| 678 | * must call ->first() before calling ->text(), e.g. |
||
| 679 | * |
||
| 680 | * in Symfony\DOMCrawler\Crawler: $c->filter('p')->text() returns the text of the first paragraph only |
||
| 681 | * in HtmlPageCrawler you need to call: $c->filter('p')->first()->text() |
||
| 682 | * |
||
| 683 | * @param null|string $text |
||
| 684 | * @return string|HtmlPageCrawler |
||
| 685 | * @api |
||
| 686 | */ |
||
| 687 | 1 | public function text($text = null) |
|
| 704 | |||
| 705 | |||
| 706 | /** |
||
| 707 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
| 708 | * |
||
| 709 | * @param string $classname One or more classnames separated by spaces |
||
| 710 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 711 | * @api |
||
| 712 | */ |
||
| 713 | 1 | public function toggleClass($classname) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
| 732 | * |
||
| 733 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 734 | * @api |
||
| 735 | */ |
||
| 736 | 1 | public function unwrap() |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Remove the matched elements, but promote the children to take their place. |
||
| 749 | * |
||
| 750 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 751 | * @api |
||
| 752 | */ |
||
| 753 | 2 | public function unwrapInner() |
|
| 769 | |||
| 770 | |||
| 771 | /** |
||
| 772 | * Wrap an HTML structure around each element in the set of matched elements |
||
| 773 | * |
||
| 774 | * The HTML structure must contain only one root node, e.g.: |
||
| 775 | * Works: <div><div></div></div> |
||
| 776 | * Does not work: <div></div><div></div> |
||
| 777 | * |
||
| 778 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
| 779 | * @return HtmlPageCrawler $this for chaining |
||
| 780 | * @api |
||
| 781 | */ |
||
| 782 | 1 | public function wrap($wrappingElement) |
|
| 819 | |||
| 820 | /** |
||
| 821 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
| 822 | * |
||
| 823 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 824 | * @throws \LogicException |
||
| 825 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 826 | * @api |
||
| 827 | */ |
||
| 828 | 1 | public function wrapAll($content) |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
| 869 | * |
||
| 870 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
| 871 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
| 872 | * @api |
||
| 873 | */ |
||
| 874 | 1 | public function wrapInner($content) |
|
| 882 | |||
| 883 | /** |
||
| 884 | * Get the HTML code fragment of all elements and their contents. |
||
| 885 | * |
||
| 886 | * If the first node contains a complete HTML document return only |
||
| 887 | * the full code of this document. |
||
| 888 | * |
||
| 889 | * @return string HTML code (fragment) |
||
| 890 | * @api |
||
| 891 | */ |
||
| 892 | 8 | public function saveHTML() |
|
| 906 | |||
| 907 | 4 | public function __toString() |
|
| 911 | |||
| 912 | /** |
||
| 913 | * checks whether the first node contains a complete html document |
||
| 914 | * (as opposed to a document fragment) |
||
| 915 | * |
||
| 916 | * @return boolean |
||
| 917 | */ |
||
| 918 | 8 | public function isHtmlDocument() |
|
| 931 | |||
| 932 | /** |
||
| 933 | * get ownerDocument of the first element |
||
| 934 | * |
||
| 935 | * @return \DOMDocument|null |
||
| 936 | */ |
||
| 937 | 1 | public function getDOMDocument() |
|
| 948 | |||
| 949 | /** |
||
| 950 | * Filters the list of nodes with a CSS selector. |
||
| 951 | * |
||
| 952 | * @param string $selector |
||
| 953 | * @return HtmlPageCrawler |
||
| 954 | */ |
||
| 955 | 8 | public function filter($selector) |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Filters the list of nodes with an XPath expression. |
||
| 962 | * |
||
| 963 | * @param string $xpath An XPath expression |
||
| 964 | * |
||
| 965 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
| 966 | * |
||
| 967 | * @api |
||
| 968 | */ |
||
| 969 | 2 | public function filterXPath($xpath) |
|
| 973 | |||
| 974 | /** |
||
| 975 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
| 976 | * |
||
| 977 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
| 978 | * |
||
| 979 | * |
||
| 980 | * @param string $content A string to parse as HTML/XML |
||
| 981 | * @param null|string $type The content type of the string |
||
| 982 | * |
||
| 983 | * @return null|void |
||
| 984 | */ |
||
| 985 | 17 | public function addContent($content, $type = null) |
|
| 997 | |||
| 998 | 15 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
| 1010 | |||
| 1011 | /** |
||
| 1012 | * returns the first node |
||
| 1013 | * deprecated, use getNode(0) instead |
||
| 1014 | * |
||
| 1015 | * @return \DOMNode|null |
||
| 1016 | * @deprecated |
||
| 1017 | * @see Crawler::getNode |
||
| 1018 | */ |
||
| 1019 | 1 | public function getFirstNode() |
|
| 1023 | |||
| 1024 | /** |
||
| 1025 | * @param int $position |
||
| 1026 | * |
||
| 1027 | * overridden from Crawler because it is not public in Symfony 2.3 |
||
| 1028 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1029 | * |
||
| 1030 | * @return \DOMElement|null |
||
| 1031 | */ |
||
| 1032 | 14 | public function getNode($position) |
|
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Returns the node name of the first node of the list. |
||
| 1039 | * |
||
| 1040 | * in Crawler (parent), this function will be available starting with 2.6.0, |
||
| 1041 | * therefore this method be removed from here as soon as we don't need to keep compatibility |
||
| 1042 | * with Symfony < 2.6 |
||
| 1043 | * |
||
| 1044 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
| 1045 | * |
||
| 1046 | * @return string The node name |
||
| 1047 | * |
||
| 1048 | * @throws \InvalidArgumentException When current node is empty |
||
| 1049 | */ |
||
| 1050 | 1 | public function nodeName() |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Adds a node to the current list of nodes. |
||
| 1060 | * |
||
| 1061 | * This method uses the appropriate specialized add*() method based |
||
| 1062 | * on the type of the argument. |
||
| 1063 | * |
||
| 1064 | * Overwritten from parent to allow Crawler to be added |
||
| 1065 | * |
||
| 1066 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
| 1067 | * |
||
| 1068 | * @api |
||
| 1069 | */ |
||
| 1070 | 29 | public function add($node) |
|
| 1080 | |||
| 1081 | /** |
||
| 1082 | * @param \DOMNode $newnode |
||
| 1083 | * @param \DOMNode $referencenode |
||
| 1084 | * @param int $clone |
||
| 1085 | * @return \DOMNode |
||
| 1086 | */ |
||
| 1087 | 6 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
| 1097 | |||
| 1098 | /** |
||
| 1099 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
| 1100 | * |
||
| 1101 | * @return bool |
||
| 1102 | */ |
||
| 1103 | 1 | public function isDisconnected() |
|
| 1108 | |||
| 1109 | 1 | public function __get($name) |
|
| 1118 | } |
||
| 1119 |
The number of this metric differs depending on the chosen design (inheritance vs. composition). For inheritance, the number should generally be a bit lower.
A high number indicates a reusable class. It might also make the class harder to change without breaking other classes though.