Code Duplication    Length = 27-31 lines in 3 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

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.