Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SimpleHtmlDom 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 SimpleHtmlDom, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class SimpleHtmlDom implements \IteratorAggregate |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected static $functionAliases = array( |
||
| 35 | 'children' => 'childNodes', |
||
| 36 | 'first_child' => 'firstChild', |
||
| 37 | 'last_child' => 'lastChild', |
||
| 38 | 'next_sibling' => 'nextSibling', |
||
| 39 | 'prev_sibling' => 'previousSibling', |
||
| 40 | 'parent' => 'parentNode', |
||
| 41 | 'outertext' => 'html', |
||
| 42 | 'innertext' => 'innerHtml', |
||
| 43 | ); |
||
| 44 | /** |
||
| 45 | * @var DOMElement |
||
| 46 | */ |
||
| 47 | protected $node; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * SimpleHtmlDom constructor. |
||
| 51 | * |
||
| 52 | * @param DOMNode $node |
||
| 53 | */ |
||
| 54 | 68 | public function __construct(DOMNode $node) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @param $name |
||
| 61 | * @param $arguments |
||
| 62 | * |
||
| 63 | * @return null|string|SimpleHtmlDom |
||
| 64 | * |
||
| 65 | */ |
||
| 66 | 8 | View Code Duplication | public function __call($name, $arguments) |
| 73 | |||
| 74 | /** |
||
| 75 | * @param $name |
||
| 76 | * |
||
| 77 | * @return array|null|string |
||
| 78 | */ |
||
| 79 | 23 | public function __get($name) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $selector |
||
| 99 | * @param int $idx |
||
| 100 | * |
||
| 101 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
| 102 | */ |
||
| 103 | 12 | public function __invoke($selector, $idx = null) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @param $name |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | 1 | public function __isset($name) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param $name |
||
| 128 | * @param $value |
||
| 129 | * |
||
| 130 | * @return SimpleHtmlDom |
||
| 131 | */ |
||
| 132 | 8 | public function __set($name, $value) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | 1 | public function __toString() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param $name |
||
| 154 | * |
||
| 155 | * @return SimpleHtmlDom |
||
| 156 | */ |
||
| 157 | 1 | public function __unset($name) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Returns children of node |
||
| 164 | * |
||
| 165 | * @param int $idx |
||
| 166 | * |
||
| 167 | * @return SimpleHtmlDomNode|SimpleHtmlDom|null |
||
| 168 | */ |
||
| 169 | 2 | public function childNodes($idx = -1) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Find list of nodes with a CSS selector |
||
| 186 | * |
||
| 187 | * @param string $selector |
||
| 188 | * @param int $idx |
||
| 189 | * |
||
| 190 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 191 | */ |
||
| 192 | 26 | public function find($selector, $idx = null) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the first child of node |
||
| 199 | * |
||
| 200 | * @return SimpleHtmlDom|null |
||
| 201 | */ |
||
| 202 | 4 | public function firstChild() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Returns array of attributes |
||
| 215 | * |
||
| 216 | * @return array|null |
||
| 217 | */ |
||
| 218 | 1 | public function getAllAttributes() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Return attribute value |
||
| 234 | * |
||
| 235 | * @param string $name |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | 9 | public function getAttribute($name) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Return SimpleHtmlDom by id. |
||
| 246 | * |
||
| 247 | * @param string $id |
||
| 248 | * |
||
| 249 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 250 | */ |
||
| 251 | 1 | public function getElementById($id) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Return SimpleHtmlDom by tag name. |
||
| 258 | * |
||
| 259 | * @param string $name |
||
| 260 | * |
||
| 261 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 262 | */ |
||
| 263 | 1 | public function getElementByTagName($name) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Returns Elements by id |
||
| 270 | * |
||
| 271 | * @param string $id |
||
| 272 | * @param null|int $idx |
||
| 273 | * |
||
| 274 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 275 | */ |
||
| 276 | public function getElementsById($id, $idx = null) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns Elements by tag name |
||
| 283 | * |
||
| 284 | * @param string $name |
||
| 285 | * @param null|int $idx |
||
| 286 | * |
||
| 287 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
| 288 | */ |
||
| 289 | 1 | public function getElementsByTagName($name, $idx = null) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Create a new "HtmlDomParser"-object from the current context. |
||
| 296 | * |
||
| 297 | * @return HtmlDomParser |
||
| 298 | */ |
||
| 299 | 41 | public function getHtmlDomParser() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Retrieve an external iterator |
||
| 306 | * |
||
| 307 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 308 | * @return SimpleHtmlDomNode An instance of an object implementing <b>Iterator</b> or |
||
| 309 | * <b>Traversable</b> |
||
| 310 | */ |
||
| 311 | 2 | public function getIterator() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @return DOMNode |
||
| 325 | */ |
||
| 326 | 42 | public function getNode() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Determine if an attribute exists on the element. |
||
| 333 | * |
||
| 334 | * @param $name |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | 1 | public function hasAttribute($name) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Get dom node's outer html |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | 12 | public function html() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get dom node's inner html |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | 3 | public function innerHtml() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the last child of node |
||
| 365 | * |
||
| 366 | * @return SimpleHtmlDom|null |
||
| 367 | */ |
||
| 368 | 4 | public function lastChild() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Returns the next sibling of node |
||
| 381 | * |
||
| 382 | * @return SimpleHtmlDom|null |
||
| 383 | */ |
||
| 384 | 1 | public function nextSibling() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Returns the parent of node |
||
| 397 | * |
||
| 398 | * @return SimpleHtmlDom |
||
| 399 | */ |
||
| 400 | 1 | public function parentNode() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Returns the previous sibling of node |
||
| 407 | * |
||
| 408 | * @return SimpleHtmlDom|null |
||
| 409 | */ |
||
| 410 | 1 | public function previousSibling() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Replace child node |
||
| 423 | * |
||
| 424 | * @param $string |
||
| 425 | * |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | 3 | protected function replaceChild($string) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Replace this node |
||
| 456 | * |
||
| 457 | * @param $string |
||
| 458 | * |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | 2 | protected function replaceNode($string) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @param HtmlDomParser $newDocument |
||
| 487 | * |
||
| 488 | * @return HtmlDomParser |
||
| 489 | */ |
||
| 490 | 5 | protected function cleanHtmlWrapper(HtmlDomParser $newDocument) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * change the name of a tag in a "DOMNode" |
||
| 523 | * |
||
| 524 | * @param DOMNode $node |
||
| 525 | * @param string $name |
||
| 526 | * |
||
| 527 | * @return DOMElement |
||
| 528 | */ |
||
| 529 | 3 | protected function changeElementName(\DOMNode $node, $name) { |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Set attribute value |
||
| 545 | * |
||
| 546 | * @param $name |
||
| 547 | * @param $value |
||
| 548 | * |
||
| 549 | * @return $this |
||
| 550 | */ |
||
| 551 | 5 | public function setAttribute($name, $value) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Get dom node's plain text |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | 9 | public function text() |
|
| 571 | } |
||
| 572 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.