Test Failed
Pull Request — master (#19)
by Flo
02:48
created

XmlResponse::generateNode()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 36
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 36
rs 6.7272
c 1
b 1
f 0
cc 7
eloc 15
nc 4
nop 3
1
<?php
2
/**
3
 * Class XmlResponse | XmlResponse.php
4
 * @package Faulancer\Http
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\Http;
8
9
/**
10
 * Class XmlResponse
11
 */
12
class XmlResponse extends Response
13
{
14
15
    public function __construct ($content = [])
16
    {
17
        parent::__construct($content);
18
19
        $this->setContent($content);
20
    }
21
22
    /** @var array  */
23
    protected $content = [];
24
25
    /**
26
     * @param array             $data
27
     * @param \SimpleXMLElement $xml
28
     * @codeCoverageIgnore
29
     */
30
    private function convertArrayToXml(\SimpleXMLElement &$xml, $data)
31
    {
32
        foreach ($data as $key=>$value) {
33
34
            if(is_numeric($key)) {
35
                $key = 'item' . $key;
36
            }
37
38
            if(!empty($value['value']) && is_array($value['value'])) {
39
40
                $node = $this->generateNode($xml, $key, $value);
41
                $this->convertArrayToXml($node, $value['value']);
0 ignored issues
show
Bug introduced by
It seems like $node defined by $this->generateNode($xml, $key, $value) on line 40 can be null; however, Faulancer\Http\XmlResponse::convertArrayToXml() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
43
            } else {
44
                $this->generateNode($xml, $key, $value);
45
            }
46
47
        }
48
    }
49
50
    /**
51
     * @param \SimpleXMLElement $xml
52
     * @param string            $key
53
     * @param null|array|string $value
54
     * @return null|\SimpleXMLElement
55
     * @codeCoverageIgnore
56
     */
57
    private function generateNode(\SimpleXMLElement &$xml, $key, $value = null)
58
    {
59
        $node = null;
0 ignored issues
show
Unused Code introduced by
$node is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
61
        if (in_array('@attributes', array_keys($value))) {
62
63
            $attributes = $value['@attributes'];
64
65
            if (!empty($value['value']) && !is_array($value['value'])) {
66
67
                $node = $xml->addChild($key, htmlspecialchars($value));
68
69
                foreach ($attributes as $attr => $val) {
70
                    $node->addAttribute($attr, $val);
71
                }
72
73
            } else {
74
75
                $node = $this->convertArrayToXml($xml, $value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 57 can also be of type string; however, Faulancer\Http\XmlResponse::convertArrayToXml() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
Are you sure the assignment to $node is correct as $this->convertArrayToXml($xml, $value) (which targets Faulancer\Http\XmlResponse::convertArrayToXml()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
77
            }
78
79
        } else if (!empty($value['value']) && is_array($value['value'])) {
80
81
            $node = $this->convertArrayToXml($xml, $value['value']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $node is correct as $this->convertArrayToXml($xml, $value['value']) (which targets Faulancer\Http\XmlResponse::convertArrayToXml()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
83
        } else {
84
85
            $node = $xml->addChild($key, htmlspecialchars($value));
86
87
        }
88
89
90
91
        return $node;
92
    }
93
94
    /**
95
     * @param array $content
96
     * @return self
97
     * @codeCoverageIgnore
98
     */
99
    public function setContent($content = [])
100
    {
101
        $this->setResponseHeader(['Content-Type' => 'text/xml']);
102
103
        $xml = new \SimpleXMLElement('<?xml version="1.0"?><root></root>');
104
        $this->convertArrayToXml($xml, $content);
105
        $result = $xml->asXml();
106
107
        $this->content = $result;
0 ignored issues
show
Documentation Bug introduced by
It seems like $result of type string or false is incompatible with the declared type array of property $content.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
        return $this;
109
    }
110
111
}