NodeProximityAssistant   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 25
rs 10
c 1
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A closest() 0 21 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Scavenger\Helper;
6
7
use InvalidArgumentException;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
class NodeProximityAssistant
11
{
12
    private const CURRENT_NODE_LIST_EMPTY = 'The current node list is empty.';
13
14
    public function closest(string $selector, Crawler $crawler): ?Crawler
15
    {
16
        if (!count($crawler)) {
17
            throw new InvalidArgumentException(self::CURRENT_NODE_LIST_EMPTY);
18
        }
19
20
        $node = $crawler->getNode(0);
21
22
        if ($node !== null) {
23
            while ($node = $node->parentNode) {
24
                if (XML_ELEMENT_NODE === $node->nodeType) {
25
                    $parentCrawler = new Crawler($node, $crawler->getUri(), $crawler->getBaseHref());
26
                    $descendantMatchingSelector = $parentCrawler->filter($selector);
27
                    if ($descendantMatchingSelector->count()) {
28
                        return $descendantMatchingSelector;
29
                    }
30
                }
31
            }
32
        }
33
34
        return null;
35
    }
36
}
37