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 HtmlDomParser 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 HtmlDomParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class HtmlDomParser |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected static $functionAliases = array( |
||
34 | 'outertext' => 'html', |
||
35 | 'innertext' => 'innerHtml', |
||
36 | 'load' => 'loadHtml', |
||
37 | 'load_file' => 'loadHtmlFile', |
||
38 | ); |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private static $domLinkReplaceHelper = array( |
||
44 | 'orig' => array('[', ']', '{', '}',), |
||
45 | 'tmp' => array( |
||
46 | '!!!!HTML_DOM__SQUARE_BRACKET_LEFT!!!!', |
||
47 | '!!!!HTML_DOM__SQUARE_BRACKET_RIGHT!!!!', |
||
48 | '!!!!HTML_DOM__BRACKET_LEFT!!!!', |
||
49 | '!!!!HTML_DOM__BRACKET_RIGHT!!!!', |
||
50 | ), |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected static $domReplaceHelper = array( |
||
57 | 'orig' => array('&', '|'), |
||
58 | 'tmp' => array('!!!!HTML_DOM__AMP!!!!', '!!!!HTML_DOM__PIPE!!!!'), |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * @var Callable |
||
63 | */ |
||
64 | protected static $callback; |
||
65 | |||
66 | /** |
||
67 | * @var DOMDocument |
||
68 | */ |
||
69 | protected $document; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $encoding = 'UTF-8'; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $isDOMDocumentCreatedWithoutHtml = false; |
||
80 | |||
81 | /** |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected $isDOMDocumentCreatedWithoutHtmlWrapper = false; |
||
85 | |||
86 | /** |
||
87 | * Constructor |
||
88 | * |
||
89 | * @param string|SimpleHtmlDom|\DOMNode $element HTML code or SimpleHtmlDom, \DOMNode |
||
90 | */ |
||
91 | 85 | public function __construct($element = null) |
|
117 | |||
118 | /** |
||
119 | * @param $name |
||
120 | * @param $arguments |
||
121 | * |
||
122 | * @return bool|mixed |
||
123 | */ |
||
124 | 6 | View Code Duplication | public function __call($name, $arguments) |
132 | |||
133 | /** |
||
134 | * @param $name |
||
135 | * @param $arguments |
||
136 | * |
||
137 | * @return HtmlDomParser |
||
138 | */ |
||
139 | 11 | public static function __callStatic($name, $arguments) |
|
155 | |||
156 | /** |
||
157 | * @param $name |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 10 | View Code Duplication | public function __get($name) |
174 | |||
175 | /** |
||
176 | * @param string $selector |
||
177 | * @param int $idx |
||
178 | * |
||
179 | * @return SimpleHtmlDom|SimpleHtmlDomNode|null |
||
180 | */ |
||
181 | 2 | public function __invoke($selector, $idx = null) |
|
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | 8 | public function __toString() |
|
193 | |||
194 | /** |
||
195 | * does nothing (only for api-compatibility-reasons) |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 1 | public function clear() |
|
203 | |||
204 | /** |
||
205 | * @param string $html |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | 49 | private function replaceToPreserveHtmlEntities($html) |
|
236 | |||
237 | /** |
||
238 | * @param string $html |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | 22 | private function putReplacedBackToPreserveHtmlEntities($html) |
|
258 | |||
259 | /** |
||
260 | * create DOMDocument from HTML |
||
261 | * |
||
262 | * @param string $html |
||
263 | * |
||
264 | * @return \DOMDocument |
||
265 | */ |
||
266 | 74 | private function createDOMDocument($html) |
|
267 | { |
||
268 | 74 | if (strpos($html, '<') === false) { |
|
269 | 3 | $this->isDOMDocumentCreatedWithoutHtml = true; |
|
270 | 3 | } |
|
271 | |||
272 | 74 | if (strpos($html, '<html') === false) { |
|
273 | 29 | $this->isDOMDocumentCreatedWithoutHtmlWrapper = true; |
|
274 | 29 | } |
|
275 | |||
276 | // set error level |
||
277 | 74 | $internalErrors = libxml_use_internal_errors(true); |
|
278 | 74 | $disableEntityLoader = libxml_disable_entity_loader(true); |
|
279 | 74 | libxml_clear_errors(); |
|
280 | |||
281 | 74 | $options = LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_NONET; |
|
282 | 74 | if (defined(LIBXML_COMPACT)) { |
|
283 | $options |= LIBXML_COMPACT; |
||
284 | } |
||
285 | |||
286 | 74 | $sxe = simplexml_load_string($html, 'SimpleXMLElement', $options); |
|
287 | 74 | if ($sxe !== false && count(libxml_get_errors()) === 0) { |
|
288 | 27 | $this->document = dom_import_simplexml($sxe)->ownerDocument; |
|
289 | 27 | } else { |
|
290 | |||
291 | // UTF-8 hack: http://php.net/manual/en/domdocument.loadhtml.php#95251 |
||
292 | 49 | $html = trim($html); |
|
293 | 49 | $xmlHackUsed = false; |
|
294 | 49 | if (stripos('<?xml', $html) !== 0) { |
|
295 | 49 | $xmlHackUsed = true; |
|
296 | 49 | $html = '<?xml encoding="' . $this->getEncoding() . '" ?>' . $html; |
|
297 | 49 | } |
|
298 | |||
299 | 49 | $html = $this->replaceToPreserveHtmlEntities($html); |
|
300 | |||
301 | 49 | $this->document->loadHTML($html); |
|
302 | |||
303 | // remove the "xml-encoding" hack |
||
304 | 49 | if ($xmlHackUsed === true) { |
|
305 | 49 | foreach ($this->document->childNodes as $child) { |
|
306 | 49 | if ($child->nodeType == XML_PI_NODE) { |
|
307 | 49 | $this->document->removeChild($child); |
|
308 | 49 | } |
|
309 | 49 | } |
|
310 | 49 | } |
|
311 | |||
312 | 49 | libxml_clear_errors(); |
|
313 | } |
||
314 | |||
315 | // set encoding |
||
316 | 74 | $this->document->encoding = $this->getEncoding(); |
|
317 | |||
318 | // restore lib-xml settings |
||
319 | 74 | libxml_use_internal_errors($internalErrors); |
|
320 | 74 | libxml_disable_entity_loader($disableEntityLoader); |
|
321 | |||
322 | 74 | return $this->document; |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * Return SimpleHtmlDom by id. |
||
327 | * |
||
328 | * @param string $id |
||
329 | * |
||
330 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
331 | */ |
||
332 | 1 | public function getElementById($id) |
|
336 | |||
337 | /** |
||
338 | * Return SimpleHtmlDom by tag name. |
||
339 | * |
||
340 | * @param string $name |
||
341 | * |
||
342 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
343 | */ |
||
344 | 1 | public function getElementByTagName($name) |
|
345 | { |
||
346 | 1 | $node = $this->document->getElementsByTagName($name)->item(0); |
|
347 | |||
348 | 1 | if ($node !== null) { |
|
349 | 1 | return new SimpleHtmlDom($node); |
|
350 | } else { |
||
351 | return new SimpleHtmlDomNodeBlank(); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Returns Elements by id |
||
357 | * |
||
358 | * @param string $id |
||
359 | * @param null|int $idx |
||
360 | * |
||
361 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
362 | */ |
||
363 | public function getElementsById($id, $idx = null) |
||
364 | { |
||
365 | return $this->find("#$id", $idx); |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Returns Elements by tag name |
||
370 | * |
||
371 | * @param string $name |
||
372 | * @param null|int $idx |
||
373 | * |
||
374 | * @return SimpleHtmlDomNode|SimpleHtmlDomNode[]|SimpleHtmlDomNodeBlank |
||
375 | */ |
||
376 | 1 | View Code Duplication | public function getElementsByTagName($name, $idx = null) |
377 | { |
||
378 | 1 | $nodesList = $this->document->getElementsByTagName($name); |
|
379 | |||
380 | 1 | $elements = new SimpleHtmlDomNode(); |
|
381 | |||
382 | 1 | foreach ($nodesList as $node) { |
|
383 | 1 | $elements[] = new SimpleHtmlDom($node); |
|
384 | 1 | } |
|
385 | |||
386 | 1 | if (null === $idx) { |
|
387 | return $elements; |
||
388 | } else { |
||
389 | 1 | if ($idx < 0) { |
|
390 | $idx = count($elements) + $idx; |
||
391 | } |
||
392 | } |
||
393 | |||
394 | 1 | if (isset($elements[$idx])) { |
|
395 | 1 | return $elements[$idx]; |
|
396 | } else { |
||
397 | return new SimpleHtmlDomNodeBlank(); |
||
398 | } |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Find list of nodes with a CSS selector. |
||
403 | * |
||
404 | * @param string $selector |
||
405 | * @param int $idx |
||
406 | * |
||
407 | * @return SimpleHtmlDom|SimpleHtmlDom[] |
||
408 | */ |
||
409 | 53 | public function find($selector, $idx = null) |
|
410 | { |
||
411 | 53 | $xPathQuery = SelectorConverter::toXPath($selector); |
|
412 | |||
413 | 53 | $xPath = new DOMXPath($this->document); |
|
414 | 53 | $nodesList = $xPath->query($xPathQuery); |
|
415 | 53 | $elements = new SimpleHtmlDomNode(); |
|
416 | |||
417 | 53 | foreach ($nodesList as $node) { |
|
418 | 51 | $elements[] = new SimpleHtmlDom($node); |
|
419 | 53 | } |
|
420 | |||
421 | 53 | if (null === $idx) { |
|
422 | 45 | return $elements; |
|
423 | } else { |
||
424 | 20 | if ($idx < 0) { |
|
425 | 11 | $idx = count($elements) + $idx; |
|
426 | 11 | } |
|
427 | } |
||
428 | |||
429 | 20 | if (isset($elements[$idx])) { |
|
430 | 20 | return $elements[$idx]; |
|
431 | } else { |
||
432 | return new SimpleHtmlDomNodeBlank(); |
||
433 | } |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @param string $content |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | 22 | protected function fixHtmlOutput($content) |
|
442 | { |
||
443 | // INFO: DOMDocument will encapsulate plaintext into a paragraph tag (<p>), |
||
444 | // so we try to remove it here again ... |
||
445 | |||
446 | 22 | if ($this->isDOMDocumentCreatedWithoutHtmlWrapper === true) { |
|
447 | 8 | $content = str_replace( |
|
448 | array( |
||
449 | 8 | "\n", |
|
450 | 8 | "\r\n", |
|
451 | 8 | "\r", |
|
452 | 8 | '<simpleHtmlDomP>', |
|
453 | 8 | '</simpleHtmlDomP>', |
|
454 | 8 | '<body>', |
|
455 | 8 | '</body>', |
|
456 | 8 | '<html>', |
|
457 | 8 | '</html>', |
|
458 | 8 | ), |
|
459 | 8 | '', |
|
460 | $content |
||
461 | 8 | ); |
|
462 | 8 | } |
|
463 | |||
464 | 22 | if ($this->isDOMDocumentCreatedWithoutHtml === true) { |
|
465 | 3 | $content = str_replace(array('<p>', '</p>'), '', $content); |
|
466 | 3 | } |
|
467 | |||
468 | 22 | $content = UTF8::html_entity_decode($content); |
|
469 | 22 | $content = trim($content); |
|
470 | 22 | $content = UTF8::urldecode($content); |
|
471 | |||
472 | 22 | $content = $this->putReplacedBackToPreserveHtmlEntities($content); |
|
473 | |||
474 | 22 | return $content; |
|
475 | } |
||
476 | |||
477 | /** |
||
478 | * @return DOMDocument |
||
479 | */ |
||
480 | 35 | public function getDocument() |
|
484 | |||
485 | /** |
||
486 | * Get the encoding to use |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | 85 | private function getEncoding() |
|
494 | |||
495 | /** |
||
496 | * @return bool |
||
497 | */ |
||
498 | 6 | public function getIsDOMDocumentCreatedWithoutHtml() |
|
502 | |||
503 | /** |
||
504 | * @return bool |
||
505 | */ |
||
506 | 19 | public function getIsDOMDocumentCreatedWithoutHtmlWrapper() |
|
510 | |||
511 | /** |
||
512 | * Get dom node's outer html |
||
513 | * |
||
514 | * @return string |
||
515 | */ |
||
516 | 19 | public function html() |
|
517 | { |
||
518 | 19 | if ($this::$callback !== null) { |
|
519 | call_user_func_array($this::$callback, array($this)); |
||
520 | } |
||
521 | |||
522 | 19 | if ($this->getIsDOMDocumentCreatedWithoutHtmlWrapper()) { |
|
523 | 7 | $content = $this->document->saveHTML($this->document->documentElement); |
|
524 | 7 | } else { |
|
525 | 16 | $content = $this->document->saveHTML(); |
|
526 | } |
||
527 | |||
528 | 19 | return $this->fixHtmlOutput($content); |
|
529 | } |
||
530 | |||
531 | /** |
||
532 | * Get the HTML as XML. |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | 1 | public function xml() |
|
545 | |||
546 | /** |
||
547 | * Get dom node's inner html |
||
548 | * |
||
549 | * @return string |
||
550 | */ |
||
551 | 5 | public function innerHtml() |
|
552 | { |
||
553 | 5 | $text = ''; |
|
554 | |||
555 | 5 | foreach ($this->document->documentElement->childNodes as $node) { |
|
556 | 5 | $text .= $this->fixHtmlOutput($this->document->saveHTML($node)); |
|
557 | 5 | } |
|
558 | |||
559 | 5 | return $text; |
|
560 | } |
||
561 | |||
562 | /** |
||
563 | * Load HTML from string |
||
564 | * |
||
565 | * @param string $html |
||
566 | * |
||
567 | * @return HtmlDomParser |
||
568 | * |
||
569 | * @throws InvalidArgumentException if argument is not string |
||
570 | */ |
||
571 | 77 | public function loadHtml($html) |
|
581 | |||
582 | /** |
||
583 | * Load HTML from file |
||
584 | * |
||
585 | * @param string $filePath |
||
586 | * |
||
587 | * @return HtmlDomParser |
||
588 | */ |
||
589 | 7 | public function loadHtmlFile($filePath) |
|
590 | { |
||
591 | 7 | if (!is_string($filePath)) { |
|
592 | 2 | throw new InvalidArgumentException(__METHOD__ . ' expects parameter 1 to be string.'); |
|
593 | } |
||
594 | |||
595 | 5 | if (!preg_match("/^https?:\/\//i", $filePath) && !file_exists($filePath)) { |
|
596 | 1 | throw new RuntimeException("File $filePath not found"); |
|
597 | } |
||
598 | |||
599 | try { |
||
600 | 4 | $html = file_get_contents($filePath); |
|
601 | |||
602 | 4 | } catch (\Exception $e) { |
|
603 | 1 | throw new RuntimeException("Could not load file $filePath"); |
|
604 | } |
||
605 | |||
606 | 3 | if ($html === false) { |
|
607 | throw new RuntimeException("Could not load file $filePath"); |
||
608 | } |
||
609 | |||
610 | 3 | $this->loadHtml($html); |
|
611 | |||
612 | 3 | return $this; |
|
613 | } |
||
614 | |||
615 | /** |
||
616 | * Save dom as string |
||
617 | * |
||
618 | * @param string $filepath |
||
619 | * |
||
620 | * @return string |
||
621 | */ |
||
622 | 1 | public function save($filepath = '') |
|
623 | { |
||
624 | 1 | $string = $this->innerHtml(); |
|
625 | 1 | if ($filepath !== '') { |
|
626 | file_put_contents($filepath, $string, LOCK_EX); |
||
627 | } |
||
628 | |||
629 | 1 | return $string; |
|
630 | } |
||
631 | |||
632 | /** |
||
633 | * @param $functionName |
||
634 | */ |
||
635 | public function set_callback($functionName) |
||
636 | { |
||
637 | $this::$callback = $functionName; |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Get dom node's plain text |
||
642 | * |
||
643 | * @return string |
||
644 | */ |
||
645 | 1 | public function text() |
|
649 | } |
||
650 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.