Finder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 18
c 3
b 0
f 0
dl 0
loc 51
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B find() 0 26 11
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PHPHtmlParser;
6
7
use PHPHtmlParser\Dom\Node\AbstractNode;
8
use PHPHtmlParser\Dom\Node\InnerNode;
9
use PHPHtmlParser\Exceptions\ChildNotFoundException;
10
use PHPHtmlParser\Exceptions\ParentNotFoundException;
11
12
class Finder
13
{
14
    /**
15
     * @var int
16
     */
17
    private $id;
18
19
    /**
20
     * Finder constructor.
21
     *
22
     * @param $id
23
     */
24
    public function __construct($id)
25
    {
26
        $this->id = $id;
27
    }
28
29
    /**
30
     * Find node in tree by id.
31
     *
32
     * @throws ChildNotFoundException
33
     * @throws ParentNotFoundException
34
     *
35
     * @return bool|AbstractNode
36
     */
37
    public function find(AbstractNode $node)
38
    {
39
        if (!$node->id() && $node instanceof InnerNode) {
40
            return $this->find($node->firstChild());
41
        }
42
43
        if ($node->id() == $this->id) {
44
            return $node;
45
        }
46
47
        if ($node->hasNextSibling()) {
48
            $nextSibling = $node->nextSibling();
49
            if ($nextSibling->id() == $this->id) {
50
                return $nextSibling;
51
            }
52
            if ($nextSibling->id() > $this->id && $node instanceof InnerNode) {
53
                return $this->find($node->firstChild());
54
            }
55
            if ($nextSibling->id() < $this->id) {
56
                return $this->find($nextSibling);
57
            }
58
        } elseif (!$node->isTextNode() && $node instanceof InnerNode) {
59
            return $this->find($node->firstChild());
60
        }
61
62
        return false;
63
    }
64
}
65