Code Duplication    Length = 31-31 lines in 2 locations

src/voku/helper/XmlDomParser.php 1 location

@@ 191-221 (lines=31) @@
188
     *
189
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
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
        if ($nodesList) {
200
            foreach ($nodesList as $node) {
201
                $elements[] = new SimpleXmlDom($node);
202
            }
203
        }
204
205
        // return all elements
206
        if ($idx === null) {
207
            if (\count($elements) === 0) {
208
                return new SimpleXmlDomNodeBlank();
209
            }
210
211
            return $elements;
212
        }
213
214
        // handle negative values
215
        if ($idx < 0) {
216
            $idx = \count($elements) + $idx;
217
        }
218
219
        // return one element
220
        return $elements[$idx] ?? new SimpleXmlDomBlank();
221
    }
222
223
    /**
224
     * Find nodes with a CSS selector.

src/voku/helper/HtmlDomParser.php 1 location

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