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:
1 | <?php |
||
19 | class TelegramLog |
||
20 | { |
||
21 | /** |
||
22 | * Monolog instance |
||
23 | * |
||
24 | * @var Logger |
||
25 | */ |
||
26 | static protected $monolog; |
||
27 | |||
28 | /** |
||
29 | * Monolog instance for update |
||
30 | * |
||
31 | * @var Logger |
||
32 | */ |
||
33 | static protected $monolog_update; |
||
34 | |||
35 | /** |
||
36 | * Path for error log |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | static protected $error_log_path; |
||
41 | |||
42 | /** |
||
43 | * Path for debug log |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | static protected $debug_log_path; |
||
48 | |||
49 | /** |
||
50 | * Path for update log |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | static protected $update_log_path; |
||
55 | |||
56 | /** |
||
57 | * Temporary stream handle for debug log |
||
58 | * |
||
59 | * @var resource|null |
||
60 | */ |
||
61 | static protected $debug_log_temp_stream_handle; |
||
62 | |||
63 | /** |
||
64 | * Initialize |
||
65 | * |
||
66 | * Initilize monolog instance. Singleton |
||
67 | * Is possbile provide an external monolog instance |
||
68 | * |
||
69 | * @param Logger $external_monolog |
||
70 | * |
||
71 | * @return Logger |
||
72 | */ |
||
73 | 3 | public static function initialize(Logger $external_monolog = null) |
|
85 | |||
86 | /** |
||
87 | * Initialize update |
||
88 | * |
||
89 | * Initilize monolog update instance. Singleton |
||
90 | * Is possbile provide an external monolog instance |
||
91 | * |
||
92 | * @param Logger $external_monolog |
||
93 | * |
||
94 | * @return Logger |
||
95 | */ |
||
96 | 1 | public static function initializeUpdate(Logger $external_monolog = null) |
|
108 | |||
109 | /** |
||
110 | * Initialize error log |
||
111 | * |
||
112 | * @param string $path |
||
113 | * @param HandlerInterface $external_handler |
||
114 | * |
||
115 | * @return Logger |
||
116 | * @throws TelegramLogException |
||
117 | */ |
||
118 | 2 | View Code Duplication | public static function initErrorLog($path, HandlerInterface $external_handler = null) |
134 | |||
135 | /** |
||
136 | * Initialize debug log |
||
137 | * |
||
138 | * @param string $path |
||
139 | * @param HandlerInterface $external_handler |
||
140 | * |
||
141 | * @return Logger |
||
142 | * @throws TelegramLogException |
||
143 | */ |
||
144 | 2 | View Code Duplication | public static function initDebugLog($path, HandlerInterface $external_handler = null) |
160 | |||
161 | /** |
||
162 | * Get the stream handle of the temporary debug output |
||
163 | * |
||
164 | * @return mixed The stream if debug is active, else false |
||
165 | */ |
||
166 | public static function getDebugLogTempStream() |
||
177 | |||
178 | /** |
||
179 | * Write the temporary debug stream to log and close the stream handle |
||
180 | * |
||
181 | * @param string $message Message (with placeholder) to write to the debug log |
||
182 | */ |
||
183 | public static function endDebugLogTempStream($message = '%s') |
||
192 | |||
193 | /** |
||
194 | * Initialize update log |
||
195 | * |
||
196 | * Initilize monolog instance. Singleton |
||
197 | * Is possbile provide an external monolog instance |
||
198 | * |
||
199 | * @param string $path |
||
200 | * @param HandlerInterface $external_handler |
||
201 | * |
||
202 | * @return Logger |
||
203 | * @throws TelegramLogException |
||
204 | */ |
||
205 | 2 | public static function initUpdateLog($path, HandlerInterface $external_handler = null) |
|
221 | |||
222 | /** |
||
223 | * Is error log active |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 4 | public static function isErrorLogActive() |
|
231 | |||
232 | /** |
||
233 | * Is debug log active |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | 2 | public static function isDebugLogActive() |
|
241 | |||
242 | /** |
||
243 | * Is update log active |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | 1 | public static function isUpdateLogActive() |
|
251 | |||
252 | /** |
||
253 | * Report error log |
||
254 | * |
||
255 | * @param string $text |
||
256 | */ |
||
257 | 4 | public static function error($text) |
|
264 | |||
265 | /** |
||
266 | * Report debug log |
||
267 | * |
||
268 | * @param string $text |
||
269 | */ |
||
270 | 2 | public static function debug($text) |
|
277 | |||
278 | /** |
||
279 | * Report update log |
||
280 | * |
||
281 | * @param string $text |
||
282 | */ |
||
283 | 1 | public static function update($text) |
|
290 | |||
291 | /** |
||
292 | * Applies vsprintf to the text if placeholder replacements are passed along. |
||
293 | * |
||
294 | * @param string $text |
||
295 | * @param array $args |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | 6 | protected static function getLogText($text, array $args = []) |
|
307 | } |
||
308 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.