Code Duplication    Length = 29-29 lines in 2 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 350-378 (lines=29) @@
347
     *
348
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
349
     */
350
    public function find(string $selector, $idx = null)
351
    {
352
        $xPathQuery = SelectorConverter::toXPath($selector);
353
354
        $xPath = new \DOMXPath($this->document);
355
        $nodesList = $xPath->query($xPathQuery);
356
        $elements = new SimpleHtmlDomNode();
357
358
        foreach ($nodesList as $node) {
359
            $elements[] = new SimpleHtmlDom($node);
360
        }
361
362
        // return all elements
363
        if ($idx === null) {
364
            if (\count($elements) === 0) {
365
                return new SimpleHtmlDomNodeBlank();
366
            }
367
368
            return $elements;
369
        }
370
371
        // handle negative values
372
        if ($idx < 0) {
373
            $idx = \count($elements) + $idx;
374
        }
375
376
        // return one element
377
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
378
    }
379
380
    /**
381
     * Find nodes with a CSS selector.

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.