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 |
||
16 | class HtmlPageCrawler extends Crawler |
||
17 | { |
||
18 | /** |
||
19 | * the (internal) root element name used when importing html fragments |
||
20 | * */ |
||
21 | const FRAGMENT_ROOT_TAGNAME = '_root'; |
||
22 | |||
23 | /** |
||
24 | * Get an HtmlPageCrawler object from a HTML string, DOMNode, DOMNodeList or HtmlPageCrawler |
||
25 | * |
||
26 | * This is the equivalent to jQuery's $() function when used for wrapping DOMNodes or creating DOMElements from HTML code. |
||
27 | * |
||
28 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|array $content |
||
29 | * @return HtmlPageCrawler |
||
30 | * @api |
||
31 | */ |
||
32 | 11 | public static function create($content) |
|
40 | |||
41 | /** |
||
42 | * Adds the specified class(es) to each element in the set of matched elements. |
||
43 | * |
||
44 | * @param string $name One or more space-separated classes to be added to the class attribute of each matched element. |
||
45 | * @return HtmlPageCrawler $this for chaining |
||
46 | * @api |
||
47 | */ |
||
48 | 1 | public function addClass($name) |
|
69 | |||
70 | /** |
||
71 | * Insert content, specified by the parameter, after each element in the set of matched elements. |
||
72 | * |
||
73 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
74 | * @return HtmlPageCrawler $this for chaining |
||
75 | * @api |
||
76 | */ |
||
77 | 3 | public function after($content) |
|
99 | |||
100 | /** |
||
101 | * Insert HTML content as child nodes of each element after existing children |
||
102 | * |
||
103 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment or DOMNode to append |
||
104 | * @return HtmlPageCrawler $this for chaining |
||
105 | * @api |
||
106 | */ |
||
107 | 2 | public function append($content) |
|
131 | |||
132 | /** |
||
133 | * Insert every element in the set of matched elements to the end of the target. |
||
134 | * |
||
135 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
136 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
137 | * @api |
||
138 | */ |
||
139 | 1 | public function appendTo($element) |
|
154 | |||
155 | /** |
||
156 | * Returns the attribute value of the first node of the list, or sets an attribute on each element |
||
157 | * |
||
158 | * @see HtmlPageCrawler::getAttribute() |
||
159 | * @see HtmlPageCrawler::setAttribute |
||
160 | * |
||
161 | * @param string $name |
||
162 | * @param null|string $value |
||
163 | * @return null|string|HtmlPageCrawler |
||
164 | * @api |
||
165 | */ |
||
166 | 1 | public function attr($name, $value = null) |
|
174 | |||
175 | /** |
||
176 | * Sets an attribute on each element |
||
177 | * |
||
178 | * @param string $name |
||
179 | * @param string $value |
||
180 | * @return HtmlPageCrawler $this for chaining |
||
181 | */ |
||
182 | 2 | public function setAttribute($name, $value) |
|
192 | |||
193 | /** |
||
194 | * Returns the attribute value of the first node of the list. |
||
195 | * |
||
196 | * @param string $name The attribute name |
||
197 | * @return string|null The attribute value or null if the attribute does not exist |
||
198 | * @throws \InvalidArgumentException When current node is empty |
||
199 | * |
||
200 | */ |
||
201 | 1 | public function getAttribute($name) |
|
209 | |||
210 | /** |
||
211 | * Insert content, specified by the parameter, before each element in the set of matched elements. |
||
212 | * |
||
213 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
214 | * @return HtmlPageCrawler $this for chaining |
||
215 | * @api |
||
216 | */ |
||
217 | 2 | public function before($content) |
|
234 | |||
235 | /** |
||
236 | * Create a deep copy of the set of matched elements. |
||
237 | * |
||
238 | * Equivalent to clone() in jQuery (clone is not a valid PHP function name) |
||
239 | * |
||
240 | * @return HtmlPageCrawler |
||
241 | * @api |
||
242 | */ |
||
243 | 1 | public function makeClone() |
|
247 | |||
248 | 1 | public function __clone() |
|
258 | |||
259 | /** |
||
260 | * Get one CSS style property of the first element or set it for all elements in the list |
||
261 | * |
||
262 | * Function is here for compatibility with jQuery; it is the same as getStyle() and setStyle() |
||
263 | * |
||
264 | * @see HtmlPageCrawler::getStyle() |
||
265 | * @see HtmlPageCrawler::setStyle() |
||
266 | * |
||
267 | * @param string $key The name of the style property |
||
268 | * @param null|string $value The CSS value to set, or NULL to get the current value |
||
269 | * @return HtmlPageCrawler|string If no param is provided, returns the CSS styles of the first element |
||
270 | * @api |
||
271 | */ |
||
272 | 1 | public function css($key, $value = null) |
|
280 | |||
281 | /** |
||
282 | * get one CSS style property of the first element |
||
283 | * |
||
284 | * @param string $key name of the property |
||
285 | * @return string|null value of the property |
||
286 | */ |
||
287 | 1 | public function getStyle($key) |
|
292 | |||
293 | /** |
||
294 | * set one CSS style property for all elements in the list |
||
295 | * |
||
296 | * @param string $key name of the property |
||
297 | * @param string $value value of the property |
||
298 | * @return HtmlPageCrawler $this for chaining |
||
299 | */ |
||
300 | 1 | public function setStyle($key, $value) |
|
316 | |||
317 | /** |
||
318 | * Removes all child nodes and text from all nodes in set |
||
319 | * |
||
320 | * Equivalent to jQuery's empty() function which is not a valid function name in PHP |
||
321 | * @return HtmlPageCrawler $this |
||
322 | * @api |
||
323 | */ |
||
324 | public function makeEmpty() |
||
331 | |||
332 | /** |
||
333 | * Determine whether any of the matched elements are assigned the given class. |
||
334 | * |
||
335 | * @param string $name |
||
336 | * @return bool |
||
337 | * @api |
||
338 | */ |
||
339 | 2 | public function hasClass($name) |
|
351 | |||
352 | /** |
||
353 | * Get the HTML contents of the first element in the set of matched elements |
||
354 | * or set the HTML contents of every matched element. |
||
355 | * |
||
356 | * Function is here for compatibility with jQuery: When called with a parameter, it is |
||
357 | * equivalent to setInnerHtml(), without parameter it is the same as getInnerHtml() |
||
358 | * |
||
359 | * @see HtmlPageCrawler::setInnerHtml() |
||
360 | * @see HtmlPageCrawler::getInnerHtml() |
||
361 | * |
||
362 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|null $html The HTML content to set, or NULL to get the current content |
||
363 | * |
||
364 | * @return HtmlPageCrawler|string If no param is provided, returns the HTML content of the first element |
||
365 | * @api |
||
366 | */ |
||
367 | public function html($html = null) |
||
376 | |||
377 | /** |
||
378 | * Get the innerHTML contents of the first element |
||
379 | * |
||
380 | * @return string HTML code fragment |
||
381 | */ |
||
382 | 2 | public function getInnerHtml() |
|
386 | |||
387 | /** |
||
388 | * Set the HTML contents of each element |
||
389 | * |
||
390 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
391 | * @return HtmlPageCrawler $this for chaining |
||
392 | */ |
||
393 | 1 | public function setInnerHtml($content) |
|
407 | |||
408 | /** |
||
409 | * Insert every element in the set of matched elements after the target. |
||
410 | * |
||
411 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
412 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
413 | * @api |
||
414 | */ |
||
415 | 1 | public function insertAfter($element) |
|
435 | |||
436 | /** |
||
437 | * Insert every element in the set of matched elements before the target. |
||
438 | * |
||
439 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
440 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
441 | * @api |
||
442 | */ |
||
443 | 1 | public function insertBefore($element) |
|
458 | |||
459 | /** |
||
460 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
461 | * |
||
462 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
463 | * @return HtmlPageCrawler $this for chaining |
||
464 | * @api |
||
465 | */ |
||
466 | 1 | public function prepend($content) |
|
488 | |||
489 | /** |
||
490 | * Insert every element in the set of matched elements to the beginning of the target. |
||
491 | * |
||
492 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
493 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
494 | * @api |
||
495 | */ |
||
496 | public function prependTo($element) |
||
516 | |||
517 | /** |
||
518 | * Remove the set of matched elements from the DOM. |
||
519 | * |
||
520 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
521 | * but leaves them in the DOM) |
||
522 | * |
||
523 | * @api |
||
524 | */ |
||
525 | 2 | public function remove() |
|
537 | |||
538 | /** |
||
539 | * Remove an attribute from each element in the set of matched elements. |
||
540 | * |
||
541 | * Alias for removeAttribute for compatibility with jQuery |
||
542 | * |
||
543 | * @param string $name |
||
544 | * @return HtmlPageCrawler |
||
545 | * @api |
||
546 | */ |
||
547 | 1 | public function removeAttr($name) |
|
551 | |||
552 | /** |
||
553 | * Remove an attribute from each element in the set of matched elements. |
||
554 | * |
||
555 | * @param string $name |
||
556 | * @return HtmlPageCrawler |
||
557 | */ |
||
558 | 1 | public function removeAttribute($name) |
|
570 | |||
571 | /** |
||
572 | * Remove a class from each element in the list |
||
573 | * |
||
574 | * @param string $name |
||
575 | * @return HtmlPageCrawler $this for chaining |
||
576 | * @api |
||
577 | */ |
||
578 | 2 | public function removeClass($name) |
|
595 | |||
596 | /** |
||
597 | * Replace each target element with the set of matched elements. |
||
598 | * |
||
599 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
600 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
601 | * @api |
||
602 | */ |
||
603 | 2 | public function replaceAll($element) |
|
624 | |||
625 | /** |
||
626 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
627 | * |
||
628 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
629 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
630 | * @api |
||
631 | */ |
||
632 | 2 | public function replaceWith($content) |
|
655 | |||
656 | /** |
||
657 | * Get the combined text contents of each element in the set of matched elements, including their descendants, |
||
658 | * or set the text contents of the matched elements. |
||
659 | * |
||
660 | * ATTENTION: Contrary to the parent Crawler class, which returns the text from the first element only, |
||
661 | * this functions returns the combined text of all elements (as jQuery does). If this is not what you need you |
||
662 | * must call ->first() before calling ->text(), e.g. |
||
663 | * |
||
664 | * in Symfony\DOMCrawler\Crawler: $c->filter('p')->text() returns the text of the first paragraph only |
||
665 | * in HtmlPageCrawler you need to call: $c->filter('p')->first()->text() |
||
666 | * |
||
667 | * @param null|string $text |
||
668 | * @return string|HtmlPageCrawler |
||
669 | * @api |
||
670 | */ |
||
671 | 1 | public function text($text = null) |
|
688 | |||
689 | |||
690 | /** |
||
691 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
692 | * |
||
693 | * @param string $classname One or more classnames separated by spaces |
||
694 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
695 | * @api |
||
696 | */ |
||
697 | 1 | public function toggleClass($classname) |
|
713 | |||
714 | /** |
||
715 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
716 | * |
||
717 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
718 | * @api |
||
719 | */ |
||
720 | 1 | public function unwrap() |
|
730 | |||
731 | /** |
||
732 | * Remove the matched elements, but promote the children to take their place. |
||
733 | * |
||
734 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
735 | * @api |
||
736 | */ |
||
737 | 1 | public function unwrapInner() |
|
753 | |||
754 | |||
755 | /** |
||
756 | * Wrap an HTML structure around each element in the set of matched elements |
||
757 | * |
||
758 | * The HTML structure must contain only one root node, e.g.: |
||
759 | * Works: <div><div></div></div> |
||
760 | * Does not work: <div></div><div></div> |
||
761 | * |
||
762 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
763 | * @return HtmlPageCrawler $this for chaining |
||
764 | * @api |
||
765 | */ |
||
766 | 1 | public function wrap($wrappingElement) |
|
803 | |||
804 | /** |
||
805 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
806 | * |
||
807 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
808 | * @throws \LogicException |
||
809 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
810 | * @api |
||
811 | */ |
||
812 | 1 | public function wrapAll($content) |
|
850 | |||
851 | /** |
||
852 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
853 | * |
||
854 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
855 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
856 | * @api |
||
857 | */ |
||
858 | 1 | public function wrapInner($content) |
|
866 | |||
867 | /** |
||
868 | * Get the HTML code fragment of all elements and their contents. |
||
869 | * |
||
870 | * If the first node contains a complete HTML document return the |
||
871 | * DocType if exists |
||
872 | * |
||
873 | * @return string HTML code (fragment) |
||
874 | * @api |
||
875 | */ |
||
876 | 3 | public function saveHTML() |
|
889 | |||
890 | public function __toString() |
||
894 | |||
895 | /** |
||
896 | * checks whether the first node contains a complete html document |
||
897 | * (as opposed to a document fragment) |
||
898 | * |
||
899 | * @return boolean |
||
900 | */ |
||
901 | 3 | public function isHtmlDocument() |
|
914 | |||
915 | /** |
||
916 | * get ownerDocument of the first element |
||
917 | * |
||
918 | * @return \DOMDocument|null |
||
919 | */ |
||
920 | public function getDOMDocument() |
||
931 | |||
932 | /** |
||
933 | * Filters the list of nodes with a CSS selector. |
||
934 | * |
||
935 | * @param string $selector |
||
936 | * @return HtmlPageCrawler |
||
937 | */ |
||
938 | 6 | public function filter($selector) |
|
942 | |||
943 | /** |
||
944 | * Filters the list of nodes with an XPath expression. |
||
945 | * |
||
946 | * @param string $xpath An XPath expression |
||
947 | * |
||
948 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
949 | * |
||
950 | * @api |
||
951 | */ |
||
952 | 1 | public function filterXPath($xpath) |
|
956 | |||
957 | /** |
||
958 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
959 | * |
||
960 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
961 | * |
||
962 | * |
||
963 | * @param string $content A string to parse as HTML/XML |
||
964 | * @param null|string $type The content type of the string |
||
965 | * |
||
966 | * @return null|void |
||
967 | */ |
||
968 | 12 | public function addContent($content, $type = null) |
|
980 | |||
981 | 10 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
993 | |||
994 | /** |
||
995 | * returns the first node |
||
996 | * deprecated, use getNode(0) instead |
||
997 | * |
||
998 | * @return \DOMNode|null |
||
999 | * @deprecated |
||
1000 | * @see Crawler::getNode |
||
1001 | */ |
||
1002 | 1 | public function getFirstNode() |
|
1006 | |||
1007 | /** |
||
1008 | * @param int $position |
||
1009 | * |
||
1010 | * overridden from Crawler because it is not public in Symfony 2.3 |
||
1011 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
1012 | * |
||
1013 | * @return \DOMElement|null |
||
1014 | */ |
||
1015 | 5 | public function getNode($position) |
|
1019 | |||
1020 | /** |
||
1021 | * Returns the node name of the first node of the list. |
||
1022 | * |
||
1023 | * in Crawler (parent), this function will be available starting with 2.6.0, |
||
1024 | * therefore this method be removed from here as soon as we don't need to keep compatibility |
||
1025 | * with Symfony < 2.6 |
||
1026 | * |
||
1027 | * TODO: throw away as soon as we don't need to support SF 2.3 any more |
||
1028 | * |
||
1029 | * @return string The node name |
||
1030 | * |
||
1031 | * @throws \InvalidArgumentException When current node is empty |
||
1032 | */ |
||
1033 | 1 | public function nodeName() |
|
1040 | |||
1041 | /** |
||
1042 | * Adds a node to the current list of nodes. |
||
1043 | * |
||
1044 | * This method uses the appropriate specialized add*() method based |
||
1045 | * on the type of the argument. |
||
1046 | * |
||
1047 | * Overwritten from parent to allow Crawler to be added |
||
1048 | * |
||
1049 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
1050 | * |
||
1051 | * @api |
||
1052 | */ |
||
1053 | 14 | public function add($node) |
|
1063 | |||
1064 | /** |
||
1065 | * @param \DOMNode $newnode |
||
1066 | * @param \DOMNode $referencenode |
||
1067 | * @param int $clone |
||
1068 | * @return \DOMNode |
||
1069 | */ |
||
1070 | 5 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
1080 | |||
1081 | /** |
||
1082 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
1083 | * |
||
1084 | * @return bool |
||
1085 | */ |
||
1086 | 1 | public function isDisconnected() |
|
1091 | |||
1092 | 1 | public function __get($name) |
|
1101 | } |
||
1102 |
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.