Passed
Push — master ( daf770...06222b )
by Dominik
01:58
created

XmlTransformer   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 121
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 121
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

6 Methods

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