Code Duplication    Length = 27-30 lines in 3 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 836-865 (lines=30) @@
833
     *
834
     * @return HtmlDomParser
835
     */
836
    public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface
837
    {
838
        // reset
839
        self::$domBrokenReplaceHelper = [];
840
841
        if (
842
            !\preg_match("/^https?:\/\//i", $filePath)
843
            &&
844
            !\file_exists($filePath)
845
        ) {
846
            throw new \RuntimeException("File ${filePath} not found");
847
        }
848
849
        try {
850
            if (\class_exists('\voku\helper\UTF8')) {
851
                /** @noinspection PhpUndefinedClassInspection */
852
                $html = UTF8::file_get_contents($filePath);
853
            } else {
854
                $html = \file_get_contents($filePath);
855
            }
856
        } catch (\Exception $e) {
857
            throw new \RuntimeException("Could not load file ${filePath}");
858
        }
859
860
        if ($html === false) {
861
            throw new \RuntimeException("Could not load file ${filePath}");
862
        }
863
864
        return $this->loadHtml($html, $libXMLExtraOptions);
865
    }
866
867
    /**
868
     * Get the HTML as XML or plain XML if needed.

src/voku/helper/XmlDomParser.php 2 locations

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