Code Duplication    Length = 31-31 lines in 2 locations

src/voku/helper/HtmlDomParser.php 1 location

@@ 383-413 (lines=31) @@
380
     *
381
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
382
     */
383
    public function find(string $selector, $idx = null)
384
    {
385
        $xPathQuery = SelectorConverter::toXPath($selector);
386
387
        $xPath = new \DOMXPath($this->document);
388
        $nodesList = $xPath->query($xPathQuery);
389
        $elements = new SimpleHtmlDomNode();
390
391
        if ($nodesList) {
392
            foreach ($nodesList as $node) {
393
                $elements[] = new SimpleHtmlDom($node);
394
            }
395
        }
396
397
        // return all elements
398
        if ($idx === null) {
399
            if (\count($elements) === 0) {
400
                return new SimpleHtmlDomNodeBlank();
401
            }
402
403
            return $elements;
404
        }
405
406
        // handle negative values
407
        if ($idx < 0) {
408
            $idx = \count($elements) + $idx;
409
        }
410
411
        // return one element
412
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
413
    }
414
415
    /**
416
     * Find nodes with a CSS selector.

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.