Code Duplication    Length = 27-27 lines in 3 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 629-655 (lines=27) @@
626
     *
627
     * @return HtmlDomParser
628
     */
629
    public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface
630
    {
631
        if (
632
            !\preg_match("/^https?:\/\//i", $filePath)
633
            &&
634
            !\file_exists($filePath)
635
        ) {
636
            throw new \RuntimeException("File ${filePath} not found");
637
        }
638
639
        try {
640
            if (\class_exists('\voku\helper\UTF8')) {
641
                /** @noinspection PhpUndefinedClassInspection */
642
                $html = UTF8::file_get_contents($filePath);
643
            } else {
644
                $html = \file_get_contents($filePath);
645
            }
646
        } catch (\Exception $e) {
647
            throw new \RuntimeException("Could not load file ${filePath}");
648
        }
649
650
        if ($html === false) {
651
            throw new \RuntimeException("Could not load file ${filePath}");
652
        }
653
654
        return $this->loadHtml($html, $libXMLExtraOptions);
655
    }
656
657
    /**
658
     * @param string $html

src/voku/helper/XmlDomParser.php 2 locations

@@ 395-421 (lines=27) @@
392
     *
393
     * @return XmlDomParser
394
     */
395
    public function loadHtmlFile(string $filePath, $libXMLExtraOptions = null): DomParserInterface
396
    {
397
        if (
398
            !\preg_match("/^https?:\/\//i", $filePath)
399
            &&
400
            !\file_exists($filePath)
401
        ) {
402
            throw new \RuntimeException("File ${filePath} not found");
403
        }
404
405
        try {
406
            if (\class_exists('\voku\helper\UTF8')) {
407
                /** @noinspection PhpUndefinedClassInspection */
408
                $html = UTF8::file_get_contents($filePath);
409
            } else {
410
                $html = \file_get_contents($filePath);
411
            }
412
        } catch (\Exception $e) {
413
            throw new \RuntimeException("Could not load file ${filePath}");
414
        }
415
416
        if ($html === false) {
417
            throw new \RuntimeException("Could not load file ${filePath}");
418
        }
419
420
        return $this->loadHtml($html, $libXMLExtraOptions);
421
    }
422
423
    /**
424
     * @param string $selector
@@ 459-485 (lines=27) @@
456
     *
457
     * @return XmlDomParser
458
     */
459
    public function loadXmlFile(string $filePath, $libXMLExtraOptions = null): self
460
    {
461
        if (
462
            !\preg_match("/^https?:\/\//i", $filePath)
463
            &&
464
            !\file_exists($filePath)
465
        ) {
466
            throw new \RuntimeException("File ${filePath} not found");
467
        }
468
469
        try {
470
            if (\class_exists('\voku\helper\UTF8')) {
471
                /** @noinspection PhpUndefinedClassInspection */
472
                $xml = UTF8::file_get_contents($filePath);
473
            } else {
474
                $xml = \file_get_contents($filePath);
475
            }
476
        } catch (\Exception $e) {
477
            throw new \RuntimeException("Could not load file ${filePath}");
478
        }
479
480
        if ($xml === false) {
481
            throw new \RuntimeException("Could not load file ${filePath}");
482
        }
483
484
        return $this->loadXml($xml, $libXMLExtraOptions);
485
    }
486
487
    /**
488
     * @param callable      $callback