Passed
Push — develop ( 477204...0132d1 )
by Mikaël
07:12
created

AbstractNodeHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 98.04%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
eloc 45
c 2
b 0
f 0
dl 0
loc 145
ccs 50
cts 51
cp 0.9804
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getNode() 0 3 1
A getValueNamespace() 0 3 1
A getValue() 0 3 1
A getChildNodes() 0 3 1
A getName() 0 8 2
A getDomDocumentHandler() 0 3 1
A getParent() 0 7 2
A getIndex() 0 3 1
A getHandlers() 0 13 3
A getNodeValue() 0 14 1
A hasChildren() 0 3 1
A getChildren() 0 3 1
A getNamespace() 0 8 2
A getAttributes() 0 3 1
A __construct() 0 5 1
A hasAttributes() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\DomHandler;
6
7
use DOMElement;
8
use DOMNode;
9
use DOMNodeList;
10
use Traversable;
11
12
abstract class AbstractNodeHandler
13
{
14
    protected DOMNode $node;
15
16
    protected int $index;
17
18
    protected AbstractDomDocumentHandler $domDocumentHandler;
19
20 98
    public function __construct(DOMNode $node, AbstractDomDocumentHandler $domDocumentHandler, int $index = 0)
21
    {
22 98
        $this->node = $node;
23 98
        $this->index = $index;
24 98
        $this->domDocumentHandler = $domDocumentHandler;
25
    }
26
27 86
    public function getNode(): DOMNode
28
    {
29 86
        return $this->node;
30
    }
31
32
    /**
33
     * @return DOMNodeList<DOMElement|DOMNode>
34
     */
35 2
    public function getChildNodes(): DOMNodeList
36
    {
37 2
        return $this->getNode()->childNodes;
38
    }
39
40 8
    public function getParent(): ?AbstractNodeHandler
41
    {
42 8
        if ($this->getNode()->parentNode instanceof DOMNode) {
43 6
            return $this->getDomDocumentHandler()->getHandler($this->getNode()->parentNode);
44
        }
45
46 4
        return null;
47
    }
48
49 2
    public function getIndex(): int
50
    {
51 2
        return $this->index;
52
    }
53
54 64
    public function getDomDocumentHandler(): AbstractDomDocumentHandler
55
    {
56 64
        return $this->domDocumentHandler;
57
    }
58
59 8
    public function getName(): string
60
    {
61 8
        $name = $this->getNode()->nodeName;
62 8
        if (false !== strpos($name, ':')) {
63 6
            $name = implode('', array_slice(explode(':', $name), -1, 1));
64
        }
65
66 8
        return $name;
67
    }
68
69 4
    public function getNamespace(): ?string
70
    {
71 4
        $name = $this->getNode()->nodeName;
72 4
        if (false !== strpos($name, ':')) {
73 2
            return implode('', array_slice(explode(':', $name), 0, -1));
74
        }
75
76 2
        return null;
77
    }
78
79 2
    public function hasAttributes(): bool
80
    {
81 2
        return $this->getNode()->hasAttributes();
82
    }
83
84
    /**
85
     * @return AbstractAttributeHandler[]|AbstractNodeHandler[]
86
     */
87 2
    public function getAttributes(): array
88
    {
89 2
        return $this->getHandlers($this->getNode()->attributes);
90
    }
91
92 8
    public function hasChildren(): bool
93
    {
94 8
        return $this->getNode()->hasChildNodes();
95
    }
96
97
    /**
98
     * @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
99
     */
100 4
    public function getChildren(): array
101
    {
102 4
        return $this->getHandlers($this->getNode()->childNodes);
103
    }
104
105
    /**
106
     * @return mixed
107
     */
108 2
    public function getNodeValue()
109
    {
110 2
        $nodeValue = trim($this->getNode()->nodeValue);
111 2
        $nodeValue = str_replace([
112
            "\r",
113
            "\n",
114
            "\t",
115
        ], [
116 2
            '',
117
            '',
118
            ' ',
119
        ], $nodeValue);
120
121 2
        return preg_replace('[\s+]', ' ', $nodeValue);
122
    }
123
124
    /**
125
     * Alias for AbstractNodeHandler::getNodeValue().
126
     *
127
     * @return mixed
128
     */
129 2
    public function getValue()
130
    {
131 2
        return $this->getNodeValue();
132
    }
133
134 2
    public function getValueNamespace(): ?string
135
    {
136 2
        return null;
137
    }
138
139
    /**
140
     * @param null|Traversable<DOMNode> $nodes
141
     *
142
     * @return AbstractAttributeHandler[]|AbstractElementHandler[]|AbstractNodeHandler[]
143
     */
144 6
    private function getHandlers(?Traversable $nodes): array
145
    {
146 6
        if (is_null($nodes)) {
147
            return [];
148
        }
149
150 6
        $handlers = [];
151 6
        $index = 0;
152 6
        foreach ($nodes as $node) {
153 6
            $handlers[] = $this->getDomDocumentHandler()->getHandler($node, $index++);
154
        }
155
156 6
        return $handlers;
157
    }
158
}
159