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

@@ 427-457 (lines=31) @@
424
     *
425
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
426
     */
427
    public function find(string $selector, $idx = null)
428
    {
429
        $xPathQuery = SelectorConverter::toXPath($selector);
430
431
        $xPath = new \DOMXPath($this->document);
432
        $nodesList = $xPath->query($xPathQuery);
433
        $elements = new SimpleHtmlDomNode();
434
435
        if ($nodesList) {
436
            foreach ($nodesList as $node) {
437
                $elements[] = new SimpleHtmlDom($node);
438
            }
439
        }
440
441
        // return all elements
442
        if ($idx === null) {
443
            if (\count($elements) === 0) {
444
                return new SimpleHtmlDomNodeBlank();
445
            }
446
447
            return $elements;
448
        }
449
450
        // handle negative values
451
        if ($idx < 0) {
452
            $idx = \count($elements) + $idx;
453
        }
454
455
        // return one element
456
        return $elements[$idx] ?? new SimpleHtmlDomBlank();
457
    }
458
459
    /**
460
     * Find nodes with a CSS selector.