Code Duplication    Length = 27-30 lines in 3 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 860-889 (lines=30) @@
857
     *
858
     * @return HtmlDomParser
859
     */
860
    public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface
861
    {
862
        // reset
863
        self::$domBrokenReplaceHelper = [];
864
865
        if (
866
            !\preg_match("/^https?:\/\//i", $filePath)
867
            &&
868
            !\file_exists($filePath)
869
        ) {
870
            throw new \RuntimeException("File ${filePath} not found");
871
        }
872
873
        try {
874
            if (\class_exists('\voku\helper\UTF8')) {
875
                $html = \voku\helper\UTF8::file_get_contents($filePath);
876
            } else {
877
                $html = \file_get_contents($filePath);
878
            }
879
        } catch (\Exception $e) {
880
            throw new \RuntimeException("Could not load file ${filePath}");
881
        }
882
883
        if ($html === false) {
884
            throw new \RuntimeException("Could not load file ${filePath}");
885
        }
886
887
        return $this->loadHtml($html, $libXMLExtraOptions);
888
    }
889
890
    /**
891
     * Get the HTML as XML or plain XML if needed.
892
     *

src/voku/helper/XmlDomParser.php 2 locations

@@ 494-520 (lines=27) @@
491
     *
492
     * @return XmlDomParser
493
     */
494
    public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface
495
    {
496
        if (
497
            !\preg_match("/^https?:\/\//i", $filePath)
498
            &&
499
            !\file_exists($filePath)
500
        ) {
501
            throw new \RuntimeException("File ${filePath} not found");
502
        }
503
504
        try {
505
            if (\class_exists('\voku\helper\UTF8')) {
506
                $html = \voku\helper\UTF8::file_get_contents($filePath);
507
            } else {
508
                $html = \file_get_contents($filePath);
509
            }
510
        } catch (\Exception $e) {
511
            throw new \RuntimeException("Could not load file ${filePath}");
512
        }
513
514
        if ($html === false) {
515
            throw new \RuntimeException("Could not load file ${filePath}");
516
        }
517
518
        return $this->loadHtml($html, $libXMLExtraOptions);
519
    }
520
521
    /**
522
     * @param string $selector
523
     * @param int    $idx
@@ 567-593 (lines=27) @@
564
     *
565
     * @return XmlDomParser
566
     */
567
    public function loadXmlFile(string $filePath, $libXMLExtraOptions = null): self
568
    {
569
        if (
570
            !\preg_match("/^https?:\/\//i", $filePath)
571
            &&
572
            !\file_exists($filePath)
573
        ) {
574
            throw new \RuntimeException("File ${filePath} not found");
575
        }
576
577
        try {
578
            if (\class_exists('\voku\helper\UTF8')) {
579
                $xml = \voku\helper\UTF8::file_get_contents($filePath);
580
            } else {
581
                $xml = \file_get_contents($filePath);
582
            }
583
        } catch (\Exception $e) {
584
            throw new \RuntimeException("Could not load file ${filePath}");
585
        }
586
587
        if ($xml === false) {
588
            throw new \RuntimeException("Could not load file ${filePath}");
589
        }
590
591
        return $this->loadXml($xml, $libXMLExtraOptions);
592
    }
593
594
    /**
595
     * @param callable      $callback
596
     * @param \DOMNode|null $domNode