Passed
Push — main ( 6fd589...d01f4c )
by Bingo
05:51
created

JsonUtil::asList()   C

Complexity

Conditions 12
Paths 13

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 34
rs 6.9666
cc 12
nc 13
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\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 && $converterOrlist instanceof JsonObjectConverter) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $converterOrlist does not exist. Did you maybe mean $converterOrValue?
Loading history...
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 asString(?array $properties = null): string
75
    {
76
        if (!empty($properties)) {
77
            return json_encode($properties);
78
        }
79
        return "";
80
    }
81
82
    public static function asPhpObject(\stdClass $jsonObject = null, JsonObjectConverter $converter = null)
83
    {
84
        if ($jsonObject !== null && $converter !== null) {
85
            return $converter->toObject($jsonObject);
86
        } else {
87
            return null;
88
        }
89
    }
90
91
    public static function asStringList($jsonObject = []): array
92
    {
93
        if (empty($jsonObject)) {
94
            return [];
95
        }
96
97
        $list = [];
98
        foreach ($jsonObject as $entry) {
99
            $stringValue = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $stringValue is dead and can be removed.
Loading history...
100
            try {
101
                $stringValue = json_encode($entry);
102
            } catch (\Exception $e) {
103
                //LOG.logJsonException(e);
104
            }
105
106
            if ($stringValue != null) {
107
                $list[] = $stringValue;
108
            }
109
        }
110
111
        return $list;
112
    }
113
114
    public static function asList($jsonArray = [], JsonObjectConverter $converter = null, $listSupplier = null)
115
    {
116
        if (empty($jsonArray) || $converter == null) {
117
            return [];
118
        }
119
120
        $list = null;
121
        if (is_string($listSupplier) && class_exists($listSupplier)) {
122
            $list = new $listSupplier();
123
        } else {
124
            $list = [];
125
        }
126
127
        foreach ($jsonArray as $element) {
128
            $jsonObject = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $jsonObject is dead and can be removed.
Loading history...
129
            try {
130
                $jsonObject = (object) $element;
131
            } 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...
132
                //LOG.logJsonException(e);
133
            }
134
135
            if ($jsonObject != null) {
136
                $rawObject = $converter->toObject($jsonObject);
137
                if ($rawObject != null) {
138
                    if (is_array($list)) {
139
                        $list[] = $rawObject;
140
                    } elseif ($list !== null && method_exists($list, 'add')) {
141
                        $list->add($rawObject);
142
                    }
143
                }
144
            }
145
        }
146
147
        return $list;
148
    }
149
150
    public static function getArray($json = null, string $memberName = null): array
151
    {
152
        if ($json != null && $json instanceof \stdClass && $memberName != null && property_exists($json, $memberName)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $memberName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
153
            return self::getArray($json->{$memberName});
154
        } elseif ($json != null && is_array($json)) {
155
            return $json;
156
        } else {
157
            return self::createArray();
158
        }
159
    }
160
161
    public static function getObject(\stdClass $json, ?string $memberName = null): \stdClass
162
    {
163
        if ($json != null && $memberName == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $memberName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
164
            return $json;
165
        }
166
        if ($json != null && $memberName != null && property_exists($json, $memberName)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $memberName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
167
            return $json->{$memberName};
168
        } else {
169
            return self::createObject();
170
        }
171
    }
172
173
    public static function createObject(): \stdClass
174
    {
175
        return new \stdClass();
176
    }
177
178
    public static function createArray(): array
179
    {
180
        return [];
181
    }
182
183
    public static function getBoolean(\stdClass $json = null, string $memberName = null): bool
184
    {
185
        if ($json !== null && $memberName !== null && property_exists($json, $memberName)) {
186
            try {
187
                return boolval(json_decode($json->{$memberName}));
188
            } catch (\Exception $e) {
189
                //LOG.logJsonException(e);
190
                return false;
191
            }
192
        } else {
193
            return false;
194
        }
195
    }
196
197
    public static function getString($json = null, string $memberName = null, string $defaultString = null): string
198
    {
199
        if (is_object($json)) {
200
            if ($json != null && $memberName != null && property_exists($json, $memberName)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $memberName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
201
                return self::getString($json->{$memberName});
202
            } else {
203
                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...
204
            }
205
        } else {
206
            try {
207
                return json_encode($json);
208
            } catch (\Exception $e) {
209
                return "";
210
            }
211
        }
212
    }
213
214
    public static function getInt(\stdClass $json = null, string $memberName = null): int
215
    {
216
        if ($json != null && $memberName != null && property_exists($json, $memberName)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $memberName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
217
            try {
218
                return intval($json->{$memberName});
219
            } catch (\Exception $e) {
220
                //LOG.logJsonException(e);
221
                return 0;
222
            }
223
        } else {
224
            return 0;
225
        }
226
    }
227
228
    public static function isNull(\stdClass $jsonObject = null, string $memberName = null): bool
229
    {
230
        if ($jsonObject != null && $memberName != null && property_exists($jsonObject, $memberName)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $memberName of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
231
            return $jsonObject->{$memberName} === null;
232
        } else {
233
            return false;
234
        }
235
    }
236
237
    public static function getLong(\stdClass $json = null, string $memberName = null): int
238
    {
239
        return self::getInt($json, $memberName);
240
    }
241
}
242