Passed
Pull Request — master (#75)
by
unknown
11:38
created

XMLConverter::getFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Stream\Data;
6
7
use Psr\Http\Message\MessageInterface;
8
9
class XMLConverter implements Converter
10
{
11
    public static function getFormat(): string
12
    {
13
        return 'text/xml';
14
    }
15
    public function setHeaders(MessageInterface $message): MessageInterface
16
    {
17
        return $message->withHeader('Content-Type', static::getFormat());
18
    }
19
    public function convert($data, array $params = []): string
20
    {
21
        return $this->xml_encode($data);
22
    }
23
24
    private function xml_encode($data, \DOMNode $domElement = null, \DOMDocument $DOMDocument = null): string
25
    {
26
        if ($DOMDocument === null) {
27
            $DOMDocument = new \DOMDocument();
28
            $DOMDocument->formatOutput = true;
29
            $this->xml_encode(['data' => $data], $DOMDocument, $DOMDocument);
30
            return $DOMDocument->saveXML();
31
        }
32
        // To cope with embedded objects
33
        if (is_object($data)) {
34
            $data = get_object_vars($data);
35
        }
36
        if (is_array($data)) {
37
            foreach ($data as $index => $mixedElement) {
38
                if (is_int($index)) {
39
                    if ($index === 0) {
40
                        $node = $domElement;
41
                    } else {
42
                        $node = $DOMDocument->createElement($domElement->tagName);
43
                        $domElement->parentNode->appendChild($node);
44
                    }
45
                } else {
46
                    $plural = $DOMDocument->createElement($index);
47
                    $domElement->appendChild($plural);
0 ignored issues
show
Bug introduced by
The method appendChild() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
                    $domElement->/** @scrutinizer ignore-call */ 
48
                                 appendChild($plural);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
                    $node = $plural;
49
                    if (!(rtrim($index, 's') === $index)) {
50
                        $singular = $DOMDocument->createElement(rtrim($index, 's'));
51
                        $plural->appendChild($singular);
52
                        $node = $singular;
53
                    }
54
                }
55
56
                $this->xml_encode($mixedElement, $node, $DOMDocument);
57
            }
58
        } else {
59
            $data = is_bool($data) ? ($data ? 'true' : 'false') : $data;
60
            $domElement->appendChild($DOMDocument->createTextNode($data));
61
        }
62
        return '';
63
    }
64
}
65