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

@@ 366-394 (lines=29) @@
363
     *
364
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
365
     */
366
    public function find(string $selector, $idx = null)
367
    {
368
        $xPathQuery = SelectorConverter::toXPath($selector);
369
370
        $xPath = new \DOMXPath($this->document);
371
        $nodesList = $xPath->query($xPathQuery);
372
        $elements = new SimpleHtmlDomNode();
373
374
        foreach ($nodesList as $node) {
375
            $elements[] = new SimpleHtmlDom($node);
376
        }
377
378
        // return all elements
379
        if ($idx === null) {
380
            if (\count($elements) === 0) {
381
                return new SimpleHtmlDomNodeBlank();
382
            }
383
384
            return $elements;
385
        }
386
387
        // handle negative values
388
        if ($idx < 0) {
389
            $idx = \count($elements) + $idx;
390
        }
391
392
        // return one element
393
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
394
    }
395
396
    /**
397
     * Find nodes with a CSS selector.