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 | 'area', |
||
83 | 'base', |
||
84 | 'basefont', |
||
85 | 'br', |
||
86 | 'col', |
||
87 | 'embed', |
||
88 | 'hr', |
||
89 | 'img', |
||
90 | 'input', |
||
91 | 'keygen', |
||
92 | 'link', |
||
93 | 'meta', |
||
94 | 'param', |
||
95 | 'source', |
||
96 | 'spacer', |
||
97 | 'track', |
||
98 | 'wbr' |
||
99 | ]; |
||
100 | |||
101 | /** |
||
102 | * A list of tags where there should be no /> at the end (html5 style) |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | protected $noSlash = []; |
||
107 | |||
108 | /** |
||
109 | * Returns the inner html of the root node. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 24 | public function __toString(): string |
|
117 | |||
118 | /** |
||
119 | * A simple wrapper around the root node. |
||
120 | * |
||
121 | * @param string $name |
||
122 | * @return mixed |
||
123 | */ |
||
124 | 9 | public function __get($name) |
|
128 | |||
129 | /** |
||
130 | * Attempts to load the dom from any resource, string, file, or URL. |
||
131 | * |
||
132 | * @param string $str |
||
133 | * @param array $options |
||
134 | * @return Dom |
||
135 | * @chainable |
||
136 | */ |
||
137 | 144 | public function load(string $str, array $options = []): Dom |
|
151 | |||
152 | /** |
||
153 | * Loads the dom from a document file/url |
||
154 | * |
||
155 | * @param string $file |
||
156 | * @param array $options |
||
157 | * @return Dom |
||
158 | * @chainable |
||
159 | */ |
||
160 | 48 | public function loadFromFile(string $file, array $options = []): Dom |
|
164 | |||
165 | /** |
||
166 | * Use a curl interface implementation to attempt to load |
||
167 | * the content from a url. |
||
168 | * |
||
169 | * @param string $url |
||
170 | * @param array $options |
||
171 | * @param CurlInterface $curl |
||
172 | * @return Dom |
||
173 | * @chainable |
||
174 | */ |
||
175 | 6 | public function loadFromUrl(string $url, array $options = [], CurlInterface $curl = null): Dom |
|
185 | |||
186 | /** |
||
187 | * Parsers the html of the given string. Used for load(), loadFromFile(), |
||
188 | * and loadFromUrl(). |
||
189 | * |
||
190 | * @param string $str |
||
191 | * @param array $option |
||
192 | * @return Dom |
||
193 | * @chainable |
||
194 | */ |
||
195 | 192 | public function loadStr(string $str, array $option): Dom |
|
214 | |||
215 | /** |
||
216 | * Sets a global options array to be used by all load calls. |
||
217 | * |
||
218 | * @param array $options |
||
219 | * @return Dom |
||
220 | * @chainable |
||
221 | */ |
||
222 | 39 | public function setOptions(array $options): Dom |
|
228 | |||
229 | /** |
||
230 | * Find elements by css selector on the root node. |
||
231 | * |
||
232 | * @param string $selector |
||
233 | * @param int $nth |
||
234 | * @return mixed |
||
235 | */ |
||
236 | 144 | public function find(string $selector, int $nth = null) |
|
242 | |||
243 | /** |
||
244 | * Find element by Id on the root node |
||
245 | * |
||
246 | * @param int $id |
||
247 | * @return mixed |
||
248 | */ |
||
249 | 9 | public function findById(int $id) |
|
255 | |||
256 | /** |
||
257 | * Adds the tag (or tags in an array) to the list of tags that will always |
||
258 | * be self closing. |
||
259 | * |
||
260 | * @param string|array $tag |
||
261 | * @return Dom |
||
262 | * @chainable |
||
263 | */ |
||
264 | 6 | public function addSelfClosingTag($tag): Dom |
|
275 | |||
276 | /** |
||
277 | * Removes the tag (or tags in an array) from the list of tags that will |
||
278 | * always be self closing. |
||
279 | * |
||
280 | * @param string|array $tag |
||
281 | * @return Dom |
||
282 | * @chainable |
||
283 | */ |
||
284 | 3 | public function removeSelfClosingTag($tag): Dom |
|
293 | |||
294 | /** |
||
295 | * Sets the list of self closing tags to empty. |
||
296 | * |
||
297 | * @return Dom |
||
298 | * @chainable |
||
299 | */ |
||
300 | 3 | public function clearSelfClosingTags(): Dom |
|
306 | |||
307 | |||
308 | /** |
||
309 | * Adds a tag to the list of self closing tags that should not have a trailing slash |
||
310 | * |
||
311 | * @param $tag |
||
312 | * @return Dom |
||
313 | * @chainable |
||
314 | */ |
||
315 | 3 | public function addNoSlashTag($tag): Dom |
|
326 | |||
327 | /** |
||
328 | * Removes a tag from the list of no-slash tags. |
||
329 | * |
||
330 | * @param $tag |
||
331 | * @return Dom |
||
332 | * @chainable |
||
333 | */ |
||
334 | public function removeNoSlashTag($tag): Dom |
||
343 | |||
344 | /** |
||
345 | * Empties the list of no-slash tags. |
||
346 | * |
||
347 | * @return Dom |
||
348 | * @chainable |
||
349 | */ |
||
350 | public function clearNoSlashTags(): Dom |
||
356 | |||
357 | /** |
||
358 | * Simple wrapper function that returns the first child. |
||
359 | * |
||
360 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
361 | */ |
||
362 | 3 | public function firstChild(): \PHPHtmlParser\Dom\AbstractNode |
|
368 | |||
369 | /** |
||
370 | * Simple wrapper function that returns the last child. |
||
371 | * |
||
372 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
373 | */ |
||
374 | 3 | public function lastChild(): \PHPHtmlParser\Dom\AbstractNode |
|
380 | |||
381 | /** |
||
382 | * Simple wrapper function that returns count of child elements |
||
383 | * |
||
384 | * @return int |
||
385 | */ |
||
386 | 3 | public function countChildren(): int |
|
392 | |||
393 | /** |
||
394 | * Get array of children |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | 3 | public function getChildren(): array |
|
404 | |||
405 | /** |
||
406 | * Check if node have children nodes |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | 3 | public function hasChildren(): bool |
|
416 | |||
417 | /** |
||
418 | * Simple wrapper function that returns an element by the |
||
419 | * id. |
||
420 | * |
||
421 | * @param string $id |
||
422 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
423 | */ |
||
424 | 12 | public function getElementById($id): \PHPHtmlParser\Dom\AbstractNode |
|
430 | |||
431 | /** |
||
432 | * Simple wrapper function that returns all elements by |
||
433 | * tag name. |
||
434 | * |
||
435 | * @param string $name |
||
436 | * @return mixed |
||
437 | */ |
||
438 | 12 | public function getElementsByTag(string $name) |
|
444 | |||
445 | /** |
||
446 | * Simple wrapper function that returns all elements by |
||
447 | * class name. |
||
448 | * |
||
449 | * @param string $class |
||
450 | * @return mixed |
||
451 | */ |
||
452 | 3 | public function getElementsByClass(string $class) |
|
458 | |||
459 | /** |
||
460 | * Checks if the load methods have been called. |
||
461 | * |
||
462 | * @throws NotLoadedException |
||
463 | */ |
||
464 | 168 | protected function isLoaded(): void |
|
470 | |||
471 | /** |
||
472 | * Cleans the html of any none-html information. |
||
473 | * |
||
474 | * @param string $str |
||
475 | * @return string |
||
476 | */ |
||
477 | 192 | protected function clean(string $str): string |
|
526 | |||
527 | /** |
||
528 | * Attempts to parse the html in content. |
||
529 | */ |
||
530 | 192 | protected function parse(): void |
|
583 | |||
584 | /** |
||
585 | * Attempt to parse a tag out of the content. |
||
586 | * |
||
587 | * @return array |
||
588 | * @throws StrictException |
||
589 | */ |
||
590 | 192 | protected function parseTag(): array |
|
732 | |||
733 | /** |
||
734 | * Attempts to detect the charset that the html was sent in. |
||
735 | * |
||
736 | * @return bool |
||
737 | */ |
||
738 | 186 | protected function detectCharset(): bool |
|
780 | } |
||
781 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.