Test Failed
Pull Request — master (#27)
by Dominik
06:22
created

JsonxTypeDecoder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 127
Duplicated Lines 20.47 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.78%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 26
loc 127
ccs 44
cts 45
cp 0.9778
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 4 1
A decode() 0 10 2
B decodeNode() 0 18 7
A decodeObjectNode() 13 13 3
A decodeArrayNode() 13 13 3
A decodeBooleanNode() 0 4 1
A decodeStringNode() 0 4 1
A decodeNumberNode() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Decoder;
6
7
use Chubbyphp\Deserialization\DeserializerRuntimeException;
8
9
/**
10
 * @link https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.6.0/com.ibm.dp.doc/json_jsonx.html
11
 */
12
final class JsonxTypeDecoder implements TypeDecoderInterface
13
{
14
    const DATATYPE_OBJECT = 'object';
15
    const DATATYPE_ARRAY = 'array';
16
    const DATATYPE_BOOLEAN = 'boolean';
17
    const DATATYPE_STRING = 'string';
18
    const DATATYPE_NUMBER = 'number';
19
    const DATATYPE_NULL = 'null';
20
21
    /**
22
     * @return string
23
     */
24 2
    public function getContentType(): string
25
    {
26 2
        return 'application/jsonx';
27
    }
28
29
    /**
30
     * @param string $data
31
     *
32
     * @return array
33
     *
34
     * @throws DeserializerRuntimeException
35
     */
36 3
    public function decode(string $data): array
37
    {
38 3
        $document = new \DOMDocument();
39
40 3
        if (!@$document->loadXML($data)) {
41 1
            throw DeserializerRuntimeException::createNotParsable($this->getContentType());
42
        }
43
44 2
        return $this->decodeNode($document->documentElement);
45
    }
46
47
    /**
48
     * @param \DOMNode $node
49
     * @return array|bool|string|int|float|null
50
     */
51 2
    private function decodeNode(\DOMNode $node)
52
    {
53 2
        $nodeType = substr($node->nodeName, 5);
54
55 2
        if (self::DATATYPE_OBJECT === $nodeType) {
56 2
            return $this->decodeObjectNode($node);
57 2
        } elseif (self::DATATYPE_ARRAY === $nodeType) {
58 1
            return $this->decodeArrayNode($node);
59 2
        } elseif (self::DATATYPE_BOOLEAN === $nodeType) {
60 2
            return $this->decodeBooleanNode($node);
61 2
        } elseif (self::DATATYPE_STRING === $nodeType) {
62 2
            return $this->decodeStringNode($node);
63 2
        } elseif (self::DATATYPE_NUMBER === $nodeType) {
64 2
            return $this->decodeNumberNode($node);
65 1
        } elseif (self::DATATYPE_NULL === $nodeType) {
66 1
            return null;
67
        }
68
    }
69
70
    /**
71
     * @param \DOMNode $node
72
     * @return array
73
     */
74 2 View Code Duplication
    private function decodeObjectNode(\DOMNode $node): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76 2
        $data = [];
77 2
        foreach ($node->childNodes as $childNode) {
78 2
            if ($childNode instanceof \DOMText) {
79 2
                continue;
80
            }
81
82 2
            $data[$childNode->getAttribute('name')] = $this->decodeNode($childNode);
83
        }
84
85 2
        return $data;
86
    }
87
88
    /**
89
     * @param \DOMNode $node
90
     * @return array
91
     */
92 1 View Code Duplication
    private function decodeArrayNode(\DOMNode $node): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94 1
        $data = [];
95 1
        foreach ($node->childNodes as $childNode) {
96 1
            if ($childNode instanceof \DOMText) {
97 1
                continue;
98
            }
99
100 1
            $data[] = $this->decodeNode($childNode);
101
        }
102
103 1
        return $data;
104
    }
105
106
    /**
107
     * @param \DOMNode $node
108
     * @return bool
109
     */
110 2
    private function decodeBooleanNode(\DOMNode $node): bool
111
    {
112 2
        return $node->nodeValue === 'true';
113
    }
114
115
    /**
116
     * @param \DOMNode $node
117
     * @return string
118
     */
119 2
    private function decodeStringNode(\DOMNode $node): string
120
    {
121 2
        return html_entity_decode($node->nodeValue, ENT_COMPAT | ENT_XML1, 'UTF-8');
122
    }
123
124
    /**
125
     * @param \DOMNoDOMNodedeList $node
126
     * @return int|float
127
     */
128 2
    private function decodeNumberNode(\DOMNode $node)
129
    {
130 2
        $value = $node->nodeValue;
131
132 2
        if ($value === (string) (int) $value) {
133 2
            return (int) $value;
134
        }
135
136 2
        return (float) $value;
137
    }
138
}
139