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 TraitForResponse 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 TraitForResponse, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | trait TraitForResponse |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * Following is optional to have user id and username attached to log |
||
33 | * Following can be anything you want for example your currently logged in |
||
34 | * user info or system user running the script. |
||
35 | * |
||
36 | * @var unknown |
||
37 | */ |
||
38 | private $UID = 0; |
||
39 | |||
40 | private $username = 'anonymous'; |
||
41 | |||
42 | /** |
||
43 | * Loglevel defaults to 4 |
||
44 | * so $EMERGENCY, $ALERT, $CRITICAL, $ERROR are loged |
||
45 | * |
||
46 | * @var int $loglevel 0 - 8 (none - all) |
||
47 | */ |
||
48 | private $loglevel = 4; |
||
49 | |||
50 | /** |
||
51 | * Path to file if you want to log messages to file |
||
52 | * |
||
53 | * @var string $log_to_file |
||
54 | */ |
||
55 | private $log_to_file = false; |
||
56 | |||
57 | /** |
||
58 | * |
||
59 | * @var responseObject as soon first msg is logged |
||
60 | */ |
||
61 | protected $response = false; |
||
62 | |||
63 | /** |
||
64 | * System is unusable, something happend which should not happen |
||
65 | * |
||
66 | * @var array $EMERGENCY |
||
67 | */ |
||
68 | private $EMERGENCY = array( |
||
69 | 1 => 'emergency', |
||
70 | 100 => 'You need minimum php version 5.4' |
||
71 | ); |
||
72 | |||
73 | /** |
||
74 | * Action must be taken immediately. |
||
75 | * |
||
76 | * Example: Entire website down, database unavailable, etc. This should |
||
77 | * trigger the SMS alerts and wake you up. |
||
78 | * |
||
79 | * @var array $ALERT |
||
80 | */ |
||
81 | private $ALERT = array( |
||
82 | 2 => 'alert', |
||
83 | 200 => 'No such file or directory' |
||
84 | ); |
||
85 | |||
86 | /** |
||
87 | * Critical conditions. |
||
88 | * |
||
89 | * Example: Application component unavailable, unexpected exception. |
||
90 | * |
||
91 | * @var array $CRITICAL |
||
92 | */ |
||
93 | private $CRITICAL = array( |
||
94 | 3 => 'critical' |
||
95 | ); |
||
96 | |||
97 | /** |
||
98 | * Runtime errors that do not require immediate action but should typically |
||
99 | * be logged and monitored. |
||
100 | * |
||
101 | * @var array $ERROR |
||
102 | */ |
||
103 | private $ERROR = array( |
||
104 | 4 => 'error', |
||
105 | 400 => 'Logfile location is invalid' |
||
106 | ); |
||
107 | |||
108 | /** |
||
109 | * Exceptional occurrences that are not errors. |
||
110 | * |
||
111 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
112 | * that are not necessarily wrong. |
||
113 | * |
||
114 | * @var array $WARNING |
||
115 | */ |
||
116 | private $WARNING = array( |
||
117 | 5 => 'warning', |
||
118 | 500 => 'Changing the working directory failed!', |
||
119 | 501 => 'Changed the temporary directory failed! Destination not writable: ', |
||
120 | 502 => 'Permission denied or parent of directory does not exist if creating recursive directories (recursive = false) ', |
||
121 | 503 => 'Can not create the file, permission denied or parent directory does not exist : ', |
||
122 | 504 => 'Failed to create file : ', |
||
123 | 505 => 'Reading info - Permission denied. : ', |
||
124 | 506 => 'Failed to create structure from array!' |
||
125 | ); |
||
126 | |||
127 | /** |
||
128 | * Normal but significant events. |
||
129 | * |
||
130 | * @var array $NOTICE |
||
131 | */ |
||
132 | private $NOTICE = array( |
||
133 | 6 => 'notice', |
||
134 | 601 => 'Called file object without filename!', |
||
135 | 602 => 'Called info object without basename!' |
||
136 | ); |
||
137 | |||
138 | /** |
||
139 | * Interesting events. |
||
140 | * |
||
141 | * Example: User logs in, SQL logs. |
||
142 | * |
||
143 | * @var array $INFO |
||
144 | */ |
||
145 | private $INFO = array( |
||
146 | 7 => 'info', |
||
147 | 701 => 'Created new directory : ', |
||
148 | 702 => 'Removed directory : ', |
||
149 | 703 => 'Created new file : ', |
||
150 | 704 => 'Created new diectory structure : ' |
||
151 | ); |
||
152 | |||
153 | /** |
||
154 | * Detailed debug information. |
||
155 | * |
||
156 | * @var array $DEBUG |
||
157 | */ |
||
158 | private $DEBUG = array( |
||
159 | 8 => 'debug', |
||
160 | 800 => 'Creating directory with empty path should not be called', |
||
161 | 801 => 'libhowi-filesystem OK', |
||
162 | 802 => 'Tried to access not directly accessible function which should be private function', |
||
163 | 803 => 'Changed the temporary directory success', |
||
164 | 804 => 'Changed the temporaty directory by sys_get_temp_dir()', |
||
165 | 805 => 'Returned current temporaty directory : ', |
||
166 | 806 => 'Created new temporary name for file : ', |
||
167 | 807 => 'Loaded directory object', |
||
168 | 808 => 'Loaded file object', |
||
169 | 809 => 'Loaded info object', |
||
170 | 810 => 'Creating structure from array!' |
||
171 | ); |
||
172 | |||
173 | /** |
||
174 | * Add message to log |
||
175 | * |
||
176 | * @param array $LEVEL |
||
177 | * from private static $LEVEL; |
||
178 | * @param number $code |
||
|
|||
179 | * @param string $context |
||
180 | * @param string $append_msg |
||
181 | * @return void |
||
182 | */ |
||
183 | 731 | private function addTrace($LEVEL, $code = 0, $context = "not specified", $append_msg = false, $to_file = false) |
|
199 | |||
200 | /** |
||
201 | * Write to the file |
||
202 | */ |
||
203 | 60 | protected function logToFile($LEVEL) |
|
218 | |||
219 | /** |
||
220 | * Set loglevel |
||
221 | * |
||
222 | * @param number $level |
||
223 | */ |
||
224 | 731 | public function setLogLevel($level = 0) |
|
228 | |||
229 | /** |
||
230 | * Set log file |
||
231 | * |
||
232 | * @param string $filepath |
||
233 | * @return boolean |
||
234 | */ |
||
235 | 731 | public function setLogFile($filepath = false) |
|
245 | |||
246 | /** |
||
247 | * Get logfile |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 731 | public function getLogFile() |
|
255 | |||
256 | /** |
||
257 | * Get log level |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | 731 | public function getLogLevel() |
|
265 | |||
266 | /** |
||
267 | * |
||
268 | * @return int userid |
||
269 | */ |
||
270 | 731 | public function getUID() |
|
274 | |||
275 | /** |
||
276 | * Return event microtime |
||
277 | */ |
||
278 | 63 | public function getTime() |
|
282 | |||
283 | /** |
||
284 | * Return username |
||
285 | */ |
||
286 | 731 | public function getUsername() |
|
290 | |||
291 | /** |
||
292 | * Get response with or without log |
||
293 | * |
||
294 | * @param string $key |
||
295 | * @return object |
||
296 | */ |
||
297 | 731 | public function response() |
|
301 | |||
302 | /** |
||
303 | * Get last status |
||
304 | * |
||
305 | * @return int |
||
306 | */ |
||
307 | 33 | public function getStatus() |
|
311 | |||
312 | /** |
||
313 | * Get last logged message |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | 66 | public function getMsg() |
|
321 | |||
322 | /** |
||
323 | * Get last logged message code |
||
324 | * |
||
325 | * @return int |
||
326 | */ |
||
327 | 84 | public function getCode() |
|
331 | |||
332 | /** |
||
333 | * Set last status |
||
334 | * |
||
335 | * @return int |
||
336 | */ |
||
337 | |||
338 | /** |
||
339 | * Set user id |
||
340 | * |
||
341 | * @param number $ID |
||
342 | * @return void |
||
343 | */ |
||
344 | 731 | public function setUID($UID) |
|
348 | |||
349 | /** |
||
350 | * Set username |
||
351 | * |
||
352 | * @param string $username |
||
353 | * @return void |
||
354 | */ |
||
355 | 731 | public function setUsername($username) |
|
359 | |||
360 | /** |
||
361 | * response setStatus |
||
362 | * |
||
363 | * @param bool $status |
||
364 | */ |
||
365 | 731 | public function setStatus($status) |
|
370 | |||
371 | /** |
||
372 | * response setCode |
||
373 | * |
||
374 | * @param bool $code |
||
375 | */ |
||
376 | 21 | public function setCode($code) |
|
381 | |||
382 | /** |
||
383 | * System is unusable, something happend which should not happen |
||
384 | * |
||
385 | * @param int $code |
||
386 | * @param resource $context |
||
387 | * @param string $append_msg |
||
388 | */ |
||
389 | 6 | View Code Duplication | public function emergency($code = 1, $append_msg = false, $context = false) |
397 | |||
398 | /** |
||
399 | * Action must be taken immediately. |
||
400 | * |
||
401 | * Example: Entire website down, database unavailable, etc. This should |
||
402 | * trigger the SMS alerts and wake you up. |
||
403 | * |
||
404 | * @param int $code |
||
405 | * @param resource $context |
||
406 | * @param string $append_msg |
||
407 | * @return null |
||
408 | */ |
||
409 | 15 | View Code Duplication | public function alert($code = 2, $append_msg = false, $context = false) |
416 | |||
417 | /** |
||
418 | * Critical conditions. |
||
419 | * |
||
420 | * Example: Application component unavailable, unexpected exception. |
||
421 | * |
||
422 | * @param int $code |
||
423 | * @param resource $context |
||
424 | * @param string $append_msg |
||
425 | * @return null |
||
426 | */ |
||
427 | 6 | View Code Duplication | public function critical($code = 3, $append_msg = false, $context = false) |
434 | |||
435 | /** |
||
436 | * Runtime errors that do not require immediate action but should typically |
||
437 | * be logged and monitored. |
||
438 | * |
||
439 | * @param int $code |
||
440 | * @param resource $context |
||
441 | * @param string $append_msg |
||
442 | * |
||
443 | * @return null |
||
444 | */ |
||
445 | 15 | View Code Duplication | public function error($code = 4, $append_msg = false, $context = false) |
452 | |||
453 | /** |
||
454 | * Exceptional occurrences that are not errors. |
||
455 | * |
||
456 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
457 | * that are not necessarily wrong. |
||
458 | * |
||
459 | * @param int $code |
||
460 | * @param resource $context |
||
461 | * @param string $append_msg |
||
462 | * @return null |
||
463 | */ |
||
464 | 24 | View Code Duplication | public function warning($code = 5, $append_msg = false, $context = false) |
471 | |||
472 | /** |
||
473 | * Normal but significant events. |
||
474 | * |
||
475 | * @param int $code |
||
476 | * @param resource $context |
||
477 | * @param string $append_msg |
||
478 | * |
||
479 | * @return null |
||
480 | */ |
||
481 | 9 | View Code Duplication | public function notice($code = 6, $append_msg = false, $context = false) |
488 | |||
489 | /** |
||
490 | * Interesting events. |
||
491 | * |
||
492 | * Example: User logs in, SQL logs. |
||
493 | * |
||
494 | * @param int $code |
||
495 | * @param resource $context |
||
496 | * @param string $append_msg |
||
497 | * |
||
498 | * @return null |
||
499 | */ |
||
500 | 36 | View Code Duplication | public function info($code = 7, $append_msg = false, $context = false) |
507 | |||
508 | /** |
||
509 | * Detailed debug information. |
||
510 | * |
||
511 | * @param int $code |
||
512 | * @param resource $context |
||
513 | * @param string $append_msg |
||
514 | * |
||
515 | * @return null |
||
516 | */ |
||
517 | 731 | View Code Duplication | public function debug($code = 8, $append_msg = false, $context = false) |
524 | } |
||
525 |