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 |
||
12 | class JSend implements JsonSerializable |
||
13 | { |
||
14 | const STATUS_SUCCESS = 'success'; |
||
15 | |||
16 | const STATUS_ERROR = 'error'; |
||
17 | |||
18 | const STATUS_FAIL = 'fail'; |
||
19 | |||
20 | /** |
||
21 | * JSend status |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $status; |
||
26 | |||
27 | /** |
||
28 | * JSend Data |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $data = []; |
||
33 | |||
34 | /** |
||
35 | * JSend Error Message |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $errorMessage = ''; |
||
40 | |||
41 | /** |
||
42 | * JSend Error Code |
||
43 | * @var int|null |
||
44 | */ |
||
45 | protected $errorCode; |
||
46 | |||
47 | /** |
||
48 | * New Instance |
||
49 | * |
||
50 | * @param string $status |
||
51 | * @param array $data |
||
52 | * @param string $errorMessage |
||
53 | * @param int $errorCode |
||
54 | */ |
||
55 | public function __construct($status, array $data = null, $errorMessage = null, $errorCode = null) |
||
61 | |||
62 | /** |
||
63 | * Filter and Validate the JSend Status |
||
64 | * |
||
65 | * @param string $status |
||
66 | * |
||
67 | * @throws UnexpectedValueException If the status value does not conform to JSend Spec. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | protected function filterStatus($status) |
||
80 | |||
81 | /** |
||
82 | * Filter and Validate the JSend Error properties |
||
83 | * |
||
84 | * @param string $errorMessage |
||
85 | * @param int $errorCode |
||
86 | */ |
||
87 | protected function filterError($errorMessage, $errorCode) |
||
98 | |||
99 | /** |
||
100 | * Validate a Type |
||
101 | * |
||
102 | * @param string $type |
||
103 | * @param callable $func |
||
104 | * @param mixed $str |
||
105 | * |
||
106 | * @throws UnexpectedValueException If the data value does not conform to the specified type |
||
107 | * |
||
108 | * @return mixed |
||
109 | */ |
||
110 | protected function validateType($type, callable $func, $str) |
||
122 | |||
123 | /** |
||
124 | * Returns the JSend status |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getStatus() |
||
132 | |||
133 | /** |
||
134 | * Returns the JSend data |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function getData() |
||
142 | |||
143 | /** |
||
144 | * Returns the JSend error message |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getErrorMessage() |
||
152 | |||
153 | /** |
||
154 | * Returns the JSend error code |
||
155 | * |
||
156 | * @return int|null |
||
157 | */ |
||
158 | public function getErrorCode() |
||
162 | |||
163 | /** |
||
164 | * Returns true if the status is success |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function isSuccess() |
||
172 | |||
173 | /** |
||
174 | * Returns true if the status is fail |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function isFail() |
||
182 | |||
183 | /** |
||
184 | * Returns true if the status is error |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function isError() |
||
192 | |||
193 | /** |
||
194 | * Transcode the JSend object into an array |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | public function toArray() |
||
216 | |||
217 | /** |
||
218 | * @inheritdoc |
||
219 | */ |
||
220 | public function jsonSerialize() |
||
224 | |||
225 | /** |
||
226 | * @inheritdoc |
||
227 | */ |
||
228 | public function __toString() |
||
232 | |||
233 | /** |
||
234 | * Encode and Send the JSend object as an HTTP Response |
||
235 | * |
||
236 | * @param array $headers Optional headers to add to the response |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | public function send(array $headers = []) |
||
249 | |||
250 | /** |
||
251 | * Filter Submitted Headers |
||
252 | * |
||
253 | * @param array $headers a Collection of key/value headers |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | protected function filterHeaders(array $headers) |
||
266 | |||
267 | /** |
||
268 | * Validate Header name |
||
269 | * |
||
270 | * @param string $name |
||
271 | * |
||
272 | * @throws InvalidArgumentException if the header name is invalid |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | protected function validateHeaderName($name) |
||
284 | |||
285 | /** |
||
286 | * Validate Header value |
||
287 | * |
||
288 | * @param string $name |
||
|
|||
289 | * |
||
290 | * @throws InvalidArgumentException if the header value is invalid |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | protected function validateHeaderValue($value) |
||
304 | |||
305 | /** |
||
306 | * Returns an instance with the specified status. |
||
307 | * |
||
308 | * This method MUST retain the state of the current instance, and return |
||
309 | * an instance that contains the specified status. |
||
310 | * |
||
311 | * @param string $status The status to use with the new instance. |
||
312 | * |
||
313 | * @return static A new instance with the specified status. |
||
314 | */ |
||
315 | public function withStatus($status) |
||
323 | |||
324 | /** |
||
325 | * Returns an instance with the specified data. |
||
326 | * |
||
327 | * This method MUST retain the state of the current instance, and return |
||
328 | * an instance that contains the specified data. |
||
329 | * |
||
330 | * @param array $data The data to use with the new instance. |
||
331 | * |
||
332 | * @return static A new instance with the specified data. |
||
333 | */ |
||
334 | public function withData(array $data) |
||
342 | |||
343 | /** |
||
344 | * Returns an instance with the specified error message and error code. |
||
345 | * |
||
346 | * This method MUST retain the state of the current instance, and return |
||
347 | * an instance that contains the specified error message and error code. |
||
348 | * |
||
349 | * @param string $errorMessage The error message to use with the new instance. |
||
350 | * @param int|null $errorCode The error code to use with the new instance. |
||
351 | * |
||
352 | * @return static A new instance with the specified status. |
||
353 | */ |
||
354 | public function withError($errorMessage, $errorCode = null) |
||
362 | |||
363 | /** |
||
364 | * Returns a successful JSend object with the specified data |
||
365 | * |
||
366 | * @param array $data The data to use with the new instance. |
||
367 | * |
||
368 | * @return static A new succesful instance with the specified data. |
||
369 | */ |
||
370 | public static function success(array $data = []) |
||
374 | |||
375 | /** |
||
376 | * Returns a failed JSend object with the specified data |
||
377 | * |
||
378 | * @param array $data The data to use with the new instance. |
||
379 | * |
||
380 | * @return static A new failed instance with the specified data. |
||
381 | */ |
||
382 | public static function fail(array $data = []) |
||
386 | |||
387 | /** |
||
388 | * Returns a error JSend object with the specified error message and error code. |
||
389 | * |
||
390 | * @param string $errorMessage The error message to use with the new instance. |
||
391 | * @param int|null $errorCode The error code to use with the new instance. |
||
392 | * @param array $data The optional data to use with the new instance. |
||
393 | * |
||
394 | * @return static A new failed instance with the specified data. |
||
395 | */ |
||
396 | public static function error($errorMessage, $errorCode = null, $data = null) |
||
400 | |||
401 | /** |
||
402 | * Returns a new instance from a JSON string |
||
403 | * |
||
404 | * @param string $json The string being decoded |
||
405 | * @param int $depth User specified recursion depth. |
||
406 | * @param int $options Bitmask of JSON decode options |
||
407 | * |
||
408 | * @throws InvalidArgumentException If the string can not be decode |
||
409 | * |
||
410 | * @return static |
||
411 | */ |
||
412 | public static function createFromString($json, $depth = 512, $options = 0) |
||
425 | |||
426 | /** |
||
427 | * Returns a new instance from an array |
||
428 | * |
||
429 | * @param array $arr The array to build a new JSend object with |
||
430 | * |
||
431 | * @return static |
||
432 | */ |
||
433 | public static function createFromArray(array $arr) |
||
440 | } |
||
441 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.