AbstractNode::getParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cloudstek\SCIM\FilterParser\AST;
6
7
/**
8
 * Abstract node.
9
 */
10
abstract class AbstractNode implements Node
11
{
12
    protected ?Node $parent = null;
13
14
    /**
15
     * Abstract node.
16
     *
17
     * @param Node|null $parent
18
     */
19
    public function __construct(?Node $parent = null)
20
    {
21
        $this->parent = $parent;
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function getParent(): ?Node
28
    {
29
        return $this->parent;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public function setParent(?Node $node): Node
36
    {
37
        $this->parent = $node;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function hasParent($parent = null, bool $recursive = false): bool
46
    {
47
        if ($parent === null) {
48
            return $this->parent !== null;
49
        }
50
51
        if ($parent instanceof Node) {
52
            $foundParent = $this->parent === $parent;
53
        } else {
54
            $foundParent = isset($this->parent) && get_class($this->parent) === $parent;
1 ignored issue
show
Bug introduced by
It seems like $this->parent can also be of type null; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            $foundParent = isset($this->parent) && get_class(/** @scrutinizer ignore-type */ $this->parent) === $parent;
Loading history...
55
        }
56
57
        if ($recursive === true && isset($this->parent) && $foundParent === false) {
58
            return $this->parent->hasParent($parent, true);
59
        }
60
61
        return $foundParent;
62
    }
63
}
64