convertXML2JSON.php ➔ dom_to_array()   B
last analyzed

Complexity

Conditions 11
Paths 21

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 21
nop 1
dl 0
loc 48
rs 7.3166
c 0
b 0
f 0

How to fix   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
 * pretty prints a json string
5
 *
6
 * @param string $json
7
 */
8
function indent($json)
9
{
10
    $result      = '';
11
    $pos         = 0;
12
    $strLen      = strlen($json);
13
    $indentStr   = '  ';
14
    $newLine     = "\n";
15
    $prevChar    = '';
16
    $outOfQuotes = true;
17
18
    for ($i=0; $i<=$strLen; $i++) {
19
20
        // Grab the next character in the string.
21
        $char = substr($json, $i, 1);
22
23
        // Are we inside a quoted string?
24
        if ($char == '"' && $prevChar != '\\') {
25
            $outOfQuotes = !$outOfQuotes;
26
27
            // If this character is the end of an element,
28
            // output a new line and indent the next line.
29
        } elseif (($char == '}' || $char == ']') && $outOfQuotes) {
30
            $result .= $newLine;
31
            $pos --;
32
            for ($j=0; $j<$pos; $j++) {
33
                $result .= $indentStr;
34
            }
35
        }
36
37
        // Add the character to the result string.
38
        $result .= $char;
39
40
        // If the last character was the beginning of an element,
41
        // output a new line and indent the next line.
42
        if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
43
            $result .= $newLine;
44
            if ($char == '{' || $char == '[') {
45
                $pos ++;
46
            }
47
48
            for ($j = 0; $j < $pos; $j++) {
49
                $result .= $indentStr;
50
            }
51
        }
52
53
        $prevChar = $char;
54
    }
55
56
    return $result;
57
}
58
59
/**
60
 * converts a DOMElement to an array representation
61
 *
62
 * @param \DOMElement $root
63
 */
64
function dom_to_array($root)
65
{
66
    // if the node has only a single text node
67
    if (!$root->hasAttributes() && $root->childNodes->length==1
68
    && $root->childNodes->item(0)->nodeType == XML_TEXT_NODE) {
69
        return $root->childNodes->item(0)->nodeValue;
70
    }
71
72
    $result = array();
73
74
    if ($root->hasAttributes()) {
75
        $attrs = $root->attributes;
76
77
        foreach ($attrs as $i => $attr) {
78
            $result["_" . $attr->name] = $attr->value;
79
        }
80
    }
81
82
    $children = $root->childNodes;
83
84
    $group = array();
85
86
    $text = "";
87
88
    for ($i = 0; $i < $children->length; $i++) {
89
        $child = $children->item($i);
90
        if ($child->nodeType == XML_TEXT_NODE) {
91
            $text = $text . $child->nodeValue;
92
        } else {
93
            if (!isset($result[$child->nodeName])) {
94
                $result[$child->nodeName] = dom_to_array($child);
0 ignored issues
show
Compatibility introduced by
$child of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95
            } else {
96
                if (!isset($group[$child->nodeName])) {
97
                    $tmp = $result[$child->nodeName];
98
                    $result[$child->nodeName] = array($tmp);
99
                    $group[$child->nodeName] = 1;
100
                }
101
102
                $result[$child->nodeName][] = dom_to_array($child);
0 ignored issues
show
Compatibility introduced by
$child of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
103
            }
104
        }
105
    }
106
    $trimmed = trim($text);
107
    if ($trimmed != "") {
108
        $result['#text'] = $text;
109
    }
110
    return $result;
111
}
112
/**
113
 * takes a file name of an xml document and returns an json representation
114
 *
115
 * @param string $fileName
116
 */
117
function convert($fileName)
118
{
119
    $d = new DOMDocument(1, "UTF-8");
120
    $d->load($fileName);
121
    $ret[$d->documentElement->nodeName] = dom_to_array($d->documentElement);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
122
    return json_encode($ret);
123
}
124
125
echo indent(str_replace("\/", "/", convert($argv[1])));
126