Code Duplication    Length = 27-31 lines in 3 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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

src/voku/helper/XmlDomParser.php 1 location

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