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 = []) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Loads the dom from a document file/url |
||
| 137 | * |
||
| 138 | * @param string $file |
||
| 139 | * @param array $options |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function loadFromFile($file, $options = []) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Use a curl interface implementation to attempt to load |
||
| 149 | * the content from a url. |
||
| 150 | * |
||
| 151 | * @param string $url |
||
| 152 | * @param array $options |
||
| 153 | * @param CurlInterface $curl |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function loadFromUrl($url, $options = [], CurlInterface $curl = null) |
||
| 157 | { |
||
| 158 | if (is_null($curl)) { |
||
| 159 | // use the default curl interface |
||
| 160 | $curl = new Curl; |
||
| 161 | } |
||
| 162 | $content = $curl->get($url); |
||
| 163 | |||
| 164 | return $this->loadStr($content, $options); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Parsers the html of the given string. Used for load(), loadFromFile(), |
||
| 169 | * and loadFromUrl(). |
||
| 170 | * |
||
| 171 | * @param string $str |
||
| 172 | * @param array $option |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function loadStr($str, $option) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets a global options array to be used by all load calls. |
||
| 197 | * |
||
| 198 | * @param array $options |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setOptions(array $options) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Find elements by css selector on the root node. |
||
| 210 | * |
||
| 211 | * @param string $selector |
||
| 212 | * @param int $nth |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function find($selector, $nth = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Find element by Id on the root node |
||
| 224 | * |
||
| 225 | * @param int $id Element Id |
||
| 226 | * @return mixed |
||
| 227 | * |
||
| 228 | */ |
||
| 229 | public function findById($id) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Adds the tag (or tags in an array) to the list of tags that will always |
||
| 238 | * be self closing. |
||
| 239 | * |
||
| 240 | * @param string|array $tag |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function addSelfClosingTag($tag) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Removes the tag (or tags in an array) from the list of tags that will |
||
| 257 | * always be self closing. |
||
| 258 | * |
||
| 259 | * @param string|array $tag |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function removeSelfClosingTag($tag) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Sets the list of self closing tags to empty. |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function clearSelfClosingTags() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Simple wrapper function that returns the first child. |
||
| 286 | * |
||
| 287 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 288 | */ |
||
| 289 | public function firstChild() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Simple wrapper function that returns the last child. |
||
| 298 | * |
||
| 299 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 300 | */ |
||
| 301 | public function lastChild() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Simple wrapper function that returns count of child elements |
||
| 310 | * |
||
| 311 | * @return int |
||
| 312 | */ |
||
| 313 | public function countChildren() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get array of children |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | public function getChildren() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Check if node have children nodes |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function hasChildren() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Simple wrapper function that returns an element by the |
||
| 346 | * id. |
||
| 347 | * |
||
| 348 | * @param string $id |
||
| 349 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 350 | */ |
||
| 351 | public function getElementById($id) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Simple wrapper function that returns all elements by |
||
| 360 | * tag name. |
||
| 361 | * |
||
| 362 | * @param string $name |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | public function getElementsByTag($name) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Simple wrapper function that returns all elements by |
||
| 374 | * class name. |
||
| 375 | * |
||
| 376 | * @param string $class |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function getElementsByClass($class) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Checks if the load methods have been called. |
||
| 388 | * |
||
| 389 | * @throws NotLoadedException |
||
| 390 | */ |
||
| 391 | protected function isLoaded() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Cleans the html of any none-html information. |
||
| 400 | * |
||
| 401 | * @param string $str |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | protected function clean($str) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Attempts to parse the html in content. |
||
| 456 | */ |
||
| 457 | protected function parse() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Attempt to parse a tag out of the content. |
||
| 513 | * |
||
| 514 | * @return array |
||
| 515 | * @throws StrictException |
||
| 516 | */ |
||
| 517 | protected function parseTag() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Attempts to detect the charset that the html was sent in. |
||
| 655 | * |
||
| 656 | * @return bool |
||
| 657 | */ |
||
| 658 | protected function detectCharset() |
||
| 700 | } |
||
| 701 |