Complex classes like JsonDataHandler 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 JsonDataHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class JsonDataHandler |
||
16 | { |
||
17 | const DATA_TYPE_ARRAY = 'array'; |
||
18 | |||
19 | const DATA_TYPE_OBJECT = 'object'; |
||
20 | |||
21 | const DATA_TYPE_USE_FLAGS = 'flags'; |
||
22 | |||
23 | const NO_ERROR_MSG = 'No error'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $data_type; |
||
29 | |||
30 | /** |
||
31 | * @var array|stdClass |
||
32 | */ |
||
33 | private $decoded_data; |
||
34 | |||
35 | /** |
||
36 | * JSON_BIGINT_AS_STRING, |
||
37 | * JSON_INVALID_UTF8_IGNORE, |
||
38 | * JSON_INVALID_UTF8_SUBSTITUTE, |
||
39 | * JSON_OBJECT_AS_ARRAY, |
||
40 | * JSON_THROW_ON_ERROR |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | private $decode_flags; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $depth; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $encoded_data; |
||
55 | |||
56 | /** |
||
57 | * JSON_FORCE_OBJECT, |
||
58 | * JSON_HEX_QUOT, |
||
59 | * JSON_HEX_TAG, |
||
60 | * JSON_HEX_AMP, |
||
61 | * JSON_HEX_APOS, |
||
62 | * JSON_INVALID_UTF8_IGNORE, |
||
63 | * JSON_INVALID_UTF8_SUBSTITUTE, |
||
64 | * JSON_NUMERIC_CHECK, |
||
65 | * JSON_PARTIAL_OUTPUT_ON_ERROR, |
||
66 | * JSON_PRESERVE_ZERO_FRACTION, |
||
67 | * JSON_PRETTY_PRINT, |
||
68 | * JSON_UNESCAPED_LINE_TERMINATORS, |
||
69 | * JSON_UNESCAPED_SLASHES, |
||
70 | * JSON_UNESCAPED_UNICODE, |
||
71 | * JSON_THROW_ON_ERROR. |
||
72 | * |
||
73 | * @var int |
||
74 | */ |
||
75 | private $encode_flags; |
||
76 | |||
77 | /** |
||
78 | * @var int |
||
79 | */ |
||
80 | private $last_error_code = JSON_ERROR_NONE; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | private $last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
||
86 | |||
87 | |||
88 | /** |
||
89 | * JsonDataHandler constructor. |
||
90 | */ |
||
91 | public function __construct() |
||
103 | |||
104 | |||
105 | /** |
||
106 | * set $data_type, $decode_flags, $encode_flags, and depth all in one shot |
||
107 | * |
||
108 | * @param string $data_type |
||
109 | * @param int $decode_flags |
||
110 | * @param int $encode_flags |
||
111 | * @param int $depth |
||
112 | */ |
||
113 | public function configure( |
||
124 | |||
125 | |||
126 | /** |
||
127 | * @param string $data_type |
||
128 | */ |
||
129 | public function setDataType(string $data_type): void |
||
137 | |||
138 | |||
139 | /** |
||
140 | * One or more Bitmask values: |
||
141 | * JSON_BIGINT_AS_STRING, |
||
142 | * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
||
143 | * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
||
144 | * JSON_OBJECT_AS_ARRAY, |
||
145 | * JSON_THROW_ON_ERROR PHP >= 7.3 |
||
146 | * |
||
147 | * pass multiple values separated with | |
||
148 | * ex: JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY |
||
149 | * |
||
150 | * @param int $decode_flags |
||
151 | */ |
||
152 | public function setDecodeFlags(int $decode_flags): void |
||
165 | |||
166 | |||
167 | /** |
||
168 | * @param int $depth |
||
169 | */ |
||
170 | public function setDepth(int $depth): void |
||
175 | |||
176 | |||
177 | /** |
||
178 | * One or more Bitmask values: |
||
179 | * JSON_FORCE_OBJECT, |
||
180 | * JSON_HEX_QUOT, |
||
181 | * JSON_HEX_TAG, |
||
182 | * JSON_HEX_AMP, |
||
183 | * JSON_HEX_APOS, |
||
184 | * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
||
185 | * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
||
186 | * JSON_NUMERIC_CHECK, |
||
187 | * JSON_PARTIAL_OUTPUT_ON_ERROR, |
||
188 | * JSON_PRESERVE_ZERO_FRACTION, |
||
189 | * JSON_PRETTY_PRINT, |
||
190 | * JSON_UNESCAPED_LINE_TERMINATORS, |
||
191 | * JSON_UNESCAPED_SLASHES, |
||
192 | * JSON_UNESCAPED_UNICODE, |
||
193 | * JSON_THROW_ON_ERROR. PHP >= 7.3 |
||
194 | * |
||
195 | * pass multiple values separated with | |
||
196 | * ex: JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR |
||
197 | * |
||
198 | * @param int $encode_flags |
||
199 | */ |
||
200 | public function setEncodeFlags(int $encode_flags): void |
||
223 | |||
224 | |||
225 | /** |
||
226 | * @return bool|null |
||
227 | */ |
||
228 | private function asAssociative(): ?bool |
||
240 | |||
241 | |||
242 | /** |
||
243 | * @param array|string $json |
||
244 | * @return array|mixed|stdClass |
||
245 | */ |
||
246 | public function decodeJson($json) |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @param $data |
||
264 | * @return string |
||
265 | */ |
||
266 | public function encodeData($data): string |
||
280 | |||
281 | |||
282 | /** |
||
283 | * @return array|stdClass |
||
284 | */ |
||
285 | public function getDecodedData() |
||
289 | |||
290 | |||
291 | /** |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getEncodedData(): string |
||
298 | |||
299 | |||
300 | /** |
||
301 | * @param bool $reset |
||
302 | * @return int |
||
303 | */ |
||
304 | public function getLastErrorCode(bool $reset = false): int |
||
312 | |||
313 | |||
314 | /** |
||
315 | * @param bool $reset |
||
316 | * @return string |
||
317 | */ |
||
318 | public function getLastErrorMessage(bool $reset = false): string |
||
326 | |||
327 | |||
328 | /** |
||
329 | * @param array|string $maybe_json |
||
330 | * @return bool |
||
331 | */ |
||
332 | public function isJson($maybe_json): bool |
||
340 | |||
341 | |||
342 | /** |
||
343 | * @since $VID:$ |
||
344 | */ |
||
345 | public function resetErrors() |
||
350 | } |
||
351 |