GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ca51c3...fdcae1 )
by Orlando
01:22
created

Node::getValidAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the cfdi-xml project.
5
 *
6
 * (c) Kinedu
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kinedu\CfdiXML\Common;
13
14
use DOMElement;
15
use DOMDocument;
16
use DOMNodeList;
17
18
class Node
19
{
20
    /**
21
     * Node document.
22
     *
23
     * @var DOMDocument
24
     */
25
    protected $document;
26
27
    /**
28
     * Node element.
29
     *
30
     * @var DOMElement
31
     */
32
    protected $element;
33
34
    /**
35
     * Define the parent node name.
36
     *
37
     * @var string|null
38
     */
39
    protected $parentNodeName = null;
40
41
    /**
42
     * Define the node name.
43
     *
44
     * @var string
45
     */
46
    protected $nodeName;
47
48
    /**
49
     * Node valid attributes.
50
     *
51
     * @var array
52
     */
53
    protected $validAttributes;
54
55
    /**
56
     * Node attributes.
57
     *
58
     * @var array
59
     */
60
    protected $attr = [];
61
62
    /**
63
     * Create a new node instance.
64
     *
65
     * @param array $attr
66
     */
67
    public function __construct(array ...$attr)
68
    {
69
        $this->attr = $attr;
70
71
        $this->document = new DOMDocument('1.0', 'UTF-8');
72
        $this->document->preserveWhiteSpace = false;
73
        $this->document->formatOutput = true;
74
75
        if ($nodeName = $this->getNodeName()) {
76
            $this->element = $this->document->createElement($nodeName);
77
            $this->document->appendChild($this->element);
78
            $this->setAttr($this->element, $this->getAttr());
79
        }
80
    }
81
82
    /**
83
     * Add a new node.
84
     *
85
     * @param \Kinedu\CfdiXML\Common\Node $node
86
     *
87
     * @return void
88
     */
89
    public function add(Node $node)
90
    {
91
        $wrapperElement = null;
92
93
        $nodeElement = $this->document->createElement($node->getNodeName());
94
        $this->setAttr($nodeElement, $node->getAttr());
95
96
        foreach ($node->element->childNodes as $child) {
97
            $nodeElement->appendChild(
98
                $this->document->importNode($child, true)
99
            );
100
        }
101
102
        if ($wrapperName = $node->getWrapperNodeName()) {
103
            $wrapperElement = $this->getDirectChildElementByName(
104
                $this->element->childNodes,
105
                $wrapperName
106
            );
107
108
            if (!$wrapperElement) {
109
                $wrapperElement = $this->document->createElement($wrapperName);
110
                $this->element->appendChild($wrapperElement);
111
            }
112
113
            $this->setAttr($wrapperElement, $node->getAttr('wrapper'));
114
        }
115
116
        if ($parentName = $node->getParentNodeName()) {
117
            $currentElement = ($wrapperElement) ? $wrapperElement : $this->element;
118
119
            $parentNode = $this->getDirectChildElementByName(
120
                $currentElement->childNodes,
121
                $parentName
122
            );
123
124
            if (!$parentNode) {
125
                $parentElement = $this->document->createElement($parentName);
126
                $currentElement->appendChild($parentElement);
127
                $parentElement->appendChild($nodeElement);
128
                $this->setAttr($parentElement, $node->getAttr('parent'));
129
            } else {
130
                $parentNode->appendChild($nodeElement);
131
            }
132
        } else {
133
            $this->element->appendChild($nodeElement);
134
        }
135
    }
136
137
    /**
138
     * Search the direct child of an element.
139
     *
140
     * @param DOMNodeList $children
141
     * @param string $find
142
     *
143
     * @return DOMElement|null
144
     */
145
    protected function getDirectChildElementByName(DOMNodeList $children, string $find)
146
    {
147
        foreach ($children as $child) {
148
            if ($child->nodeName == $find) {
149
                return $child;
150
            }
151
        }
152
153
        return null;
154
    }
155
156
    /**
157
     * Get the node valid attributes.
158
     *
159
     * @return array
160
     */
161
    public function getValidAttributes(): array
162
    {
163
        return $this->validAttributes;
164
    }
165
166
    /**
167
     * Get node attributes.
168
     *
169
     * @param string $index
170
     *
171
     * @return array|null
172
     */
173
    public function getAttr(string $index = 'node')
174
    {
175
        $attrIndex = ['node', 'parent', 'wrapper'];
176
177
        $index = (in_array($index, $attrIndex))
178
            ? array_search($index, $attrIndex)
179
            : 0;
180
181
        return $this->attr[$index] ?? null;
182
    }
183
184
    /**
185
     * Adds attributes to an element.
186
     *
187
     * @param DOMElement $element
188
     * @param array $attr
189
     *
190
     * @return void
191
     */
192
    public function setAttr(DOMElement $element, array $attr = null)
193
    {
194
        if (!is_null($attr)) {
195
            foreach ($attr as $key => $value) {
196
                $element->setAttribute($key, $value);
197
            }
198
        }
199
    }
200
201
    /**
202
     * Get element.
203
     *
204
     * @return DOMElement
205
     */
206
    public function getElement(): DOMElement
207
    {
208
        return $this->element;
209
    }
210
211
    /**
212
     * Get document.
213
     *
214
     * @return DOMDocument
215
     */
216
    public function getDocument(): DOMDocument
217
    {
218
        return $this->document;
219
    }
220
221
    /**
222
     * Get wrapper node name.
223
     *
224
     * @return string|null
225
     */
226
    public function getWrapperNodeName()
227
    {
228
        return $this->wrapperNodeName ?? null;
0 ignored issues
show
Bug introduced by
The property wrapperNodeName does not seem to exist. Did you mean nodeName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
229
    }
230
231
    /**
232
     * Get parent node name.
233
     *
234
     * @return string|null
235
     */
236
    public function getParentNodeName()
237
    {
238
        return $this->parentNodeName ?? null;
239
    }
240
241
    /**
242
     * Get node name.
243
     *
244
     * @return string
245
     */
246
    public function getNodeName()
247
    {
248
        return $this->nodeName ?? null;
249
    }
250
}
251