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) |
|
123 | |||
124 | /** |
||
125 | * Insert every element in the set of matched elements to the end of the target. |
||
126 | * |
||
127 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
128 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
129 | * @api |
||
130 | */ |
||
131 | 2 | public function appendTo($element) |
|
148 | |||
149 | /** |
||
150 | * Sets an attribute on each element |
||
151 | * |
||
152 | * @param string $name |
||
153 | * @param string $value |
||
154 | * @return HtmlPageCrawler $this for chaining |
||
155 | * @api |
||
156 | */ |
||
157 | 3 | public function setAttribute($name, $value) |
|
167 | |||
168 | /** |
||
169 | * Returns the attribute value of the first node of the list. |
||
170 | * This is just an alias for attr() for naming consistency with setAttribute() |
||
171 | * |
||
172 | * @param string $name The attribute name |
||
173 | * @return string|null The attribute value or null if the attribute does not exist |
||
174 | * @throws \InvalidArgumentException When current node is empty |
||
175 | */ |
||
176 | 1 | public function getAttribute($name) |
|
180 | |||
181 | /** |
||
182 | * Insert content, specified by the parameter, before each element in the set of matched elements. |
||
183 | * |
||
184 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
185 | * @return HtmlPageCrawler $this for chaining |
||
186 | * @api |
||
187 | */ |
||
188 | 2 | public function before($content) |
|
207 | |||
208 | /** |
||
209 | * Create a deep copy of the set of matched elements. |
||
210 | * |
||
211 | * Equivalent to clone() in jQuery (clone is not a valid PHP function name) |
||
212 | * |
||
213 | * @return HtmlPageCrawler |
||
214 | * @api |
||
215 | */ |
||
216 | 1 | public function makeClone() |
|
220 | |||
221 | 1 | public function __clone() |
|
231 | |||
232 | /** |
||
233 | * Get one CSS style property of the first element or set it for all elements in the list |
||
234 | * |
||
235 | * Function is here for compatibility with jQuery; it is the same as getStyle() and setStyle() |
||
236 | * |
||
237 | * @see HtmlPageCrawler::getStyle() |
||
238 | * @see HtmlPageCrawler::setStyle() |
||
239 | * |
||
240 | * @param string $key The name of the style property |
||
241 | * @param null|string $value The CSS value to set, or NULL to get the current value |
||
242 | * @return HtmlPageCrawler|string If no param is provided, returns the CSS styles of the first element |
||
243 | * @api |
||
244 | */ |
||
245 | 1 | public function css($key, $value = null) |
|
253 | |||
254 | /** |
||
255 | * get one CSS style property of the first element |
||
256 | * |
||
257 | * @param string $key name of the property |
||
258 | * @return string|null value of the property |
||
259 | */ |
||
260 | 1 | public function getStyle($key) |
|
265 | |||
266 | /** |
||
267 | * set one CSS style property for all elements in the list |
||
268 | * |
||
269 | * @param string $key name of the property |
||
270 | * @param string $value value of the property |
||
271 | * @return HtmlPageCrawler $this for chaining |
||
272 | */ |
||
273 | 1 | public function setStyle($key, $value) |
|
289 | |||
290 | /** |
||
291 | * Removes all child nodes and text from all nodes in set |
||
292 | * |
||
293 | * Equivalent to jQuery's empty() function which is not a valid function name in PHP |
||
294 | * @return HtmlPageCrawler $this |
||
295 | * @api |
||
296 | */ |
||
297 | 1 | public function makeEmpty() |
|
304 | |||
305 | /** |
||
306 | * Determine whether any of the matched elements are assigned the given class. |
||
307 | * |
||
308 | * @param string $name |
||
309 | * @return bool |
||
310 | * @api |
||
311 | */ |
||
312 | 2 | public function hasClass($name) |
|
324 | |||
325 | /** |
||
326 | * Set the HTML contents of each element |
||
327 | * |
||
328 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
329 | * @return HtmlPageCrawler $this for chaining |
||
330 | * @api |
||
331 | */ |
||
332 | 3 | public function setInnerHtml($content) |
|
346 | |||
347 | /** |
||
348 | * Alias for Crawler::html() for naming consistency with setInnerHtml() |
||
349 | * |
||
350 | * @return string |
||
351 | * @api |
||
352 | */ |
||
353 | 1 | public function getInnerHtml() |
|
357 | |||
358 | /** |
||
359 | * Insert every element in the set of matched elements after the target. |
||
360 | * |
||
361 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
362 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
363 | * @api |
||
364 | */ |
||
365 | 2 | public function insertAfter($element) |
|
385 | |||
386 | /** |
||
387 | * Insert every element in the set of matched elements before the target. |
||
388 | * |
||
389 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
390 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
391 | * @api |
||
392 | */ |
||
393 | 2 | public function insertBefore($element) |
|
410 | |||
411 | /** |
||
412 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
413 | * |
||
414 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
415 | * @return HtmlPageCrawler $this for chaining |
||
416 | * @api |
||
417 | */ |
||
418 | 2 | public function prepend($content) |
|
440 | |||
441 | /** |
||
442 | * Insert every element in the set of matched elements to the beginning of the target. |
||
443 | * |
||
444 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
445 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
446 | * @api |
||
447 | */ |
||
448 | 1 | public function prependTo($element) |
|
470 | |||
471 | /** |
||
472 | * Remove the set of matched elements from the DOM. |
||
473 | * |
||
474 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
475 | * but leaves them in the DOM) |
||
476 | * |
||
477 | * @api |
||
478 | */ |
||
479 | 2 | public function remove() |
|
491 | |||
492 | /** |
||
493 | * Remove an attribute from each element in the set of matched elements. |
||
494 | * |
||
495 | * Alias for removeAttribute for compatibility with jQuery |
||
496 | * |
||
497 | * @param string $name |
||
498 | * @return HtmlPageCrawler |
||
499 | * @api |
||
500 | */ |
||
501 | 1 | public function removeAttr($name) |
|
505 | |||
506 | /** |
||
507 | * Remove an attribute from each element in the set of matched elements. |
||
508 | * |
||
509 | * @param string $name |
||
510 | * @return HtmlPageCrawler |
||
511 | */ |
||
512 | 1 | public function removeAttribute($name) |
|
524 | |||
525 | /** |
||
526 | * Remove a class from each element in the list |
||
527 | * |
||
528 | * @param string $name |
||
529 | * @return HtmlPageCrawler $this for chaining |
||
530 | * @api |
||
531 | */ |
||
532 | 2 | public function removeClass($name) |
|
549 | |||
550 | /** |
||
551 | * Replace each target element with the set of matched elements. |
||
552 | * |
||
553 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
554 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
555 | * @api |
||
556 | */ |
||
557 | 2 | public function replaceAll($element) |
|
578 | |||
579 | /** |
||
580 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
581 | * |
||
582 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
583 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
584 | * @api |
||
585 | */ |
||
586 | 2 | public function replaceWith($content) |
|
609 | |||
610 | /** |
||
611 | * Get the combined text contents of each element in the set of matched elements, including their descendants. |
||
612 | * This is what the jQuery text() function does, contrary to the Crawler::text() method that returns only |
||
613 | * the text of the first node. |
||
614 | * |
||
615 | * @return string |
||
616 | * @api |
||
617 | */ |
||
618 | 1 | public function getCombinedText() |
|
627 | |||
628 | /** |
||
629 | * Set the text contents of the matched elements. |
||
630 | * |
||
631 | * @param string $text |
||
632 | * @return HtmlPageCrawler |
||
633 | * @api |
||
634 | */ |
||
635 | 2 | public function setText($text) |
|
644 | |||
645 | /** |
||
646 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
647 | * |
||
648 | * @param string $classname One or more classnames separated by spaces |
||
649 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
650 | * @api |
||
651 | */ |
||
652 | 1 | public function toggleClass($classname) |
|
668 | |||
669 | /** |
||
670 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
671 | * |
||
672 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
673 | * @api |
||
674 | */ |
||
675 | 1 | public function unwrap() |
|
685 | |||
686 | /** |
||
687 | * Remove the matched elements, but promote the children to take their place. |
||
688 | * |
||
689 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
690 | * @api |
||
691 | */ |
||
692 | 2 | public function unwrapInner() |
|
708 | |||
709 | |||
710 | /** |
||
711 | * Wrap an HTML structure around each element in the set of matched elements |
||
712 | * |
||
713 | * The HTML structure must contain only one root node, e.g.: |
||
714 | * Works: <div><div></div></div> |
||
715 | * Does not work: <div></div><div></div> |
||
716 | * |
||
717 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
718 | * @return HtmlPageCrawler $this for chaining |
||
719 | * @api |
||
720 | */ |
||
721 | 1 | public function wrap($wrappingElement) |
|
758 | |||
759 | /** |
||
760 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
761 | * |
||
762 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
763 | * @throws \LogicException |
||
764 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
765 | * @api |
||
766 | */ |
||
767 | 1 | public function wrapAll($content) |
|
768 | { |
||
769 | 1 | $content = self::create($content); |
|
770 | 1 | $parent = $this->getNode(0)->parentNode; |
|
771 | 1 | foreach ($this as $i => $node) { |
|
772 | /** @var \DOMNode $node */ |
||
773 | 1 | if ($node->parentNode !== $parent) { |
|
774 | 1 | throw new \LogicException('Nodes to be wrapped with wrapAll() must all have the same parent'); |
|
775 | } |
||
776 | } |
||
777 | |||
778 | 1 | $newnode = $content->getNode(0); |
|
779 | /** @var \DOMNode $newnode */ |
||
780 | 1 | $newnode = static::importNewnode($newnode, $parent); |
|
781 | |||
782 | 1 | $newnode = $parent->insertBefore($newnode,$this->getNode(0)); |
|
783 | 1 | $content->clear(); |
|
784 | 1 | $content->add($newnode); |
|
785 | |||
786 | 1 | while ($newnode->hasChildNodes()) { |
|
787 | 1 | $elementFound = false; |
|
788 | 1 | foreach ($newnode->childNodes as $child) { |
|
789 | 1 | if ($child instanceof \DOMElement) { |
|
790 | 1 | $newnode = $child; |
|
791 | 1 | $elementFound = true; |
|
792 | 1 | break; |
|
793 | } |
||
794 | } |
||
795 | 1 | if (!$elementFound) { |
|
796 | break; |
||
797 | } |
||
798 | } |
||
799 | 1 | foreach ($this as $i => $node) { |
|
800 | /** @var \DOMNode $node */ |
||
801 | 1 | $newnode->appendChild($node); |
|
802 | } |
||
803 | 1 | return $this; |
|
804 | } |
||
805 | |||
806 | /** |
||
807 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
808 | * |
||
809 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
810 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
811 | * @api |
||
812 | */ |
||
813 | 1 | public function wrapInner($content) |
|
821 | |||
822 | /** |
||
823 | * Get the HTML code fragment of all elements and their contents. |
||
824 | * |
||
825 | * If the first node contains a complete HTML document return only |
||
826 | * the full code of this document. |
||
827 | * |
||
828 | * @return string HTML code (fragment) |
||
829 | * @api |
||
830 | */ |
||
831 | 8 | public function saveHTML() |
|
845 | |||
846 | 4 | public function __toString() |
|
850 | |||
851 | /** |
||
852 | * checks whether the first node contains a complete html document |
||
853 | * (as opposed to a document fragment) |
||
854 | * |
||
855 | * @return boolean |
||
856 | */ |
||
857 | 8 | public function isHtmlDocument() |
|
870 | |||
871 | /** |
||
872 | * get ownerDocument of the first element |
||
873 | * |
||
874 | * @return \DOMDocument|null |
||
875 | */ |
||
876 | 1 | public function getDOMDocument() |
|
887 | |||
888 | /** |
||
889 | * Filters the list of nodes with a CSS selector. |
||
890 | * |
||
891 | * @param string $selector |
||
892 | * @return HtmlPageCrawler |
||
893 | */ |
||
894 | 7 | public function filter($selector) |
|
898 | |||
899 | /** |
||
900 | * Filters the list of nodes with an XPath expression. |
||
901 | * |
||
902 | * @param string $xpath An XPath expression |
||
903 | * |
||
904 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
905 | * |
||
906 | * @api |
||
907 | */ |
||
908 | 2 | public function filterXPath($xpath) |
|
912 | |||
913 | /** |
||
914 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
915 | * |
||
916 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
917 | * |
||
918 | * |
||
919 | * @param string $content A string to parse as HTML/XML |
||
920 | * @param null|string $type The content type of the string |
||
921 | * |
||
922 | * @return null|void |
||
923 | */ |
||
924 | 17 | public function addContent($content, $type = null) |
|
936 | |||
937 | 15 | public function addHtmlFragment($content, $charset = 'UTF-8') |
|
950 | |||
951 | /** |
||
952 | * Adds a node to the current list of nodes. |
||
953 | * |
||
954 | * This method uses the appropriate specialized add*() method based |
||
955 | * on the type of the argument. |
||
956 | * |
||
957 | * Overwritten from parent to allow Crawler to be added |
||
958 | * |
||
959 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
960 | * |
||
961 | * @api |
||
962 | */ |
||
963 | 29 | public function add($node) |
|
973 | |||
974 | /** |
||
975 | * @param \DOMNode $newnode |
||
976 | * @param \DOMNode $referencenode |
||
977 | * @param int $clone |
||
978 | * @return \DOMNode |
||
979 | */ |
||
980 | 6 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
991 | |||
992 | // /** |
||
993 | // * Checks whether the first node in the set is disconnected (has no parent node) |
||
994 | // * |
||
995 | // * @return bool |
||
996 | // */ |
||
997 | // public function isDisconnected() |
||
998 | // { |
||
999 | // $parent = $this->getNode(0)->parentNode; |
||
1000 | // return ($parent == null || $parent->tagName == self::FRAGMENT_ROOT_TAGNAME); |
||
1001 | // } |
||
1002 | |||
1003 | 1 | public function __get($name) |
|
1012 | } |
||
1013 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.