Code Duplication    Length = 31-31 lines in 2 locations

src/voku/helper/XmlDomParser.php 1 location

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

src/voku/helper/HtmlDomParser.php 1 location

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