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

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