Code Duplication    Length = 29-29 lines in 2 locations

src/voku/helper/HtmlDomParser.php 1 location

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

src/voku/helper/XmlDomParser.php 1 location

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