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 | public function __toString() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * A simple wrapper around the root node. |
||
| 120 | * |
||
| 121 | * @param string $name |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | 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 $this |
||
| 135 | */ |
||
| 136 | public function load($str, $options = []) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Loads the dom from a document file/url |
||
| 153 | * |
||
| 154 | * @param string $file |
||
| 155 | * @param array $options |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function loadFromFile($file, $options = []) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Use a curl interface implementation to attempt to load |
||
| 165 | * the content from a url. |
||
| 166 | * |
||
| 167 | * @param string $url |
||
| 168 | * @param array $options |
||
| 169 | * @param CurlInterface $curl |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function loadFromUrl($url, $options = [], CurlInterface $curl = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Parsers the html of the given string. Used for load(), loadFromFile(), |
||
| 185 | * and loadFromUrl(). |
||
| 186 | * |
||
| 187 | * @param string $str |
||
| 188 | * @param array $option |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function loadStr($str, $option) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets a global options array to be used by all load calls. |
||
| 213 | * |
||
| 214 | * @param array $options |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function setOptions(array $options) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Find elements by css selector on the root node. |
||
| 226 | * |
||
| 227 | * @param string $selector |
||
| 228 | * @param int $nth |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function find($selector, $nth = null) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Find element by Id on the root node |
||
| 240 | * |
||
| 241 | * @param int $id Element Id |
||
| 242 | * @return mixed |
||
| 243 | * |
||
| 244 | */ |
||
| 245 | public function findById($id) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Adds the tag (or tags in an array) to the list of tags that will always |
||
| 254 | * be self closing. |
||
| 255 | * |
||
| 256 | * @param string|array $tag |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function addSelfClosingTag($tag) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Removes the tag (or tags in an array) from the list of tags that will |
||
| 273 | * always be self closing. |
||
| 274 | * |
||
| 275 | * @param string|array $tag |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function removeSelfClosingTag($tag) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Sets the list of self closing tags to empty. |
||
| 290 | * |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function clearSelfClosingTags() |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Adds a tag to the list of self closing tags that should not have a trailing slash |
||
| 303 | * |
||
| 304 | * @param $tag |
||
| 305 | * @return $this |
||
| 306 | */ |
||
| 307 | public function addNoSlashTag($tag) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Removes a tag from the list of no-slash tags. |
||
| 321 | * |
||
| 322 | * @param $tag |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | public function removeNoSlashTag($tag) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Empties the list of no-slash tags. |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function clearNoSlashTags() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Simple wrapper function that returns the first child. |
||
| 349 | * |
||
| 350 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 351 | */ |
||
| 352 | public function firstChild() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Simple wrapper function that returns the last child. |
||
| 361 | * |
||
| 362 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 363 | */ |
||
| 364 | public function lastChild() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Simple wrapper function that returns count of child elements |
||
| 373 | * |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | public function countChildren() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get array of children |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | public function getChildren() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Check if node have children nodes |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | public function hasChildren() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Simple wrapper function that returns an element by the |
||
| 409 | * id. |
||
| 410 | * |
||
| 411 | * @param string $id |
||
| 412 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 413 | */ |
||
| 414 | public function getElementById($id) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Simple wrapper function that returns all elements by |
||
| 423 | * tag name. |
||
| 424 | * |
||
| 425 | * @param string $name |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public function getElementsByTag($name) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Simple wrapper function that returns all elements by |
||
| 437 | * class name. |
||
| 438 | * |
||
| 439 | * @param string $class |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public function getElementsByClass($class) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Checks if the load methods have been called. |
||
| 451 | * |
||
| 452 | * @throws NotLoadedException |
||
| 453 | */ |
||
| 454 | protected function isLoaded() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Cleans the html of any none-html information. |
||
| 463 | * |
||
| 464 | * @param string $str |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | protected function clean($str) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Attempts to parse the html in content. |
||
| 519 | */ |
||
| 520 | protected function parse() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Attempt to parse a tag out of the content. |
||
| 576 | * |
||
| 577 | * @return array |
||
| 578 | * @throws StrictException |
||
| 579 | */ |
||
| 580 | protected function parseTag() |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Attempts to detect the charset that the html was sent in. |
||
| 725 | * |
||
| 726 | * @return bool |
||
| 727 | */ |
||
| 728 | protected function detectCharset() |
||
| 770 | } |
||
| 771 |