Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
20 | class JSend |
||
21 | { |
||
22 | const SUCCESS = 'success'; |
||
23 | const FAIL = 'fail'; |
||
24 | const ERROR = 'error'; |
||
25 | |||
26 | /** @var string The status. */ |
||
27 | private $status; |
||
28 | |||
29 | /** @var array The data. */ |
||
30 | private $data; |
||
31 | |||
32 | /** @var string|null The message. */ |
||
33 | private $message; |
||
34 | |||
35 | /** @var int|null The code. */ |
||
36 | private $code; |
||
37 | |||
38 | /** |
||
39 | * Construct a JSend object with the given status, data, message and code. |
||
40 | * |
||
41 | * @param string $status |
||
42 | * @param array|null $data = null |
||
43 | * @param string|null $message = null |
||
44 | * @param int|null $code = null |
||
45 | * @throws \UnexpectedValueException |
||
46 | */ |
||
47 | 32 | public function __construct($status, array $data = null, $message = null, $code = null) |
|
54 | |||
55 | /** |
||
56 | * Returns the string representation of the object. |
||
57 | * |
||
58 | * @return string the string representation of the object. |
||
59 | */ |
||
60 | 7 | public function __toString() |
|
64 | |||
65 | /** |
||
66 | * Returns the array representation of the object. |
||
67 | * |
||
68 | * @return array the array representation of the object. |
||
69 | */ |
||
70 | 9 | public function toArray() |
|
96 | |||
97 | /** |
||
98 | * Returns a JSend succes object with the given data. |
||
99 | * |
||
100 | * @param array|null $data = null |
||
101 | * @return JSend a JSend succes object with the given data. |
||
102 | */ |
||
103 | 2 | public static function success(array $data = null) |
|
107 | |||
108 | /** |
||
109 | * Returns a JSend fail object with the given data. |
||
110 | * |
||
111 | * @param array|null $data = null |
||
112 | * @return JSend a JSend fail object with the given data. |
||
113 | */ |
||
114 | 2 | public static function fail(array $data = null) |
|
118 | |||
119 | /** |
||
120 | * Returns a JSend error object with the given message, code and data. |
||
121 | * |
||
122 | * @param string $message |
||
123 | * @param int|null $code = null |
||
124 | * @param array|null $data = null |
||
125 | * @return JSend a JSend error object with the given message, code and data. |
||
126 | */ |
||
127 | 2 | public static function error($message, $code = null, array $data = null) |
|
131 | |||
132 | /** |
||
133 | * Returns the decoded JSend object. |
||
134 | * |
||
135 | * @param string $input |
||
136 | * @return JSend the decoded JSend object. |
||
137 | * @throws \UnexpectedValueException |
||
138 | */ |
||
139 | 7 | public static function decode($input) |
|
149 | |||
150 | /** |
||
151 | * Returns the decoded JSend input. |
||
152 | * |
||
153 | * @param array $json |
||
154 | * @return JSend the decoded JSend input. |
||
155 | * @throws \UnexpectedValueException |
||
156 | */ |
||
157 | 6 | public static function decodeJson(array $json) |
|
176 | |||
177 | /** |
||
178 | * Returns the decoded jSend succes object. |
||
179 | * |
||
180 | * @param array $json |
||
181 | * @return JSend the decoded jSend succes object. |
||
182 | * @throws \UnexpectedValueException |
||
183 | */ |
||
184 | 2 | View Code Duplication | public static function decodeSucces(array $json) |
192 | |||
193 | /** |
||
194 | * Returns the decoded jSend fail object. |
||
195 | * |
||
196 | * @param array $json |
||
197 | * @return JSend the decoded jSend fail object. |
||
198 | * @throws \UnexpectedValueException |
||
199 | */ |
||
200 | 2 | View Code Duplication | public static function decodeFail(array $json) |
208 | |||
209 | /** |
||
210 | * Returns the decoded jSend error object. |
||
211 | * |
||
212 | * @param array $json |
||
213 | * @return JSend the decoded jSend error object. |
||
214 | * @throws \UnexpectedValueException |
||
215 | */ |
||
216 | 2 | public static function decodeError(array $json) |
|
227 | |||
228 | /** |
||
229 | * Returns the encoded JSend object. |
||
230 | * |
||
231 | * @return string the encoded JSend object. |
||
232 | */ |
||
233 | 8 | public function encode() |
|
237 | |||
238 | /** |
||
239 | * Returns true if the status is success. |
||
240 | * |
||
241 | * @return bool true if the status is success. |
||
242 | */ |
||
243 | 1 | public function isSuccess() |
|
247 | |||
248 | /** |
||
249 | * Returns true if the status is fail. |
||
250 | * |
||
251 | * @return bool true if the status is fail. |
||
252 | */ |
||
253 | 1 | public function isFail() |
|
257 | |||
258 | /** |
||
259 | * Returns true if the status is error. |
||
260 | * |
||
261 | * @return bool true if the status is error. |
||
262 | */ |
||
263 | 1 | public function isError() |
|
267 | |||
268 | /** |
||
269 | * Returns the status. |
||
270 | * |
||
271 | * @return string the status. |
||
272 | */ |
||
273 | 2 | public function getStatus() |
|
277 | |||
278 | /** |
||
279 | * Set the status. |
||
280 | * |
||
281 | * @param string $status |
||
282 | * @return JSend $this |
||
283 | * @throws \UnexpectedValueException |
||
284 | */ |
||
285 | 32 | public function setStatus($status) |
|
295 | |||
296 | /** |
||
297 | * Returns the data. |
||
298 | * |
||
299 | * @return array the data. |
||
300 | */ |
||
301 | 3 | public function getData() |
|
305 | |||
306 | /** |
||
307 | * Set the data. |
||
308 | * |
||
309 | * @param array|null $data = null |
||
310 | * @return JSend $this |
||
311 | */ |
||
312 | 32 | public function setData(array $data = null) |
|
318 | |||
319 | /** |
||
320 | * Returns the message. |
||
321 | * |
||
322 | * @return string|null the message. |
||
323 | */ |
||
324 | 2 | public function getMessage() |
|
328 | |||
329 | /** |
||
330 | * Set the message. |
||
331 | * |
||
332 | * @param string|null $message = null |
||
333 | * @return JSend $this |
||
334 | */ |
||
335 | 32 | public function setMessage($message = null) |
|
341 | |||
342 | /** |
||
343 | * Returns the code. |
||
344 | * |
||
345 | * @return int|null the code. |
||
346 | */ |
||
347 | 2 | public function getCode() |
|
351 | |||
352 | /** |
||
353 | * Set the code. |
||
354 | * |
||
355 | * @param int|null $code = null |
||
356 | * @return JSend $this |
||
357 | */ |
||
358 | 32 | public function setCode($code = null) |
|
364 | |||
365 | /** |
||
366 | * Sends the JSend object. |
||
367 | * |
||
368 | * @return void |
||
369 | */ |
||
370 | 6 | public function send() |
|
381 | } |
||
382 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.