Code Duplication    Length = 27-31 lines in 3 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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

src/voku/helper/XmlDomParser.php 1 location

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