Code Duplication    Length = 31-31 lines in 2 locations

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.

src/voku/helper/HtmlDomParser.php 1 location

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