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

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