Code Duplication    Length = 31-31 lines in 2 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 416-446 (lines=31) @@
413
     *
414
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
415
     */
416
    public function find(string $selector, $idx = null)
417
    {
418
        $xPathQuery = SelectorConverter::toXPath($selector);
419
420
        $xPath = new \DOMXPath($this->document);
421
        $nodesList = $xPath->query($xPathQuery);
422
        $elements = new SimpleHtmlDomNode();
423
424
        if ($nodesList) {
425
            foreach ($nodesList as $node) {
426
                $elements[] = new SimpleHtmlDom($node);
427
            }
428
        }
429
430
        // return all elements
431
        if ($idx === null) {
432
            if (\count($elements) === 0) {
433
                return new SimpleHtmlDomNodeBlank();
434
            }
435
436
            return $elements;
437
        }
438
439
        // handle negative values
440
        if ($idx < 0) {
441
            $idx = \count($elements) + $idx;
442
        }
443
444
        // return one element
445
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
446
    }
447
448
    /**
449
     * Find nodes with a CSS selector.

src/voku/helper/XmlDomParser.php 1 location

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