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 ( 44b402...d5a68e )
by Orlando
01:12
created

Node::isGlobalDefinition()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
nop 1
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 attributes.
50
     *
51
     * @var array
52
     */
53
    protected $attr = [];
54
55
    /**
56
     * Create a new node instance.
57
     *
58
     * @param array $attr
59
     */
60
    public function __construct(array ...$attr)
61
    {
62
        $this->attr = $attr;
63
64
        $this->document = new DOMDocument('1.0', 'UTF-8');
65
        $this->document->preserveWhiteSpace = false;
66
67
        if ($nodeName = $this->getNodeName()) {
68
            $this->element = $this->document->createElement($nodeName);
69
            $this->document->appendChild($this->element);
70
            $this->setAttr($this->element, $this->getAttr());
71
        }
72
    }
73
74
    /**
75
     * Add a new node.
76
     *
77
     * @param \Kinedu\CfdiXML\Common\Node $node
78
     *
79
     * @return void
80
     */
81
    public function add(Node $node)
82
    {
83
        $wrapperElement = null;
84
85
        if (isset($node->schemaDefinition) && $node->globalSchemaLocation) {
0 ignored issues
show
Bug introduced by
The property globalSchemaLocation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
86
            $this->setSchemaDefinition(
87
                $node->getSchemaLocation()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Kinedu\CfdiXML\Common\Node as the method getSchemaLocation() does only exist in the following sub-classes of Kinedu\CfdiXML\Common\Node: Kinedu\CfdiXML\Common\Complemento, Kinedu\CfdiXML\Common\ComplementoBase, Kinedu\CfdiXML\Common\ComplementoConcepto, Kinedu\CfdiXML\Common\Complemento\IEDU, Kinedu\CfdiXML\Common\Complemento\Pago, Kinedu\CfdiXML\Common\Co...nto\TimbreFiscalDigital. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
88
            );
89
90
            $this->setNamespace($node);
91
        }
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
     * @param  \Kinedu\CfdiXML\Common\Node  $node
158
     * @return void
159
     */
160
    public function setNamespace(Node $node)
161
    {
162
        $element = $this->element;
163
164
        $namespaceKey = "xmlns:{$node->namespaceKey}";
0 ignored issues
show
Bug introduced by
The property namespaceKey does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
165
        $namespaceValue = $node->getNamespace();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Kinedu\CfdiXML\Common\Node as the method getNamespace() does only exist in the following sub-classes of Kinedu\CfdiXML\Common\Node: Kinedu\CfdiXML\Common\Complemento, Kinedu\CfdiXML\Common\ComplementoBase, Kinedu\CfdiXML\Common\ComplementoConcepto, Kinedu\CfdiXML\Common\Complemento\IEDU, Kinedu\CfdiXML\Common\Complemento\Pago, Kinedu\CfdiXML\Common\Co...nto\TimbreFiscalDigital. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
166
167
        $elementAttr = [];
168
169
        if ($element->hasAttributes()) {
170
            $lastAttrName = null;
171
172
            foreach (iterator_to_array($element->attributes) as $attr) {
173
                if ((substr($lastAttrName, 0, 5) == 'xmlns') &&
174
                    (substr($attr->name, 0, 5) != 'xmlns')) {
175
                    $elementAttr[$namespaceKey] = $namespaceValue;
176
                }
177
178
                $elementAttr[$lastAttrName = $attr->name] = $attr->value;
179
180
                $element->removeAttributeNode($attr);
181
            }
182
        }
183
184
        $this->setAttr($element, $elementAttr);
185
    }
186
187
    /**
188
     * @param  string  $schemaDefinition
189
     * @return void
190
     */
191
    public function setSchemaDefinition(string $schemaDefinition)
192
    {
193
        $attrName = 'xsi:schemaLocation';
194
195
        $node = $this->element;
196
197
        if ($node->hasAttribute($attrName)) {
198
            $value = $node->getAttribute($attrName);
199
            $value = "{$value} {$schemaDefinition}";
200
201
            $node->setAttribute($attrName, $value);
202
        }
203
    }
204
205
    /**
206
     * Get node attributes.
207
     *
208
     * @param string $index
209
     *
210
     * @return array|null
211
     */
212
    public function getAttr(string $index = 'node')
213
    {
214
        $attrIndex = ['node', 'parent', 'wrapper'];
215
216
        $index = (in_array($index, $attrIndex))
217
            ? array_search($index, $attrIndex)
218
            : 0;
219
220
        return $this->attr[$index] ?? null;
221
    }
222
223
    /**
224
     * Adds attributes to an element.
225
     *
226
     * @param DOMElement $element
227
     * @param array $attr
228
     *
229
     * @return void
230
     */
231
    public function setAttr(DOMElement $element, array $attr = null)
232
    {
233
        if (!is_null($attr)) {
234
            foreach ($attr as $key => $value) {
235
                $element->setAttribute($key, $value);
236
            }
237
        }
238
    }
239
240
    /**
241
     * Get element.
242
     *
243
     * @return DOMElement
244
     */
245
    public function getElement(): DOMElement
246
    {
247
        return $this->element;
248
    }
249
250
    /**
251
     * Get document.
252
     *
253
     * @return DOMDocument
254
     */
255
    public function getDocument(): DOMDocument
256
    {
257
        return $this->document;
258
    }
259
260
    /**
261
     * Get wrapper node name.
262
     *
263
     * @return string|null
264
     */
265
    public function getWrapperNodeName()
266
    {
267
        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...
268
    }
269
270
    /**
271
     * Get parent node name.
272
     *
273
     * @return string|null
274
     */
275
    public function getParentNodeName()
276
    {
277
        return $this->parentNodeName ?? null;
278
    }
279
280
    /**
281
     * Get node name.
282
     *
283
     * @return string
284
     */
285
    public function getNodeName()
286
    {
287
        return $this->nodeName ?? null;
288
    }
289
}
290