Finder::find()   B
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 15
c 3
b 0
f 0
nc 8
nop 1
dl 0
loc 26
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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