Passed
Push — master ( 95e744...8d5d5e )
by Dominik
01:52
created

XmlTypeDecoder   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 111
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 4 1
A decode() 0 10 2
C transformType() 0 40 11
A getKey() 0 13 3
A getValue() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Decoder;
6
7
use Chubbyphp\Deserialization\DeserializerRuntimeException;
8
9
final class XmlTypeDecoder implements TypeDecoderInterface
10
{
11
    /**
12
     * @return string
13
     */
14 2
    public function getContentType(): string
15
    {
16 2
        return 'application/xml';
17
    }
18
19
    /**
20
     * @param string $data
21
     *
22
     * @return array
23
     *
24
     * @throws DeserializerRuntimeException
25
     */
26 3
    public function decode(string $data): array
27
    {
28 3
        $document = new \DOMDocument();
29
30 3
        if (!@$document->loadXML($data)) {
31 1
            throw DeserializerRuntimeException::createNotParsable($this->getContentType());
32
        }
33
34 2
        return $this->transformType($document->getElementsByTagName('object')->item(0));
0 ignored issues
show
Compatibility introduced by
$document->getElementsBy...Name('object')->item(0) 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...
35
    }
36
37
    /**
38
     * @param \DOMElement $node
39
     *
40
     * @return array
41
     */
42 2
    private function transformType(\DOMElement $node): array
43
    {
44 2
        $data = [];
45
46 2
        $childNodes = [];
47 2
        foreach ($node->childNodes as $childNode) {
48 2
            if ($childNode instanceof \DOMElement) {
49 2
                $childNodes[] = $childNode;
50
            }
51
        }
52
53 2
        foreach ($childNodes as $childNode) {
54 2
            if (0 === $childNode->childNodes->length) {
55 1
                $data[$this->getKey($childNode)] = null;
56
57 1
                continue;
58
            }
59
60 2
            if (1 === $childNode->childNodes->length) {
61 2
                $data[$this->getKey($childNode)] = $this->getValue($childNode);
62
63 2
                continue;
64
            }
65
66 2
            if ('object' === $childNode->nodeName
67 2
                && $childNode->hasAttribute('type')
68 2
                && !$childNode->hasAttribute('key')
69
            ) {
70 1
                return $this->transformType($childNode);
71
            }
72
73 2
            $data[$this->getKey($childNode)] = $this->transformType($childNode);
74
        }
75
76 2
        if ('object' === $node->nodeName && $node->hasAttribute('type')) {
77 2
            $data['_type'] = $node->getAttribute('type');
78
        }
79
80 2
        return $data;
81
    }
82
83
    /**
84
     * @param \DOMElement $node
85
     *
86
     * @return string|int
87
     */
88 2
    private function getKey(\DOMElement $node)
89
    {
90 2
        if ($node->hasAttribute('key')) {
91 2
            return (int) $node->getAttribute('key');
92
        }
93
94 2
        $name = $node->nodeName;
95 2
        if (0 === strpos($name, 'meta-')) {
96 1
            $name = '_'.substr($name, 5);
97
        }
98
99 2
        return $name;
100
    }
101
102
    /**
103
     * @param \DOMElement $node
104
     *
105
     * @return bool|string
106
     */
107 2
    private function getValue(\DOMElement $node)
108
    {
109 2
        $value = $node->nodeValue;
110
111 2
        if ('boolean' === $type = $node->getAttribute('type')) {
112 2
            return 'true' === $value;
113
        }
114
115 2
        settype($value, $type);
116
117 2
        return $value;
118
    }
119
}
120