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 | * Attention: Changed behavior since 2.0: |
||
26 | * - If no content is passed, an empty HTML skeleton document will be created. |
||
27 | * - It is no longer possible to create Crawler objects on an HTML fragment this way. Use createFragment() instead. |
||
28 | * |
||
29 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|array|null $content |
||
30 | * @return HtmlPageCrawler |
||
31 | * @api |
||
32 | */ |
||
33 | 3 | public static function create($content = null) |
|
34 | { |
||
35 | 3 | if ($content instanceof HtmlPageCrawler) { |
|
36 | return $content; |
||
37 | } else { |
||
38 | 3 | if ($content === null) { |
|
39 | 1 | $content = '<html><body></body></html>'; |
|
40 | } |
||
41 | 3 | return new HtmlPageCrawler($content); |
|
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Adds the specified class(es) to each element in the set of matched elements. |
||
47 | * |
||
48 | * @param string $name One or more space-separated classes to be added to the class attribute of each matched element. |
||
49 | * @return HtmlPageCrawler $this for chaining |
||
50 | * @api |
||
51 | */ |
||
52 | 1 | public function addClass($name) |
|
53 | { |
||
54 | 1 | foreach ($this as $node) { |
|
55 | 1 | if ($node instanceof \DOMElement) { |
|
56 | /** @var \DOMElement $node */ |
||
57 | 1 | $classes = preg_split('/\s+/s', $node->getAttribute('class')); |
|
58 | 1 | $found = false; |
|
59 | 1 | $count = count($classes); |
|
60 | 1 | for ($i = 0; $i < $count; $i++) { |
|
61 | 1 | if ($classes[$i] == $name) { |
|
62 | 1 | $found = true; |
|
63 | } |
||
64 | } |
||
65 | 1 | if (!$found) { |
|
66 | 1 | $classes[] = $name; |
|
67 | 1 | $node->setAttribute('class', trim(join(' ', $classes))); |
|
68 | } |
||
69 | } |
||
70 | } |
||
71 | 1 | return $this; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * Insert content, specified by the parameter, after each element in the set of matched elements. |
||
76 | * |
||
77 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
78 | * @return HtmlPageCrawler $this for chaining |
||
79 | * @api |
||
80 | */ |
||
81 | 1 | public function after($content) |
|
82 | { |
||
83 | 1 | $content = self::createFragmentCrawler($content); |
|
84 | 1 | $newnodes = array(); |
|
85 | 1 | foreach ($this as $i => $node) { |
|
86 | /** @var \DOMNode $node */ |
||
87 | 1 | $refnode = $node->nextSibling; |
|
88 | 1 | foreach ($content as $newnode) { |
|
89 | /** @var \DOMNode $newnode */ |
||
90 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
91 | 1 | if ($refnode === null) { |
|
92 | 1 | $node->parentNode->appendChild($newnode); |
|
93 | } else { |
||
94 | $node->parentNode->insertBefore($newnode, $refnode); |
||
95 | } |
||
96 | 1 | $newnodes[] = $newnode; |
|
97 | } |
||
98 | } |
||
99 | 1 | $content->clear(); |
|
100 | 1 | $content->add($newnodes); |
|
101 | 1 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Insert HTML content as child nodes of each element after existing children |
||
106 | * |
||
107 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment or DOMNode to append |
||
108 | * @return HtmlPageCrawler $this for chaining |
||
109 | * @api |
||
110 | */ |
||
111 | 2 | public function append($content) |
|
112 | { |
||
113 | 2 | $content = self::createFragmentCrawler($content); |
|
114 | 2 | $newnodes = array(); |
|
115 | 2 | foreach ($this as $i => $node) { |
|
116 | /** @var \DOMNode $node */ |
||
117 | 2 | foreach ($content as $newnode) { |
|
118 | /** @var \DOMNode $newnode */ |
||
119 | 2 | $newnode = static::importNewnode($newnode, $node, $i); |
|
120 | 2 | $node->appendChild($newnode); |
|
121 | 2 | $newnodes[] = $newnode; |
|
122 | } |
||
123 | } |
||
124 | 2 | $content->clear(); |
|
125 | 2 | $content->add($newnodes); |
|
126 | 2 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Insert every element in the set of matched elements to the end of the target. |
||
131 | * |
||
132 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
133 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
134 | * @api |
||
135 | */ |
||
136 | 1 | public function appendTo($element) |
|
137 | { |
||
138 | 1 | $e = self::createFragmentCrawler($element); |
|
139 | 1 | $newnodes = array(); |
|
140 | 1 | foreach ($e as $i => $node) { |
|
141 | /** @var \DOMNode $node */ |
||
142 | 1 | foreach ($this as $newnode) { |
|
143 | /** @var \DOMNode $newnode */ |
||
144 | 1 | if ($node !== $newnode) { |
|
145 | 1 | $newnode = static::importNewnode($newnode, $node, $i); |
|
146 | 1 | $node->appendChild($newnode); |
|
147 | } |
||
148 | 1 | $newnodes[] = $newnode; |
|
149 | } |
||
150 | } |
||
151 | 1 | return self::createFragmentCrawler($newnodes); |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns the attribute value of the first node of the list, or sets an attribute on each element |
||
156 | * |
||
157 | * @see HtmlPageCrawler::getAttribute() |
||
158 | * @see HtmlPageCrawler::setAttribute |
||
159 | * |
||
160 | * @param string $name |
||
161 | * @param null|string $value |
||
162 | * @return null|string|HtmlPageCrawler |
||
163 | * @api |
||
164 | */ |
||
165 | 1 | public function attr($name, $value = null) |
|
166 | { |
||
167 | 1 | if ($value === null) { |
|
168 | 1 | return $this->getAttribute($name); |
|
169 | } else { |
||
170 | return $this->setAttribute($name, $value); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Sets an attribute on each element |
||
176 | * |
||
177 | * @param string $name |
||
178 | * @param string $value |
||
179 | * @return HtmlPageCrawler $this for chaining |
||
180 | */ |
||
181 | 2 | public function setAttribute($name, $value) |
|
191 | |||
192 | /** |
||
193 | * Returns the attribute value of the first node of the list. |
||
194 | * |
||
195 | * @param string $name The attribute name |
||
196 | * @return string|null The attribute value or null if the attribute does not exist |
||
197 | * @throws \InvalidArgumentException When current node is empty |
||
198 | * |
||
199 | */ |
||
200 | 1 | public function getAttribute($name) |
|
208 | |||
209 | /** |
||
210 | * Insert content, specified by the parameter, before each element in the set of matched elements. |
||
211 | * |
||
212 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
213 | * @return HtmlPageCrawler $this for chaining |
||
214 | * @api |
||
215 | */ |
||
216 | 1 | public function before($content) |
|
235 | |||
236 | /** |
||
237 | * Create a deep copy of the set of matched elements. |
||
238 | * |
||
239 | * Equivalent to clone() in jQuery (clone is not a valid PHP function name) |
||
240 | * |
||
241 | * @return HtmlPageCrawler |
||
242 | * @api |
||
243 | */ |
||
244 | public function makeClone() |
||
248 | |||
249 | public function __clone() |
||
259 | |||
260 | /** |
||
261 | * Get one CSS style property of the first element or set it for all elements in the list |
||
262 | * |
||
263 | * Function is here for compatibility with jQuery; it is the same as getStyle() and setStyle() |
||
264 | * |
||
265 | * @see HtmlPageCrawler::getStyle() |
||
266 | * @see HtmlPageCrawler::setStyle() |
||
267 | * |
||
268 | * @param string $key The name of the style property |
||
269 | * @param null|string $value The CSS value to set, or NULL to get the current value |
||
270 | * @return HtmlPageCrawler|string If no param is provided, returns the CSS styles of the first element |
||
271 | * @api |
||
272 | */ |
||
273 | 1 | public function css($key, $value = null) |
|
281 | |||
282 | /** |
||
283 | * get one CSS style property of the first element |
||
284 | * |
||
285 | * @param string $key name of the property |
||
286 | * @return string|null value of the property |
||
287 | */ |
||
288 | 1 | public function getStyle($key) |
|
293 | |||
294 | /** |
||
295 | * set one CSS style property for all elements in the list |
||
296 | * |
||
297 | * @param string $key name of the property |
||
298 | * @param string $value value of the property |
||
299 | * @return HtmlPageCrawler $this for chaining |
||
300 | */ |
||
301 | 1 | public function setStyle($key, $value) |
|
317 | |||
318 | /** |
||
319 | * Removes all child nodes and text from all nodes in set |
||
320 | * |
||
321 | * Equivalent to jQuery's empty() function which is not a valid function name in PHP |
||
322 | * @return HtmlPageCrawler $this |
||
323 | * @api |
||
324 | */ |
||
325 | 1 | public function makeEmpty() |
|
332 | |||
333 | /** |
||
334 | * Determine whether any of the matched elements are assigned the given class. |
||
335 | * |
||
336 | * @param string $name |
||
337 | * @return bool |
||
338 | * @api |
||
339 | */ |
||
340 | 1 | public function hasClass($name) |
|
352 | |||
353 | /** |
||
354 | * Set the HTML contents of each element |
||
355 | * |
||
356 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
357 | * @return HtmlPageCrawler $this for chaining |
||
358 | */ |
||
359 | 2 | public function setInnerHtml($content) |
|
373 | |||
374 | /** |
||
375 | * Insert every element in the set of matched elements after the target. |
||
376 | * |
||
377 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
378 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
379 | * @api |
||
380 | */ |
||
381 | 1 | public function insertAfter($element) |
|
401 | |||
402 | /** |
||
403 | * Insert every element in the set of matched elements before the target. |
||
404 | * |
||
405 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
406 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
407 | * @api |
||
408 | */ |
||
409 | 1 | public function insertBefore($element) |
|
426 | |||
427 | /** |
||
428 | * Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
||
429 | * |
||
430 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
||
431 | * @return HtmlPageCrawler $this for chaining |
||
432 | * @api |
||
433 | */ |
||
434 | 2 | public function prepend($content) |
|
456 | |||
457 | /** |
||
458 | * Insert every element in the set of matched elements to the beginning of the target. |
||
459 | * |
||
460 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
461 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
||
462 | * @api |
||
463 | */ |
||
464 | 1 | public function prependTo($element) |
|
486 | |||
487 | /** |
||
488 | * Remove the set of matched elements from the DOM. |
||
489 | * |
||
490 | * (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
||
491 | * but leaves them in the DOM) |
||
492 | * |
||
493 | * @api |
||
494 | */ |
||
495 | 1 | public function remove() |
|
507 | |||
508 | /** |
||
509 | * Remove an attribute from each element in the set of matched elements. |
||
510 | * |
||
511 | * Alias for removeAttribute for compatibility with jQuery |
||
512 | * |
||
513 | * @param string $name |
||
514 | * @return HtmlPageCrawler |
||
515 | * @api |
||
516 | */ |
||
517 | public function removeAttr($name) |
||
521 | |||
522 | /** |
||
523 | * Remove an attribute from each element in the set of matched elements. |
||
524 | * |
||
525 | * @param string $name |
||
526 | * @return HtmlPageCrawler |
||
527 | */ |
||
528 | public function removeAttribute($name) |
||
540 | |||
541 | /** |
||
542 | * Remove a class from each element in the list |
||
543 | * |
||
544 | * @param string $name |
||
545 | * @return HtmlPageCrawler $this for chaining |
||
546 | * @api |
||
547 | */ |
||
548 | 1 | public function removeClass($name) |
|
565 | |||
566 | /** |
||
567 | * Replace each target element with the set of matched elements. |
||
568 | * |
||
569 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
||
570 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
||
571 | * @api |
||
572 | */ |
||
573 | public function replaceAll($element) |
||
594 | |||
595 | /** |
||
596 | * Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
||
597 | * |
||
598 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
599 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
600 | * @api |
||
601 | */ |
||
602 | public function replaceWith($content) |
||
625 | |||
626 | /** |
||
627 | * Get the combined text contents of each element in the set of matched elements, including their descendants. |
||
628 | * This is what the jQuery text() function does, contrary to the Crawler::text() method that returns only |
||
629 | * the text of the first node. |
||
630 | * |
||
631 | * @return string |
||
632 | * @api |
||
633 | */ |
||
634 | public function getCombinedText() |
||
643 | |||
644 | /** |
||
645 | * Set the text contents of the matched elements. |
||
646 | * |
||
647 | * @param string $text |
||
648 | * @return HtmlPageCrawler |
||
649 | * @api |
||
650 | */ |
||
651 | public function setText($text) |
||
660 | |||
661 | /** |
||
662 | * Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
||
663 | * |
||
664 | * @param string $classname One or more classnames separated by spaces |
||
665 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
666 | * @api |
||
667 | */ |
||
668 | public function toggleClass($classname) |
||
684 | |||
685 | /** |
||
686 | * Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
||
687 | * |
||
688 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
689 | * @api |
||
690 | */ |
||
691 | public function unwrap() |
||
701 | |||
702 | /** |
||
703 | * Remove the matched elements, but promote the children to take their place. |
||
704 | * |
||
705 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
706 | * @api |
||
707 | */ |
||
708 | public function unwrapInner() |
||
724 | |||
725 | |||
726 | /** |
||
727 | * Wrap an HTML structure around each element in the set of matched elements |
||
728 | * |
||
729 | * The HTML structure must contain only one root node, e.g.: |
||
730 | * Works: <div><div></div></div> |
||
731 | * Does not work: <div></div><div></div> |
||
732 | * |
||
733 | * @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
||
734 | * @return HtmlPageCrawler $this for chaining |
||
735 | * @api |
||
736 | */ |
||
737 | 1 | public function wrap($wrappingElement) |
|
774 | |||
775 | /** |
||
776 | * Wrap an HTML structure around all elements in the set of matched elements. |
||
777 | * |
||
778 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
779 | * @throws \LogicException |
||
780 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
781 | * @api |
||
782 | */ |
||
783 | public function wrapAll($content) |
||
821 | |||
822 | /** |
||
823 | * Wrap an HTML structure around the content of each element in the set of matched elements. |
||
824 | * |
||
825 | * @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
||
826 | * @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
||
827 | * @api |
||
828 | */ |
||
829 | public function wrapInner($content) |
||
837 | |||
838 | /** |
||
839 | * Get the HTML code fragment of all elements and their contents. |
||
840 | * |
||
841 | * If the first node contains a complete HTML document return only |
||
842 | * the full code of this document. |
||
843 | * |
||
844 | * @return string HTML code (fragment) |
||
845 | * @api |
||
846 | */ |
||
847 | 5 | public function saveHTML() |
|
861 | |||
862 | 2 | public function __toString() |
|
866 | |||
867 | /** |
||
868 | * checks whether the first node contains a complete html document |
||
869 | * (as opposed to a document fragment) |
||
870 | * |
||
871 | * @return boolean |
||
872 | */ |
||
873 | 5 | public function isHtmlDocument() |
|
886 | |||
887 | /** |
||
888 | * get ownerDocument of the first element |
||
889 | * |
||
890 | * @return \DOMDocument|null |
||
891 | */ |
||
892 | 2 | public function getDOMDocument() |
|
903 | |||
904 | /** |
||
905 | * Filters the list of nodes with a CSS selector. |
||
906 | * |
||
907 | * @param string $selector |
||
908 | * @return HtmlPageCrawler |
||
909 | */ |
||
910 | 4 | public function filter($selector) |
|
914 | |||
915 | /** |
||
916 | * Filters the list of nodes with an XPath expression. |
||
917 | * |
||
918 | * @param string $xpath An XPath expression |
||
919 | * |
||
920 | * @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
||
921 | * |
||
922 | * @api |
||
923 | */ |
||
924 | 2 | public function filterXPath($xpath) |
|
928 | |||
929 | /** |
||
930 | * Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
||
931 | * |
||
932 | * Function overriden from Crawler because HTML fragments are always added as complete documents there |
||
933 | * |
||
934 | * |
||
935 | * @param string $content A string to parse as HTML/XML |
||
936 | * @param null|string $type The content type of the string |
||
937 | * |
||
938 | * @return null|void |
||
939 | */ |
||
940 | 4 | public function addContent($content, $type = null) |
|
957 | |||
958 | // public function addHtmlFragment($content, $charset = 'UTF-8') |
||
959 | // { |
||
960 | // $d = new \DOMDocument('1.0', $charset); |
||
961 | // $d->preserveWhiteSpace = false; |
||
962 | // $root = $d->appendChild($d->createElement(self::FRAGMENT_ROOT_TAGNAME)); |
||
963 | // $bodynode = Helpers::getBodyNodeFromHtmlFragment($content, $charset); |
||
964 | // foreach ($bodynode->childNodes as $child) { |
||
965 | // $inode = $root->appendChild($d->importNode($child, true)); |
||
966 | // if ($inode) { |
||
967 | // $this->addNode($inode); |
||
968 | // } |
||
969 | // } |
||
970 | // } |
||
971 | |||
972 | /** |
||
973 | * @param string $content |
||
974 | * @param string $charset |
||
975 | * @return HtmlPageCrawler |
||
976 | * @throws \Exception |
||
977 | */ |
||
978 | 2 | public function createFragment($content, $charset = 'UTF-8') |
|
994 | |||
995 | 2 | protected function createFragmentCrawler($content) |
|
1019 | |||
1020 | /** |
||
1021 | * Adds a node to the current list of nodes. |
||
1022 | * |
||
1023 | * This method uses the appropriate specialized add*() method based |
||
1024 | * on the type of the argument. |
||
1025 | * |
||
1026 | * Overwritten from parent to allow Crawler to be added |
||
1027 | * |
||
1028 | * @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
||
1029 | * |
||
1030 | * @api |
||
1031 | */ |
||
1032 | 17 | public function add($node) |
|
1042 | |||
1043 | /** |
||
1044 | * @param \DOMNode $newnode |
||
1045 | * @param \DOMNode $referencenode |
||
1046 | * @param int $clone |
||
1047 | * @return \DOMNode |
||
1048 | */ |
||
1049 | 2 | protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
|
1060 | |||
1061 | /** |
||
1062 | * Checks whether the first node in the set is disconnected (has no parent node) |
||
1063 | * |
||
1064 | * @return bool |
||
1065 | */ |
||
1066 | public function isDisconnected() |
||
1071 | |||
1072 | public function __get($name) |
||
1081 | } |
||
1082 |
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.