Code Duplication    Length = 27-31 lines in 3 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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

src/voku/helper/XmlDomParser.php 1 location

@@ 363-389 (lines=27) @@
360
     *
361
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
362
     */
363
    public function getElementsByTagName(string $name, $idx = null)
364
    {
365
        $nodesList = $this->document->getElementsByTagName($name);
366
367
        $elements = new SimpleXmlDomNode();
368
369
        foreach ($nodesList as $node) {
370
            $elements[] = new SimpleXmlDom($node);
371
        }
372
373
        // return all elements
374
        if ($idx === null) {
375
            if (\count($elements) === 0) {
376
                return new SimpleXmlDomNodeBlank();
377
            }
378
379
            return $elements;
380
        }
381
382
        // handle negative values
383
        if ($idx < 0) {
384
            $idx = \count($elements) + $idx;
385
        }
386
387
        // return one element
388
        return $elements[$idx] ?? new SimpleXmlDomNodeBlank();
389
    }
390
391
    /**
392
     * Get dom node's outer html.