Completed
Push — master ( 3607c7...59cd47 )
by Chris
03:04
created

Node   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 58.54%

Importance

Changes 0
Metric Value
wmc 17
dl 0
loc 123
ccs 24
cts 41
cp 0.5854
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstChild() 0 3 1
A getKey() 0 3 1
A getParent() 0 3 1
A getLastChild() 0 3 1
A __clone() 0 3 1
A containsChild() 0 3 1
A getPreviousSibling() 0 3 1
A getOwnerDocument() 0 3 1
A setReferences() 0 6 1
A getNextSibling() 0 3 1
B createFromValue() 0 30 5
A hasChildren() 0 3 1
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
use DaveRandom\Jom\Exceptions\InvalidNodeValueException;
6
7
abstract class Node implements \JsonSerializable
8
{
9
    public const IGNORE_INVALID_VALUES = 0b01;
10
    public const PERMIT_INCORRECT_REFERENCE_TYPE = 0b10;
11
12
    protected $ownerDocument;
13
14
    /** @var string|int|null */
15
    protected $key;
16
17
    /** @var VectorNode|null */
18
    protected $parent;
19
20
    /** @var Node|null */
21
    protected $previousSibling;
22
23
    /** @var Node|null */
24
    protected $nextSibling;
25
26
    /**
27
     * @throws InvalidNodeValueException
28
     * @return static
29
     */
30 37
    public static function createFromValue($value, ?Document $ownerDocument = null, ?int $flags = 0): Node
31
    {
32 37
        static $nodeFactory;
33
34 37
        $flags = $flags ?? 0;
35
36
        try {
37 37
            $result = ($nodeFactory ?? $nodeFactory = new UnsafeNodeFactory)
38 37
                ->createNodeFromValue($value, $ownerDocument, $flags);
39
        } catch (InvalidNodeValueException $e) {
40
            throw $e;
41
        //@codeCoverageIgnoreStart
42
        } catch (\Exception $e) {
43
            /** @noinspection PhpInternalEntityUsedInspection */
44
            throw unexpected($e);
45
        }
46
        //@codeCoverageIgnoreEnd
47
48
        // Check that the created node matches the class used to call the method
49 37
        if (!($result instanceof static) && !($flags & self::PERMIT_INCORRECT_REFERENCE_TYPE)) {
50
            /** @noinspection PhpInternalEntityUsedInspection */
51
            throw new InvalidNodeValueException(\sprintf(
52
                "Value of type %s parsed as instance of %s, instance of %s expected",
53
                describe($value),
54
                \get_class($result),
55
                static::class
56
            ));
57
        }
58
59 37
        return $result;
60
    }
61
62 165
    protected function __construct(?Document $ownerDocument)
63
    {
64 165
        $this->ownerDocument = $ownerDocument;
65
    }
66
67
    public function __clone()
68
    {
69
        $this->setReferences(null, null, null, null);
70
    }
71
72 151
    final protected function setReferences(?VectorNode $parent, $key, ?Node $previousSibling, ?Node $nextSibling): void
73
    {
74 151
        $this->parent = $parent;
75 151
        $this->key = $key;
76 151
        $this->previousSibling = $previousSibling;
77 151
        $this->nextSibling = $nextSibling;
78
    }
79
80 47
    final public function getParent(): ?VectorNode
81
    {
82 47
        return $this->parent;
83
    }
84
85 7
    final public function getPreviousSibling(): ?Node
86
    {
87 7
        return $this->previousSibling;
88
    }
89
90 7
    final public function getNextSibling(): ?Node
91
    {
92 7
        return $this->nextSibling;
93
    }
94
95
    public function hasChildren(): bool
96
    {
97
        return false;
98
    }
99
100
    public function containsChild(/** @noinspection PhpUnusedParameterInspection */ Node $child): bool
101
    {
102
        return false;
103
    }
104
105
    public function getFirstChild(): ?Node
106
    {
107
        return null;
108
    }
109
110
    public function getLastChild(): ?Node
111
    {
112
        return null;
113
    }
114
115 49
    final public function getOwnerDocument(): ?Document
116
    {
117 49
        return $this->ownerDocument;
118
    }
119
120
    /**
121
     * @return string|int|null
122
     */
123 50
    final public function getKey()
124
    {
125 50
        return $this->key;
126
    }
127
128
    abstract public function getValue();
129
    abstract public function jsonSerialize();
130
}
131