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 | * A list of tags where there should be no /> at the end (html5 style) |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $noSlash = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the inner html of the root node. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function __toString() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * A simple wrapper around the root node. |
||
| 112 | * |
||
| 113 | * @param string $name |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | public function __get($name) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Attempts to load the dom from any resource, string, file, or URL. |
||
| 123 | * |
||
| 124 | * @param string $str |
||
| 125 | * @param array $options |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function load($str, $options = []) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Loads the dom from a document file/url |
||
| 144 | * |
||
| 145 | * @param string $file |
||
| 146 | * @param array $options |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function loadFromFile($file, $options = []) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Use a curl interface implementation to attempt to load |
||
| 156 | * the content from a url. |
||
| 157 | * |
||
| 158 | * @param string $url |
||
| 159 | * @param array $options |
||
| 160 | * @param CurlInterface $curl |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function loadFromUrl($url, $options = [], CurlInterface $curl = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Parsers the html of the given string. Used for load(), loadFromFile(), |
||
| 176 | * and loadFromUrl(). |
||
| 177 | * |
||
| 178 | * @param string $str |
||
| 179 | * @param array $option |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function loadStr($str, $option) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Sets a global options array to be used by all load calls. |
||
| 204 | * |
||
| 205 | * @param array $options |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setOptions(array $options) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Find elements by css selector on the root node. |
||
| 217 | * |
||
| 218 | * @param string $selector |
||
| 219 | * @param int $nth |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | public function find($selector, $nth = null) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Adds the tag (or tags in an array) to the list of tags that will always |
||
| 231 | * be self closing. |
||
| 232 | * |
||
| 233 | * @param string|array $tag |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function addSelfClosingTag($tag) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Removes the tag (or tags in an array) from the list of tags that will |
||
| 250 | * always be self closing. |
||
| 251 | * |
||
| 252 | * @param string|array $tag |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function removeSelfClosingTag($tag) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Sets the list of self closing tags to empty. |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function clearSelfClosingTags() |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Adds a tag to the list of self closing tags that should not have a trailing slash |
||
| 280 | * |
||
| 281 | * @param $tag |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function addNoSlashTag($tag) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Removes a tag from the list of no-slash tags. |
||
| 298 | * |
||
| 299 | * @param $tag |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | public function removeNoSlashTag($tag) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Empties the list of no-slash tags. |
||
| 314 | * |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function clearNoSlashTags() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Simple wrapper function that returns the first child. |
||
| 326 | * |
||
| 327 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 328 | */ |
||
| 329 | public function firstChild() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Simple wrapper function that returns the last child. |
||
| 338 | * |
||
| 339 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 340 | */ |
||
| 341 | public function lastChild() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Simple wrapper function that returns an element by the |
||
| 350 | * id. |
||
| 351 | * |
||
| 352 | * @param string $id |
||
| 353 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 354 | */ |
||
| 355 | public function getElementById($id) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Simple wrapper function that returns all elements by |
||
| 364 | * tag name. |
||
| 365 | * |
||
| 366 | * @param string $name |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | public function getElementsByTag($name) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Simple wrapper function that returns all elements by |
||
| 378 | * class name. |
||
| 379 | * |
||
| 380 | * @param string $class |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function getElementsByClass($class) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Checks if the load methods have been called. |
||
| 392 | * |
||
| 393 | * @throws NotLoadedException |
||
| 394 | */ |
||
| 395 | protected function isLoaded() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Cleans the html of any none-html information. |
||
| 404 | * |
||
| 405 | * @param string $str |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | protected function clean($str) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Attempts to parse the html in content. |
||
| 458 | */ |
||
| 459 | protected function parse() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Attempt to parse a tag out of the content. |
||
| 515 | * |
||
| 516 | * @return array |
||
| 517 | * @throws StrictException |
||
| 518 | */ |
||
| 519 | protected function parseTag() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Attempts to detect the charset that the html was sent in. |
||
| 664 | * |
||
| 665 | * @return bool |
||
| 666 | */ |
||
| 667 | protected function detectCharset() |
||
| 709 | } |
||
| 710 |