Passed
Push — master ( f03a56...8d49be )
by Dominik
02:42
created

XmlTransformer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 95
ccs 36
cts 36
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 7 1
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
     * @param string $string
11
     *
12
     * @return array
13
     */
14 1
    public function transform(string $string): array
15
    {
16 1
        $document = new \DOMDocument();
17 1
        $document->loadXML($string);
18
19 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...
20
    }
21
22
    /**
23
     * @param \DOMElement $node
24
     *
25
     * @return array
26
     */
27 1
    private function transformType(\DOMElement $node): array
28
    {
29 1
        $data = [];
30
31 1
        $childNodes = [];
32 1
        foreach ($node->childNodes as $childNode) {
33 1
            if ($childNode instanceof \DOMElement) {
34 1
                $childNodes[] = $childNode;
35
            }
36
        }
37
38 1
        foreach ($childNodes as $childNode) {
39 1
            if (0 === $childNode->childNodes->length) {
40 1
                $data[$this->getKey($childNode)] = null;
41
42 1
                continue;
43
            }
44
45 1
            if (1 === $childNode->childNodes->length) {
46 1
                $data[$this->getKey($childNode)] = $this->getValue($childNode);
47
48 1
                continue;
49
            }
50
51 1
            if ('meta-type' === $childNode->nodeName && !$childNode->hasAttribute('key')) {
52 1
                return $this->transformType($childNode);
53
            }
54
55 1
            $data[$this->getKey($childNode)] = $this->transformType($childNode);
56
        }
57
58 1
        if ('meta-type' === $node->nodeName && $node->hasAttribute('value')) {
59 1
            $data['_type'] = $node->getAttribute('value');
60
        }
61
62 1
        return $data;
63
    }
64
65
    /**
66
     * @param \DOMElement $node
67
     *
68
     * @return string|int
69
     */
70 1
    private function getKey(\DOMElement $node)
71
    {
72 1
        if ($node->hasAttribute('key')) {
73 1
            return (int) $node->getAttribute('key');
74
        }
75
76 1
        $name = $node->nodeName;
77 1
        if (0 === strpos($name, 'meta-')) {
78 1
            $name = '_'.substr($name, 5);
79
        }
80
81 1
        return $name;
82
    }
83
84
    /**
85
     * @param \DOMElement $node
86
     *
87
     * @return bool|string
88
     */
89 1
    private function getValue(\DOMElement $node)
90
    {
91 1
        $value = $node->nodeValue;
92
93 1
        if ('boolean' === $type = $node->getAttribute('type')) {
94 1
            return 'true' === $value;
95
        }
96
97 1
        settype($value, $type);
98
99 1
        return $value;
100
    }
101
}
102