Passed
Push — develop ( cd7fef...a71946 )
by Mikaël
01:37
created

AbstractNodeHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 98.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 45
c 1
b 0
f 0
dl 0
loc 131
ccs 52
cts 53
cp 0.9811
rs 10

16 Methods

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