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) |
|
709 | |||
710 | /** |
||
711 | * Get the combined text contents of each element in the set of matched elements, including their descendants. |
||
712 | * This is what the jQuery text() function does, contrary to the Crawler::text() method that returns only |
||
713 | * the text of the first node. |
||
714 | * |
||
715 | * @return string |
||
716 | * @api |
||
717 | * @since 1.4 |
||
718 | */ |
||
719 | 1 | public function getCombinedText() |
|
728 | /** |
||
729 | * Set the text contents of the matched elements. |
||
730 | * |
||
731 | * @param string $text |
||
732 | * @return HtmlPageCrawler |
||
733 | * @api |
||
734 | * @since 1.4 |
||
735 | */ |
||
736 | 1 | public function setText($text) |
|
744 | |||
745 | |||
746 | /** |
||
747 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
748 | * |
||
749 | * @param string $classname One or more classnames separated by spaces |
||
750 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
751 | * @api |
||
752 | */ |
||
753 | 1 | public function toggleClass($classname) |
|
769 | |||
770 | /** |
||
771 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
772 | * |
||
773 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
774 | * @api |
||
775 | */ |
||
776 | 1 | public function unwrap() |
|
786 | |||
787 | /** |
||
788 | * Remove the matched elements, but promote the children to take their place. |
||
789 | * |
||
790 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
791 | * @api |
||
792 | */ |
||
793 | 2 | public function unwrapInner() |
|
809 | |||
810 | |||
811 | /** |
||
812 | * Wrap an HTML structure around each element in the set of matched elements |
||
813 | * |
||
814 | * The HTML structure must contain only one root node, e.g.: |
||
815 | * Works: <div><div></div></div> |
||
816 | * Does not work: <div></div><div></div> |
||
817 | * |
||
818 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
819 | * @return HtmlPageCrawler $this for chaining |
||
820 | * @api |
||
821 | */ |
||
822 | 1 | public function wrap($wrappingElement) |
|
859 | |||
860 | /** |
||
861 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
862 | * |
||
863 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
864 | * @throws \LogicException |
||
865 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
866 | * @api |
||
867 | */ |
||
868 | 1 | public function wrapAll($content) |
|
906 | |||
907 | /** |
||
908 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
909 | * |
||
910 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
911 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
912 | * @api |
||
913 | */ |
||
914 | 1 | public function wrapInner($content) |
|
922 | |||
923 | /** |
||
924 | * Get the HTML code fragment of all elements and their contents. |
||
925 | * |
||
926 | * If the first node contains a complete HTML document return only |
||
927 | * the full code of this document. |
||
928 | * |
||
929 | * @return string HTML code (fragment) |
||
930 | * @api |
||
931 | */ |
||
932 | 8 | public function saveHTML() |
|
946 | |||
947 | 4 | public function __toString() |
|
951 | |||
952 | /** |
||
953 | * checks whether the first node contains a complete html document |
||
954 | * (as opposed to a document fragment) |
||
955 | * |
||
956 | * @return boolean |
||
957 | */ |
||
958 | 8 | public function isHtmlDocument() |
|
971 | |||
972 | /** |
||
973 | * get ownerDocument of the first element |
||
974 | * |
||
975 | * @return \DOMDocument|null |
||
976 | */ |
||
977 | 1 | public function getDOMDocument() |
|
988 | |||
989 | /** |
||
990 | * Filters the list of nodes with a CSS selector. |
||
991 | * |
||
992 | * @param string $selector |
||
993 | * @return HtmlPageCrawler |
||
994 | */ |
||
995 | 8 | public function filter($selector) |
|
999 | |||
1000 | /** |
||
1001 | * Filters the list of nodes with an XPath expression. |
||
1002 | * |
||
1003 | * @param string $xpath An XPath expression |
||
1004 | * |
||
1005 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
1006 | * |
||
1007 | * @api |
||
1008 | */ |
||
1009 | 2 | public function filterXPath($xpath) |
|
1013 | |||
1014 | /** |
||
1015 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
1016 | * |
||
1017 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
1018 | * |
||
1019 | * |
||
1020 | * @param string $content A string to parse as HTML/XML |
||
1021 | * @param null|string $type The content type of the string |
||
1022 | * |
||
1023 | * @return null|void |
||
1024 | */ |
||
1025 | 17 | public function addContent($content, $type = null) |
|
1037 | |||
1038 | 15 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
1050 | |||
1051 | /** |
||
1052 | * returns the first node |
||
1053 | * deprecated, use getNode(0) instead |
||
1054 | * |
||
1055 | * @return \DOMNode|null |
||
1056 | * @deprecated |
||
1057 | * @see Crawler::getNode |
||
1058 | */ |
||
1059 | 1 | public function getFirstNode() |
|
1063 | |||
1064 | /** |
||
1065 | * @param int $position |
||
1066 | * |
||
1067 | * overridden from Crawler because it is not public in Symfony 2.3 |
||
1068 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
1069 | * |
||
1070 | * @return \DOMElement|null |
||
1071 | */ |
||
1072 | 14 | public function getNode($position) |
|
1076 | |||
1077 | /** |
||
1078 | * Returns the node name of the first node of the list. |
||
1079 | * |
||
1080 | * in Crawler (parent), this function will be available starting with 2.6.0, |
||
1081 | * therefore this method be removed from here as soon as we don't need to keep compatibility |
||
1082 | * with Symfony < 2.6 |
||
1083 | * |
||
1084 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
1085 | * |
||
1086 | * @return string The node name |
||
1087 | * |
||
1088 | * @throws \InvalidArgumentException When current node is empty |
||
1089 | */ |
||
1090 | 1 | public function nodeName() |
|
1097 | |||
1098 | /** |
||
1099 | * Adds a node to the current list of nodes. |
||
1100 | * |
||
1101 | * This method uses the appropriate specialized add*() method based |
||
1102 | * on the type of the argument. |
||
1103 | * |
||
1104 | * Overwritten from parent to allow Crawler to be added |
||
1105 | * |
||
1106 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
1107 | * |
||
1108 | * @api |
||
1109 | */ |
||
1110 | 29 | public function add($node) |
|
1120 | |||
1121 | /** |
||
1122 | * @param \DOMNode $newnode |
||
1123 | * @param \DOMNode $referencenode |
||
1124 | * @param int $clone |
||
1125 | * @return \DOMNode |
||
1126 | */ |
||
1127 | 6 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
1137 | |||
1138 | /** |
||
1139 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
1140 | * |
||
1141 | * @return bool |
||
1142 | */ |
||
1143 | 1 | public function isDisconnected() |
|
1148 | |||
1149 | 1 | public function __get($name) |
|
1158 | } |
||
1159 |
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.