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

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