Complex classes like Dom 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 Dom, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Dom |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * The charset we would like the output to be in. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $defaultCharset = 'UTF-8'; |
||
25 | |||
26 | /** |
||
27 | * Contains the root node of this dom tree. |
||
28 | * |
||
29 | * @var HtmlNode |
||
30 | */ |
||
31 | public $root; |
||
32 | |||
33 | /** |
||
34 | * The raw version of the document string. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $raw; |
||
39 | |||
40 | /** |
||
41 | * The document string. |
||
42 | * |
||
43 | * @var Content |
||
44 | */ |
||
45 | protected $content = null; |
||
46 | |||
47 | /** |
||
48 | * The original file size of the document. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $rawSize; |
||
53 | |||
54 | /** |
||
55 | * The size of the document after it is cleaned. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $size; |
||
60 | |||
61 | /** |
||
62 | * A global options array to be used by all load calls. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $globalOptions = []; |
||
67 | |||
68 | /** |
||
69 | * A persistent option object to be used for all options in the |
||
70 | * parsing of the file. |
||
71 | * |
||
72 | * @var Options |
||
73 | */ |
||
74 | protected $options; |
||
75 | |||
76 | /** |
||
77 | * A list of tags which will always be self closing |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $selfClosing = [ |
||
82 | 'img', |
||
83 | 'br', |
||
84 | 'input', |
||
85 | 'meta', |
||
86 | 'link', |
||
87 | 'hr', |
||
88 | 'base', |
||
89 | 'embed', |
||
90 | 'spacer', |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * Returns the inner html of the root node. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function __toString() |
||
102 | |||
103 | /** |
||
104 | * A simple wrapper around the root node. |
||
105 | * |
||
106 | * @param string $name |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function __get($name) |
||
113 | |||
114 | /** |
||
115 | * Attempts to load the dom from any resource, string, file, or URL. |
||
116 | * |
||
117 | * @param string $str |
||
118 | * @param array $options |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function load($str, $options = []) |
||
135 | |||
136 | /** |
||
137 | * Loads the dom from a document file/url |
||
138 | * |
||
139 | * @param string $file |
||
140 | * @param array $options |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function loadFromFile($file, $options = []) |
||
147 | |||
148 | /** |
||
149 | * Use a curl interface implementation to attempt to load |
||
150 | * the content from a url. |
||
151 | * |
||
152 | * @param string $url |
||
153 | * @param array $options |
||
154 | * @param CurlInterface $curl |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function loadFromUrl($url, $options = [], CurlInterface $curl = null) |
||
167 | |||
168 | /** |
||
169 | * Parsers the html of the given string. Used for load(), loadFromFile(), |
||
170 | * and loadFromUrl(). |
||
171 | * |
||
172 | * @param string $str |
||
173 | * @param array $option |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function loadStr($str, $option) |
||
195 | |||
196 | /** |
||
197 | * Sets a global options array to be used by all load calls. |
||
198 | * |
||
199 | * @param array $options |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setOptions(array $options) |
||
208 | |||
209 | /** |
||
210 | * Find elements by css selector on the root node. |
||
211 | * |
||
212 | * @param string $selector |
||
213 | * @param int $nth |
||
214 | * @return array |
||
215 | */ |
||
216 | public function find($selector, $nth = null) |
||
222 | |||
223 | /** |
||
224 | * Find element by Id on the root node |
||
225 | * |
||
226 | * @param int $id Element Id |
||
227 | * @return mixed |
||
228 | * |
||
229 | */ |
||
230 | public function findById($id) |
||
236 | |||
237 | /** |
||
238 | * Adds the tag (or tags in an array) to the list of tags that will always |
||
239 | * be self closing. |
||
240 | * |
||
241 | * @param string|array $tag |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function addSelfClosingTag($tag) |
||
255 | |||
256 | /** |
||
257 | * Removes the tag (or tags in an array) from the list of tags that will |
||
258 | * always be self closing. |
||
259 | * |
||
260 | * @param string|array $tag |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function removeSelfClosingTag($tag) |
||
272 | |||
273 | /** |
||
274 | * Sets the list of self closing tags to empty. |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function clearSelfClosingTags() |
||
284 | |||
285 | /** |
||
286 | * Simple wrapper function that returns the first child. |
||
287 | * |
||
288 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
289 | */ |
||
290 | public function firstChild() |
||
296 | |||
297 | /** |
||
298 | * Simple wrapper function that returns the last child. |
||
299 | * |
||
300 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
301 | */ |
||
302 | public function lastChild() |
||
308 | |||
309 | /** |
||
310 | * Simple wrapper function that returns count of child elements |
||
311 | * |
||
312 | * @return int |
||
313 | */ |
||
314 | public function countChildren() |
||
320 | |||
321 | /** |
||
322 | * Get array of children |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | public function getChildren() |
||
332 | |||
333 | /** |
||
334 | * Check if node have children nodes |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | public function hasChildren() |
||
344 | |||
345 | /** |
||
346 | * Simple wrapper function that returns an element by the |
||
347 | * id. |
||
348 | * |
||
349 | * @param string $id |
||
350 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
351 | */ |
||
352 | public function getElementById($id) |
||
358 | |||
359 | /** |
||
360 | * Simple wrapper function that returns all elements by |
||
361 | * tag name. |
||
362 | * |
||
363 | * @param string $name |
||
364 | * @return array |
||
365 | */ |
||
366 | public function getElementsByTag($name) |
||
372 | |||
373 | /** |
||
374 | * Simple wrapper function that returns all elements by |
||
375 | * class name. |
||
376 | * |
||
377 | * @param string $class |
||
378 | * @return array |
||
379 | */ |
||
380 | public function getElementsByClass($class) |
||
386 | |||
387 | /** |
||
388 | * Checks if the load methods have been called. |
||
389 | * |
||
390 | * @throws NotLoadedException |
||
391 | */ |
||
392 | protected function isLoaded() |
||
398 | |||
399 | /** |
||
400 | * Cleans the html of any none-html information. |
||
401 | * |
||
402 | * @param string $str |
||
403 | * @return string |
||
404 | */ |
||
405 | protected function clean($str) |
||
454 | |||
455 | /** |
||
456 | * Attempts to parse the html in content. |
||
457 | */ |
||
458 | protected function parse() |
||
511 | |||
512 | /** |
||
513 | * Attempt to parse a tag out of the content. |
||
514 | * |
||
515 | * @return array |
||
516 | * @throws StrictException |
||
517 | */ |
||
518 | protected function parseTag() |
||
653 | |||
654 | /** |
||
655 | * Attempts to detect the charset that the html was sent in. |
||
656 | * |
||
657 | * @return bool |
||
658 | */ |
||
659 | protected function detectCharset() |
||
701 | } |
||
702 |