Completed
Push — master ( b96d44...cf3a4a )
by Adam
03:18
created

Xml::elementNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Format;
4
5
use BestServedCold\PhalueObjects\Format;
6
7
final class Xml extends Format
8
{
9
    public function __construct($value)
10
    {
11
        parent::__construct($value);
12
        $domDocument = new \DOMDocument('1.0', 'UTF-8');
13
        $domDocument->formatOutput = true;
14
        $domDocument->encoding = 'UTF-8';
15
        $domDocument->loadXML($this->getValue());
16
        $this->value = $domDocument;
17
    }
18
19
    public function parse()
20
    {
21
        return [$this->getValue()->documentElement->tagName =>
22
            $this->convert($this->value->documentElement)];
23
    }
24
25
    public function convert($node, $output = [])
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
26
    {
27
        switch ($node->nodeType) {
28
            case XML_CDATA_SECTION_NODE:
29
                $output['@cdata'] = trim($node->textContent);
30
                break;
31
            case XML_TEXT_NODE:
32
                $output = trim($node->textContent);
33
                break;
34
            case XML_ELEMENT_NODE:
35
                $output = $this->elementNode($node);
36
                break;
37
        }
38
39
        return $output;
40
    }
41
42
    private function elementNode($node)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
43
    {
44
        $output = $this->addChildNodes($this->childNode($node));
45
        return $this->attributes($node, $output);
46
    }
47
48
    private function childNode($node)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
49
    {
50
        foreach($node->childNodes as $child) {
51
            $children = $this->convert($child);
52
53
            if(isset($child->tagName)) {
54
                $tagName = $child->tagName;
55
                $output[$tagName] = isset($output[$tagName])
0 ignored issues
show
Bug introduced by
The variable $output does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
56
                    ? $output[$tagName]
57
                    : [];
58
59
                $output[$tagName][] = $children;
60
            } elseif($children !== '') {
61
                $output = $children;
62
            }
63
        }
64
65
        return $output;
66
    }
67
68
    private function loopAttributes($attributes, $array = [])
69
    {
70
        // Loop through the attributes and collect them.
71
        foreach($attributes as $key => $node) {
72
            $array[$key] = (string) $node->value;
73
        }
74
75
        return $array;
76
    }
77
78
    private function attributes($node, $output)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
79
    {
80
        // If there are attributes.
81
        if($node->attributes->length) {
82
            $output = is_array($output) ? $output : ['@value' => $output];
83
            $output['@attributes'] = $this->loopAttributes($node->attributes);
84
        }
85
86
        return $output;
87
    }
88
89
    private function addChildNodes ($output)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
90
    {
91
        if(is_array($output)) {
92
            foreach ($output as $key => $value) {
93
                $output[$key] = is_array($value) && count($value) === 1
94
                    ? $value[0]
95
                    : $value;
96
            }
97
98
            $output = empty($output) ? '' : $output;
99
        }
100
101
        return $output;
102
    }
103
104
105
}
106