Code Duplication    Length = 31-31 lines in 2 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 482-512 (lines=31) @@
479
     *
480
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
481
     */
482
    public function find(string $selector, $idx = null)
483
    {
484
        $xPathQuery = SelectorConverter::toXPath($selector);
485
486
        $xPath = new \DOMXPath($this->document);
487
        $nodesList = $xPath->query($xPathQuery);
488
        $elements = new SimpleHtmlDomNode();
489
490
        if ($nodesList) {
491
            foreach ($nodesList as $node) {
492
                $elements[] = new SimpleHtmlDom($node);
493
            }
494
        }
495
496
        // return all elements
497
        if ($idx === null) {
498
            if (\count($elements) === 0) {
499
                return new SimpleHtmlDomNodeBlank();
500
            }
501
502
            return $elements;
503
        }
504
505
        // handle negative values
506
        if ($idx < 0) {
507
            $idx = \count($elements) + $idx;
508
        }
509
510
        // return one element
511
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
512
    }
513
514
    /**
515
     * Find nodes with a CSS selector.

src/voku/helper/XmlDomParser.php 1 location

@@ 198-228 (lines=31) @@
195
     *
196
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
197
     */
198
    public function find(string $selector, $idx = null)
199
    {
200
        $xPathQuery = SelectorConverter::toXPath($selector);
201
202
        $xPath = new \DOMXPath($this->document);
203
        $nodesList = $xPath->query($xPathQuery);
204
        $elements = new SimpleXmlDomNode();
205
206
        if ($nodesList) {
207
            foreach ($nodesList as $node) {
208
                $elements[] = new SimpleXmlDom($node);
209
            }
210
        }
211
212
        // return all elements
213
        if ($idx === null) {
214
            if (\count($elements) === 0) {
215
                return new SimpleXmlDomNodeBlank();
216
            }
217
218
            return $elements;
219
        }
220
221
        // handle negative values
222
        if ($idx < 0) {
223
            $idx = \count($elements) + $idx;
224
        }
225
226
        // return one element
227
        return $elements[$idx] ?? new SimpleXmlDomBlank();
228
    }
229
230
    /**
231
     * Find nodes with a CSS selector.