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 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $data_type; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array|stdClass |
||
| 30 | */ |
||
| 31 | private $decoded_data; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * JSON_BIGINT_AS_STRING, |
||
| 35 | * JSON_INVALID_UTF8_IGNORE, |
||
| 36 | * JSON_INVALID_UTF8_SUBSTITUTE, |
||
| 37 | * JSON_OBJECT_AS_ARRAY, |
||
| 38 | * JSON_THROW_ON_ERROR |
||
| 39 | * |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | private $decode_flags; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $depth; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $encoded_data; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * JSON_FORCE_OBJECT, |
||
| 56 | * JSON_HEX_QUOT, |
||
| 57 | * JSON_HEX_TAG, |
||
| 58 | * JSON_HEX_AMP, |
||
| 59 | * JSON_HEX_APOS, |
||
| 60 | * JSON_INVALID_UTF8_IGNORE, |
||
| 61 | * JSON_INVALID_UTF8_SUBSTITUTE, |
||
| 62 | * JSON_NUMERIC_CHECK, |
||
| 63 | * JSON_PARTIAL_OUTPUT_ON_ERROR, |
||
| 64 | * JSON_PRESERVE_ZERO_FRACTION, |
||
| 65 | * JSON_PRETTY_PRINT, |
||
| 66 | * JSON_UNESCAPED_LINE_TERMINATORS, |
||
| 67 | * JSON_UNESCAPED_SLASHES, |
||
| 68 | * JSON_UNESCAPED_UNICODE, |
||
| 69 | * JSON_THROW_ON_ERROR. |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | private $encode_flags; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | private $last_error_code; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private $last_error_msg; |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * JsonDataHandler constructor. |
||
| 88 | */ |
||
| 89 | public function __construct() |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * set $data_type, $decode_flags, $encode_flags, and depth all in one shot |
||
| 105 | * |
||
| 106 | * @param string $data_type |
||
| 107 | * @param int $decode_flags |
||
| 108 | * @param int $encode_flags |
||
| 109 | * @param int $depth |
||
| 110 | */ |
||
| 111 | public function configure( |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $data_type |
||
| 126 | */ |
||
| 127 | public function setDataType(string $data_type): void |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * One or more Bitmask values: |
||
| 139 | * JSON_BIGINT_AS_STRING, |
||
| 140 | * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
||
| 141 | * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
||
| 142 | * JSON_OBJECT_AS_ARRAY, |
||
| 143 | * JSON_THROW_ON_ERROR PHP >= 7.3 |
||
| 144 | * |
||
| 145 | * pass multiple values separated with | |
||
| 146 | * ex: JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY |
||
| 147 | * |
||
| 148 | * @param int $decode_flags |
||
| 149 | */ |
||
| 150 | public function setDecodeFlags(int $decode_flags): void |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * @param int $depth |
||
| 167 | */ |
||
| 168 | public function setDepth(int $depth): void |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * One or more Bitmask values: |
||
| 177 | * JSON_FORCE_OBJECT, |
||
| 178 | * JSON_HEX_QUOT, |
||
| 179 | * JSON_HEX_TAG, |
||
| 180 | * JSON_HEX_AMP, |
||
| 181 | * JSON_HEX_APOS, |
||
| 182 | * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
||
| 183 | * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
||
| 184 | * JSON_NUMERIC_CHECK, |
||
| 185 | * JSON_PARTIAL_OUTPUT_ON_ERROR, |
||
| 186 | * JSON_PRESERVE_ZERO_FRACTION, |
||
| 187 | * JSON_PRETTY_PRINT, |
||
| 188 | * JSON_UNESCAPED_LINE_TERMINATORS, |
||
| 189 | * JSON_UNESCAPED_SLASHES, |
||
| 190 | * JSON_UNESCAPED_UNICODE, |
||
| 191 | * JSON_THROW_ON_ERROR. PHP >= 7.3 |
||
| 192 | * |
||
| 193 | * pass multiple values separated with | |
||
| 194 | * ex: JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR |
||
| 195 | * |
||
| 196 | * @param int $encode_flags |
||
| 197 | */ |
||
| 198 | public function setEncodeFlags(int $encode_flags): void |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @return bool|null |
||
| 225 | */ |
||
| 226 | private function asAssociative() |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $json |
||
| 242 | * @return array|mixed|stdClass |
||
| 243 | */ |
||
| 244 | public function decodeJson(string $json) |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $data |
||
| 255 | * @return false|string |
||
| 256 | */ |
||
| 257 | public function encodeData($data) |
||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * @return array|stdClass |
||
| 268 | */ |
||
| 269 | public function getDecodedData() |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getEncodedData(): string |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * @param false $reset |
||
| 286 | * @return int |
||
| 287 | */ |
||
| 288 | public function getLastErrorCode($reset = false) |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @param false $reset |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function getLastErrorMessage($reset = false) |
||
| 310 | } |
||
| 311 |