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

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