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

@@ 471-501 (lines=31) @@
468
     *
469
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
470
     */
471
    public function find(string $selector, $idx = null)
472
    {
473
        $xPathQuery = SelectorConverter::toXPath($selector);
474
475
        $xPath = new \DOMXPath($this->document);
476
        $nodesList = $xPath->query($xPathQuery);
477
        $elements = new SimpleHtmlDomNode();
478
479
        if ($nodesList) {
480
            foreach ($nodesList as $node) {
481
                $elements[] = new SimpleHtmlDom($node);
482
            }
483
        }
484
485
        // return all elements
486
        if ($idx === null) {
487
            if (\count($elements) === 0) {
488
                return new SimpleHtmlDomNodeBlank();
489
            }
490
491
            return $elements;
492
        }
493
494
        // handle negative values
495
        if ($idx < 0) {
496
            $idx = \count($elements) + $idx;
497
        }
498
499
        // return one element
500
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
501
    }
502
503
    /**
504
     * Find nodes with a CSS selector.