Code Duplication    Length = 29-29 lines in 2 locations

src/voku/helper/XmlDomParser.php 1 location

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

src/voku/helper/HtmlDomParser.php 1 location

@@ 383-411 (lines=29) @@
380
     *
381
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
382
     */
383
    public function find(string $selector, $idx = null)
384
    {
385
        $xPathQuery = SelectorConverter::toXPath($selector);
386
387
        $xPath = new \DOMXPath($this->document);
388
        $nodesList = $xPath->query($xPathQuery);
389
        $elements = new SimpleHtmlDomNode();
390
391
        foreach ($nodesList as $node) {
392
            $elements[] = new SimpleHtmlDom($node);
393
        }
394
395
        // return all elements
396
        if ($idx === null) {
397
            if (\count($elements) === 0) {
398
                return new SimpleHtmlDomNodeBlank();
399
            }
400
401
            return $elements;
402
        }
403
404
        // handle negative values
405
        if ($idx < 0) {
406
            $idx = \count($elements) + $idx;
407
        }
408
409
        // return one element
410
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
411
    }
412
413
    /**
414
     * Find nodes with a CSS selector.