Total Complexity | 44 |
Total Lines | 231 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like json 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 json, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class json |
||
10 | { |
||
11 | public $result; |
||
12 | private $file; |
||
13 | |||
14 | public static function headerJSON() |
||
17 | } |
||
18 | |||
19 | public static $static; |
||
20 | |||
21 | /** |
||
22 | * chaining. |
||
23 | * |
||
24 | * @return json |
||
25 | */ |
||
26 | public static function init() |
||
27 | { |
||
28 | if (!self::$static) { |
||
29 | self::$static = new self(); |
||
30 | } |
||
31 | |||
32 | return self::$static; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Load JSON. |
||
37 | */ |
||
38 | public function load(string $file, bool $assoc = false) |
||
39 | { |
||
40 | if (is_file($file)) { |
||
41 | $this->file = $file; |
||
42 | $this->result = json_decode(file_get_contents($file), $assoc); |
||
43 | } |
||
44 | |||
45 | return $this; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Save JSON from `load(string $file, bool $assoc = false)`. |
||
50 | * |
||
51 | * @return json |
||
52 | */ |
||
53 | public function save() |
||
54 | { |
||
55 | if ($this->file && file_exists($this->file)) { |
||
56 | file_put_contents($this->file, json_encode($this->result, JSON_PRETTY_PRINT)); |
||
57 | } |
||
58 | |||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Force Assoc. |
||
64 | * |
||
65 | * @param object|array $arr |
||
66 | * |
||
67 | * @return json_decode |
||
|
|||
68 | */ |
||
69 | public static function assoc($arr) |
||
70 | { |
||
71 | $str = json_encode($arr); |
||
72 | |||
73 | return json_decode($str, true); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * JSON formatter. |
||
78 | * |
||
79 | * @param mixed $data |
||
80 | * @param bool $header |
||
81 | * @param bool $print |
||
82 | */ |
||
83 | public static function json($data = [], $header = true, $print = true) |
||
84 | { |
||
85 | if ($header && !headers_sent()) { |
||
86 | header('Content-Type: application/json'); |
||
87 | } |
||
88 | if ('string' == strtolower(gettype($data)) && self::is_json($data)) { |
||
89 | $data = json_decode($data, true); |
||
90 | } |
||
91 | |||
92 | $json = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
||
93 | |||
94 | if ($print) { |
||
95 | echo $json; |
||
96 | } else { |
||
97 | return $json; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * beautify JSON. |
||
103 | * |
||
104 | * @param string|array|object|stdClass $data |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public static function beautify($data) |
||
109 | { |
||
110 | if (ctype_alnum($data) && is_string($data)) { |
||
111 | $is_json = self::is_json($data); |
||
112 | if ($is_json) { |
||
113 | $data = json_decode($data); |
||
114 | } else { |
||
115 | throw new \MVC\Exception("INVALID JSON (\JSON\json::beautify)", 1); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Validate json string. |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public static function is_json(string $string) |
||
128 | { |
||
129 | json_decode($string); |
||
130 | |||
131 | return JSON_ERROR_NONE == json_last_error(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * JSON decode with verification. |
||
136 | * |
||
137 | * @param string $str |
||
138 | */ |
||
139 | public static function jsond($str, $callback = null) |
||
140 | { |
||
141 | if (self::is_json($str)) { |
||
142 | return json_decode($str); |
||
143 | } elseif (is_callable($callback)) { |
||
144 | if (!self::is_json($str)) { |
||
145 | return call_user_func($callback, $str); |
||
146 | } else { |
||
147 | return call_user_func($callback, json_decode($str)); |
||
148 | } |
||
149 | } else { |
||
150 | throw new Exception('Not valid JSON string', 1); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * JSON error explanator. |
||
156 | */ |
||
157 | public static function json_last_error_e() |
||
158 | { |
||
159 | switch (json_last_error()) { |
||
160 | case JSON_ERROR_NONE: |
||
161 | echo ' - No errors'; |
||
162 | break; |
||
163 | case JSON_ERROR_DEPTH: |
||
164 | echo ' - Maximum stack depth exceeded'; |
||
165 | break; |
||
166 | case JSON_ERROR_STATE_MISMATCH: |
||
167 | echo ' - Underflow or the modes mismatch'; |
||
168 | break; |
||
169 | case JSON_ERROR_CTRL_CHAR: |
||
170 | echo ' - Unexpected control character found'; |
||
171 | break; |
||
172 | case JSON_ERROR_SYNTAX: |
||
173 | echo ' - Syntax error, malformed JSON'; |
||
174 | break; |
||
175 | case JSON_ERROR_UTF8: |
||
176 | echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; |
||
177 | break; |
||
178 | default: |
||
179 | echo ' - Unknown error'; |
||
180 | break; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * json_decode default assoc. |
||
186 | * |
||
187 | * @param bool $assoc |
||
188 | * |
||
189 | * @return \json_decode |
||
190 | */ |
||
191 | public static function json_decode(string $str, $err_callback = null, $assoc = true) |
||
192 | { |
||
193 | if (!self::isJson($str)) { |
||
194 | if (ctype_alnum($err_callback)) { |
||
195 | return $err_callback; |
||
196 | } else { |
||
197 | throw new Exception('str must be a valid JSON format, instead of ' . gettype($str), 1); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | return json_decode($str, $assoc); |
||
202 | } |
||
203 | |||
204 | public static function jsonDecode($input) |
||
233 | } |
||
234 | |||
235 | public static function isJson($string) |
||
236 | { |
||
237 | json_decode($string); |
||
238 | |||
239 | return JSON_ERROR_NONE == json_last_error(); |
||
240 | } |
||
241 | } |
||
242 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths