Complex classes like JSend 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 JSend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | final class JSend implements JsonSerializable |
||
| 40 | { |
||
| 41 | const STATUS_SUCCESS = 'success'; |
||
| 42 | |||
| 43 | const STATUS_ERROR = 'error'; |
||
| 44 | |||
| 45 | const STATUS_FAIL = 'fail'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * JSend status. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $status; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * JSend Data. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $data; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * JSend Error Message. |
||
| 63 | * |
||
| 64 | * @var string|null |
||
| 65 | */ |
||
| 66 | private $errorMessage; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * JSend Error Code. |
||
| 70 | * |
||
| 71 | * @var int|null |
||
| 72 | */ |
||
| 73 | private $errorCode; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns a new instance from a JSON string. |
||
| 77 | * |
||
| 78 | * @throws Exception If the string can not be decode |
||
| 79 | */ |
||
| 80 | 9 | public static function fromJSON($json, int $depth = 512, int $options = 0): self |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Returns a new instance from an array. |
||
| 100 | */ |
||
| 101 | 6 | public static function fromArray(array $arr): self |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Returns a successful JSend object with the specified data. |
||
| 108 | * |
||
| 109 | * @param null|mixed $data |
||
| 110 | */ |
||
| 111 | 102 | public static function success($data = null): self |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Returns a failed JSend object with the specified data. |
||
| 118 | * |
||
| 119 | * @param null|mixed $data |
||
| 120 | */ |
||
| 121 | 3 | public static function fail($data = null): self |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Returns a error JSend object with the specified error message and error code. |
||
| 128 | * |
||
| 129 | * @param null|mixed $data |
||
| 130 | */ |
||
| 131 | 15 | public static function error($errorMessage, int $errorCode = null, $data = null): self |
|
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | 3 | public static function __set_state(array $prop) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * New Instance. |
||
| 146 | * |
||
| 147 | * @param null|mixed $data |
||
| 148 | * @param null|mixed $errorMessage |
||
| 149 | */ |
||
| 150 | 102 | private function __construct( |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Filter and Validate the JSend Status. |
||
| 163 | * |
||
| 164 | * @throws Exception If the status value does not conform to JSend Spec. |
||
| 165 | */ |
||
| 166 | 102 | private function filterStatus(string $status): string |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Filter and Validate the JSend Data. |
||
| 178 | * |
||
| 179 | * @param mixed $data The data can be |
||
| 180 | * <ul> |
||
| 181 | * <li>An Array |
||
| 182 | * <li>A JsonSerializable object |
||
| 183 | * <li>null |
||
| 184 | * </ul> |
||
| 185 | * |
||
| 186 | * @throws Exception If the input does not conform to one of the valid type |
||
| 187 | */ |
||
| 188 | 102 | private function filterData($data): array |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Filter and Validate the JSend Error properties. |
||
| 211 | */ |
||
| 212 | 102 | private function filterError($errorMessage, int $errorCode = null): array |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Validate a string. |
||
| 223 | * |
||
| 224 | * @throws Exception If the data value is not a empty string |
||
| 225 | */ |
||
| 226 | 15 | private function filterErrorMessage($str): string |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the status. |
||
| 242 | */ |
||
| 243 | 42 | public function getStatus(): string |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Returns the data. |
||
| 250 | */ |
||
| 251 | 39 | public function getData(): array |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the error message. |
||
| 258 | * |
||
| 259 | * @return null|string |
||
| 260 | */ |
||
| 261 | 15 | public function getErrorMessage() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the error code. |
||
| 268 | * |
||
| 269 | * @return null|int |
||
| 270 | */ |
||
| 271 | 18 | public function getErrorCode() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Returns whether the status is success. |
||
| 278 | */ |
||
| 279 | 9 | public function isSuccess(): bool |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Returns whether the status is fail. |
||
| 286 | */ |
||
| 287 | 9 | public function isFail(): bool |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns whether the status is error. |
||
| 294 | */ |
||
| 295 | 15 | public function isError(): bool |
|
| 299 | |||
| 300 | /** |
||
| 301 | * {@inheritdoc} |
||
| 302 | */ |
||
| 303 | 9 | public function jsonSerialize() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the array representation. |
||
| 310 | */ |
||
| 311 | 57 | public function toArray(): array |
|
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | 48 | public function __toString() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritdoc} |
||
| 338 | */ |
||
| 339 | 3 | public function __debugInfo() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Output all the data of the JSend object. |
||
| 346 | * |
||
| 347 | * @param array $headers Optional headers to add to the response |
||
| 348 | * |
||
| 349 | * @return int Returns the number of characters read from the JSend object |
||
| 350 | * and passed throught to the output |
||
| 351 | */ |
||
| 352 | 12 | public function send(array $headers = []): int |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Filter Submitted Headers. |
||
| 371 | * |
||
| 372 | * @param array $headers a Collection of key/value headers |
||
| 373 | */ |
||
| 374 | 12 | private function filterHeaders(array $headers): array |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Validate Header name. |
||
| 386 | * |
||
| 387 | * @throws Exception if the header name is invalid |
||
| 388 | */ |
||
| 389 | 12 | private function validateHeaderName(string $name): string |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Validate Header value. |
||
| 400 | * |
||
| 401 | * @throws Exception if the header value is invalid |
||
| 402 | */ |
||
| 403 | 12 | private function validateHeaderValue(string $value): string |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Returns an instance with the specified status. |
||
| 416 | * |
||
| 417 | * This method MUST retain the state of the current instance, and return |
||
| 418 | * an instance that contains the specified status. |
||
| 419 | */ |
||
| 420 | 6 | public function withStatus(string $status): self |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Returns an instance with the specified data. |
||
| 432 | * |
||
| 433 | * This method MUST retain the state of the current instance, and return |
||
| 434 | * an instance that contains the specified data. |
||
| 435 | */ |
||
| 436 | 6 | public function withData($data): self |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Returns an instance with the specified error message and error code. |
||
| 451 | * |
||
| 452 | * This method MUST retain the state of the current instance, and return |
||
| 453 | * an instance that contains the specified error message and error code. |
||
| 454 | */ |
||
| 455 | 6 | public function withError($errorMessage, int $errorCode = null): self |
|
| 469 | } |
||
| 470 |