Code Duplication    Length = 29-29 lines in 2 locations

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.

src/voku/helper/HtmlDomParser.php 1 location

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