Passed
Push — develop ( 52b304...9fd2b6 )
by Mikaël
01:40
created

AbstractNodeHandler::getHandlers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 10
cc 4
nc 4
nop 1
crap 4.0218
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 96
    public function __construct(DOMNode $node, AbstractDomDocumentHandler $domDocumentHandler, int $index = 0)
20
    {
21 96
        $this->node = $node;
22 96
        $this->index = $index;
23 96
        $this->domDocumentHandler = $domDocumentHandler;
24 96
    }
25
26 84
    public function getNode(): DOMNode
27
    {
28 84
        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 62
    public function getDomDocumentHandler(): AbstractDomDocumentHandler
51
    {
52 62
        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 6
    private function getHandlers(?Traversable $nodes): array
96
    {
97 6
        if (is_null($nodes)) {
98
            return [];
99
        }
100
101 6
        $handlers = array();
102 6
        foreach ($nodes as $index => $node) {
103 6
            if (!is_int($index)) {
104 2
                continue;
105
            }
106
107 4
            $handlers[] = $this->getDomDocumentHandler()->getHandler($node, $index);
108
        }
109
110 6
        return $handlers;
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116 2
    public function getNodeValue()
117
    {
118 2
        $nodeValue = trim($this->getNode()->nodeValue);
119 2
        $nodeValue = str_replace(array(
120 2
            "\r",
121
            "\n",
122
            "\t",
123
        ), array(
124 2
            '',
125
            '',
126
            ' ',
127
        ), $nodeValue);
128 2
        $nodeValue = preg_replace('[\s+]', ' ', $nodeValue);
129 2
        return $nodeValue;
130
    }
131
132
    /**
133
     * Alias for AbstractNodeHandler::getNodeValue()
134
     * @return mixed
135
     */
136 2
    public function getValue()
137
    {
138 2
        return $this->getNodeValue();
139
    }
140
141 2
    public function getValueNamespace(): ?string
142
    {
143 2
        return null;
144
    }
145
}
146