Code Duplication    Length = 27-31 lines in 3 locations

src/voku/helper/SimpleXmlDom.php 1 location

@@ 513-543 (lines=31) @@
510
     *
511
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
512
     */
513
    public function getElementsByTagName(string $name, $idx = null)
514
    {
515
        if ($this->node instanceof \DOMElement) {
516
            $nodesList = $this->node->getElementsByTagName($name);
517
        } else {
518
            $nodesList = [];
519
        }
520
521
        $elements = new SimpleXmlDomNode();
522
523
        foreach ($nodesList as $node) {
524
            $elements[] = new static($node);
525
        }
526
527
        // return all elements
528
        if ($idx === null) {
529
            if (\count($elements) === 0) {
530
                return new SimpleXmlDomNodeBlank();
531
            }
532
533
            return $elements;
534
        }
535
536
        // handle negative values
537
        if ($idx < 0) {
538
            $idx = \count($elements) + $idx;
539
        }
540
541
        // return one element
542
        return $elements[$idx] ?? new SimpleXmlDomBlank();
543
    }
544
545
    /**
546
     * @return \DOMNode

src/voku/helper/XmlDomParser.php 1 location

@@ 446-472 (lines=27) @@
443
     *
444
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
445
     */
446
    public function getElementsByTagName(string $name, $idx = null)
447
    {
448
        $nodesList = $this->document->getElementsByTagName($name);
449
450
        $elements = new SimpleXmlDomNode();
451
452
        foreach ($nodesList as $node) {
453
            $elements[] = new SimpleXmlDom($node);
454
        }
455
456
        // return all elements
457
        if ($idx === null) {
458
            if (\count($elements) === 0) {
459
                return new SimpleXmlDomNodeBlank();
460
            }
461
462
            return $elements;
463
        }
464
465
        // handle negative values
466
        if ($idx < 0) {
467
            $idx = \count($elements) + $idx;
468
        }
469
470
        // return one element
471
        return $elements[$idx] ?? new SimpleXmlDomNodeBlank();
472
    }
473
474
    /**
475
     * Get dom node's outer html.

src/voku/helper/SimpleHtmlDom.php 1 location

@@ 542-572 (lines=31) @@
539
     *
540
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
541
     */
542
    public function getElementsByTagName(string $name, $idx = null)
543
    {
544
        if ($this->node instanceof \DOMElement) {
545
            $nodesList = $this->node->getElementsByTagName($name);
546
        } else {
547
            $nodesList = [];
548
        }
549
550
        $elements = new SimpleHtmlDomNode();
551
552
        foreach ($nodesList as $node) {
553
            $elements[] = new static($node);
554
        }
555
556
        // return all elements
557
        if ($idx === null) {
558
            if (\count($elements) === 0) {
559
                return new SimpleHtmlDomNodeBlank();
560
            }
561
562
            return $elements;
563
        }
564
565
        // handle negative values
566
        if ($idx < 0) {
567
            $idx = \count($elements) + $idx;
568
        }
569
570
        // return one element
571
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
572
    }
573
574
    /**
575
     * Create a new "HtmlDomParser"-object from the current context.