Completed
Pull Request — master (#51)
by Günter
02:02
created

DOMTools::nodeToArray()   C

Complexity

Conditions 19
Paths 34

Size

Total Lines 72
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 5.4418
c 0
b 0
f 0
cc 19
eloc 43
nc 34
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP\DOM;
13
14
use DOMElement;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AfriCC\EPP\DOM\DOMElement.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
16
class DOMTools
17
{
18
    /**
19
     * transform epp style DOMElement to a php-array
20
     *
21
     * @param DOMElement $node
22
     * @param array $forceArrayKeys force certain tags into an array that are
23
     * expected to be iterated (forex: street).
24
     * @param array $ignoreAttributeKeys ignore certain attributes as they
25
     * contain duplicate information (forex: ipType).
26
     */
27
    public static function nodeToArray(
28
        DOMElement $node,
29
        $forceArrayKeys = ['hostAttr', 'hostObj', 'street', 'hostAddr'],
30
        $ignoreAttributeKeys = ['hostAddr']
31
    )
32
    {
33
        $tmp = [];
34
        foreach ($node->childNodes as $each) {
35
            if ($each->nodeType !== XML_ELEMENT_NODE) {
36
                continue;
37
            }
38
39
            // if node only has a type attribute lets distinguish them directly
40
            // and then ignore the attribtue
41
            if ($each->hasAttribute('type')) {
42
                $key = $each->localName . '@' . $each->getAttribute('type');
43
                $ignore_attributes = true;
44
            } else {
45
                $key = $each->localName;
46
                $ignore_attributes = false;
47
            }
48
49
            // in case of special keys, always create array
50
            if (in_array($key, $forceArrayKeys)) {
51
                $current = &$tmp[$key][];
52
                end($tmp[$key]);
53
                $insert_key = key($tmp[$key]);
54
            }
55
            // if key already exists, dynamically create an array
56
            elseif (isset($tmp[$key])) {
57
                if (!is_array($tmp[$key]) || !isset($tmp[$key][0])) {
58
                    $tmp[$key] = [$tmp[$key]];
59
                }
60
                $current = &$tmp[$key][];
61
                end($tmp[$key]);
62
                $insert_key = key($tmp[$key]);
63
            }
64
            // key was not yet set, so lets start off with a string
65
            else {
66
                $current = &$tmp[$key];
67
                $insert_key = null;
68
            }
69
70
            if ($each->hasChildNodes()) {
71
                $current = static::nodeToArray($each, $forceArrayKeys, $ignoreAttributeKeys);
72
            } else {
73
                $current = $each->nodeValue;
74
75
                if (!$ignore_attributes && !in_array($each->localName, $ignoreAttributeKeys) && $each->hasAttributes()) {
76
                    foreach ($each->attributes as $attr) {
77
78
                        // single attribute with empty node, use the attr-value directly
79
                        if ($each->localName === 'status' || ($each->attributes->length === 1 && $each->nodeValue === '')) {
80
                            $current = $attr->nodeValue;
81
                            break;
82
                        }
83
84
                        if ($insert_key) {
85
                            if (isset($tmp['@' . $key][$attr->nodeName]) && !is_array($tmp['@' . $key][$attr->nodeName])) {
86
                                $tmp['@' . $key][$attr->nodeName] = [$tmp['@' . $key][$attr->nodeName]];
87
                            }
88
                            $tmp['@' . $key][$attr->nodeName][$insert_key] = $attr->nodeValue;
89
                        } else {
90
                            $tmp['@' . $key][$attr->nodeName] = $attr->nodeValue;
91
                        }
92
                    }
93
                }
94
            }
95
        }
96
97
        return $tmp;
98
    }
99
}
100