Code Duplication    Length = 27-30 lines in 3 locations

src/voku/helper/XmlDomParser.php 2 locations

@@ 434-460 (lines=27) @@
431
     *
432
     * @return XmlDomParser
433
     */
434
    public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface
435
    {
436
        if (
437
            !\preg_match("/^https?:\/\//i", $filePath)
438
            &&
439
            !\file_exists($filePath)
440
        ) {
441
            throw new \RuntimeException("File ${filePath} not found");
442
        }
443
444
        try {
445
            if (\class_exists('\voku\helper\UTF8')) {
446
                /** @noinspection PhpUndefinedClassInspection */
447
                $html = UTF8::file_get_contents($filePath);
448
            } else {
449
                $html = \file_get_contents($filePath);
450
            }
451
        } catch (\Exception $e) {
452
            throw new \RuntimeException("Could not load file ${filePath}");
453
        }
454
455
        if ($html === false) {
456
            throw new \RuntimeException("Could not load file ${filePath}");
457
        }
458
459
        return $this->loadHtml($html, $libXMLExtraOptions);
460
    }
461
462
    /**
463
     * @param string $selector
@@ 498-524 (lines=27) @@
495
     *
496
     * @return XmlDomParser
497
     */
498
    public function loadXmlFile(string $filePath, $libXMLExtraOptions = null): self
499
    {
500
        if (
501
            !\preg_match("/^https?:\/\//i", $filePath)
502
            &&
503
            !\file_exists($filePath)
504
        ) {
505
            throw new \RuntimeException("File ${filePath} not found");
506
        }
507
508
        try {
509
            if (\class_exists('\voku\helper\UTF8')) {
510
                /** @noinspection PhpUndefinedClassInspection */
511
                $xml = UTF8::file_get_contents($filePath);
512
            } else {
513
                $xml = \file_get_contents($filePath);
514
            }
515
        } catch (\Exception $e) {
516
            throw new \RuntimeException("Could not load file ${filePath}");
517
        }
518
519
        if ($xml === false) {
520
            throw new \RuntimeException("Could not load file ${filePath}");
521
        }
522
523
        return $this->loadXml($xml, $libXMLExtraOptions);
524
    }
525
526
    /**
527
     * @param callable      $callback

src/voku/helper/HtmlDomParser.php 1 location

@@ 764-793 (lines=30) @@
761
     *
762
     * @return HtmlDomParser
763
     */
764
    public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface
765
    {
766
        // reset
767
        self::$domBrokenReplaceHelper = [];
768
769
        if (
770
            !\preg_match("/^https?:\/\//i", $filePath)
771
            &&
772
            !\file_exists($filePath)
773
        ) {
774
            throw new \RuntimeException("File ${filePath} not found");
775
        }
776
777
        try {
778
            if (\class_exists('\voku\helper\UTF8')) {
779
                /** @noinspection PhpUndefinedClassInspection */
780
                $html = UTF8::file_get_contents($filePath);
781
            } else {
782
                $html = \file_get_contents($filePath);
783
            }
784
        } catch (\Exception $e) {
785
            throw new \RuntimeException("Could not load file ${filePath}");
786
        }
787
788
        if ($html === false) {
789
            throw new \RuntimeException("Could not load file ${filePath}");
790
        }
791
792
        return $this->loadHtml($html, $libXMLExtraOptions);
793
    }
794
795
    /**
796
     * Get the HTML as XML or plain XML if needed.