JsonUtil   F
last analyzed

Complexity

Total Complexity 88

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 88
eloc 119
dl 0
loc 255
rs 2
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A createObject() 0 3 1
A isNull() 0 6 4
C asList() 0 34 12
A addField() 0 6 4
A addNullField() 0 3 1
A getLong() 0 3 1
A createArray() 0 3 1
A addArrayField() 0 4 2
A asBytes() 0 3 1
A getString() 0 13 6
B getArray() 0 8 7
A asStringList() 0 21 5
B addListField() 0 13 8
A getObject() 0 9 6
A asObject() 0 14 5
A getBoolean() 0 11 5
A addFieldRawValue() 0 4 2
A asPhpObject() 0 6 3
A addDateField() 0 7 4
A getInt() 0 11 5
A addElement() 0 6 3
A asString() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like JsonUtil often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use JsonUtil, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Jabe\Impl\Util;
4
5
use Jabe\Impl\Json\JsonObjectConverter;
6
7
class JsonUtil
8
{
9
    public static function addFieldRawValue(\stdClass $jsonObject, string $memberName, $rawValue = null): void
10
    {
11
        if ($rawValue !== null) {
12
            $jsonObject->{$memberName} = json_decode($rawValue);
13
        }
14
    }
15
16
    public static function addNullField(\stdClass $jsonObject, string $name): void
17
    {
18
        $jsonObject->{$name} = null;
19
    }
20
21
    public static function addField(\stdClass $jsonObject, string $name, $converterOrValue = null, $value = null): void
22
    {
23
        if ($converterOrValue !== null && $converterOrValue instanceof JsonObjectConverter) {
24
            $jsonObject->{$name} = $converterOrValue->toJsonObject($value);
25
        } elseif ($converterOrValue !== null) {
26
            $jsonObject->{$name} = $converterOrValue;
27
        }
28
    }
29
30
    public static function addListField(\stdClass $jsonObject, string $name, $converterOrlist, $list = null): void
31
    {
32
        if ($converterOrlist !== null && is_array($converterOrlist)) {
33
            $jsonObject->{$name} = $converterOrlist;
34
        } elseif ($converterOrlist !== null && $converterOrlist instanceof JsonObjectConverter && $list !== null) {
35
            $arrayNode = [];
36
            foreach ($list as $item) {
37
                if ($item !== null) {
38
                    $jsonElement = $converterOrlist->toJsonObject($item);
39
                    $arrayNode[] = $jsonElement;
40
                }
41
            }
42
            $jsonObject->{$name} = $arrayNode;
43
        }
44
    }
45
46
    public static function addArrayField(\stdClass $jsonObject, string $name, array $array = null): void
47
    {
48
        if ($array !== null) {
49
            self::addListField($jsonObject, $name, $array);
50
        }
51
    }
52
53
    public static function addDateField(\stdClass $jsonObject, string $name, $date = null): void
54
    {
55
        if ($date !== null) {
56
            if (is_string($date)) {
57
                $jsonObject->{$name} = $date;
58
            } elseif ($date instanceof \DateTime) {
59
                $jsonObject->{$name} = $date->format('c');
60
            }
61
        }
62
    }
63
64
    public static function addElement(array &$jsonObject, JsonObjectConverter $converter, $value = null): void
65
    {
66
        if ($value !== null) {
67
            $jsonElement = $converter->toJsonObject($value);
68
            if ($jsonElement !== null) {
69
                $jsonObject[] = $jsonElement;
70
            }
71
        }
72
    }
73
74
    public static function asObject($data)
75
    {
76
        try {
77
            if (is_object($data)) {
78
                return $data;
79
            }
80
            if (is_string($data)) {
81
                return json_decode($data);
82
            }
83
            if (is_array($data)) {
84
                return (object) $data;
85
            }
86
        } catch (\Exception $e) {
87
            throw $e;
88
        }
89
    }
90
91
    public static function asBytes(\stdClass $jsonObject): string
92
    {
93
        return json_encode($jsonObject);
94
    }
95
96
    public static function asString(?array $properties = null): string
97
    {
98
        if (!empty($properties)) {
99
            return json_encode($properties);
100
        }
101
        return "";
102
    }
103
104
    public static function asPhpObject(\stdClass $jsonObject = null, JsonObjectConverter $converter = null)
105
    {
106
        if ($jsonObject !== null && $converter !== null) {
107
            return $converter->toObject($jsonObject);
108
        } else {
109
            return null;
110
        }
111
    }
112
113
    public static function asStringList($jsonObject = []): array
114
    {
115
        if (empty($jsonObject)) {
116
            return [];
117
        }
118
119
        $list = [];
120
        foreach ($jsonObject as $entry) {
121
            $stringValue = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $stringValue is dead and can be removed.
Loading history...
122
            try {
123
                $stringValue = json_encode($entry);
124
            } catch (\Exception $e) {
125
                //LOG.logJsonException(e);
126
            }
127
128
            if ($stringValue !== null) {
129
                $list[] = $stringValue;
130
            }
131
        }
132
133
        return $list;
134
    }
135
136
    public static function asList($jsonArray = [], JsonObjectConverter $converter = null, $listSupplier = null)
137
    {
138
        if (empty($jsonArray) || $converter === null) {
139
            return [];
140
        }
141
142
        $list = null;
143
        if (is_string($listSupplier) && class_exists($listSupplier)) {
144
            $list = new $listSupplier();
145
        } else {
146
            $list = [];
147
        }
148
149
        foreach ($jsonArray as $element) {
150
            $jsonObject = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $jsonObject is dead and can be removed.
Loading history...
151
            try {
152
                $jsonObject = (object) $element;
153
            } catch (\Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
154
                //LOG.logJsonException(e);
155
            }
156
157
            if ($jsonObject !== null) {
158
                $rawObject = $converter->toObject($jsonObject);
159
                if ($rawObject !== null) {
160
                    if (is_array($list)) {
161
                        $list[] = $rawObject;
162
                    } elseif ($list !== null && method_exists($list, 'add')) {
163
                        $list->add($rawObject);
164
                    }
165
                }
166
            }
167
        }
168
169
        return $list;
170
    }
171
172
    public static function getArray($json = null, string $memberName = null): array
173
    {
174
        if ($json !== null && $json instanceof \stdClass && $memberName !== null && property_exists($json, $memberName)) {
175
            return self::getArray($json->{$memberName});
176
        } elseif ($json !== null && is_array($json)) {
177
            return $json;
178
        } else {
179
            return self::createArray();
180
        }
181
    }
182
183
    public static function getObject(\stdClass $json, ?string $memberName = null): \stdClass
184
    {
185
        if ($json !== null && $memberName === null) {
186
            return $json;
187
        }
188
        if ($json !== null && $memberName !== null && property_exists($json, $memberName)) {
189
            return $json->{$memberName};
190
        } else {
191
            return self::createObject();
192
        }
193
    }
194
195
    public static function createObject(): \stdClass
196
    {
197
        return new \stdClass();
198
    }
199
200
    public static function createArray(): array
201
    {
202
        return [];
203
    }
204
205
    public static function getBoolean(\stdClass $json = null, string $memberName = null): bool
206
    {
207
        if ($json !== null && $memberName !== null && property_exists($json, $memberName)) {
208
            try {
209
                return boolval(json_decode($json->{$memberName}));
210
            } catch (\Exception $e) {
211
                //LOG.logJsonException(e);
212
                return false;
213
            }
214
        } else {
215
            return false;
216
        }
217
    }
218
219
    public static function getString($json = null, string $memberName = null, string $defaultString = null): string
220
    {
221
        if (is_object($json)) {
222
            if ($json !== null && $memberName !== null && property_exists($json, $memberName)) {
223
                return self::getString($json->{$memberName});
224
            } else {
225
                return $defaultString;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $defaultString could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
226
            }
227
        } else {
228
            try {
229
                return json_encode($json);
230
            } catch (\Exception $e) {
231
                return "";
232
            }
233
        }
234
    }
235
236
    public static function getInt(\stdClass $json = null, string $memberName = null): int
237
    {
238
        if ($json !== null && $memberName !== null && property_exists($json, $memberName)) {
239
            try {
240
                return intval($json->{$memberName});
241
            } catch (\Exception $e) {
242
                //LOG.logJsonException(e);
243
                return 0;
244
            }
245
        } else {
246
            return 0;
247
        }
248
    }
249
250
    public static function isNull(\stdClass $jsonObject = null, string $memberName = null): bool
251
    {
252
        if ($jsonObject !== null && $memberName !== null && property_exists($jsonObject, $memberName)) {
253
            return $jsonObject->{$memberName} === null;
254
        } else {
255
            return false;
256
        }
257
    }
258
259
    public static function getLong(\stdClass $json = null, string $memberName = null): int
260
    {
261
        return self::getInt($json, $memberName);
262
    }
263
}
264