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 |
||
18 | class TelegramLog |
||
19 | { |
||
20 | /** |
||
21 | * Monolog instance |
||
22 | * |
||
23 | * @var \Monolog\Logger |
||
24 | */ |
||
25 | static protected $monolog = null; |
||
26 | |||
27 | /** |
||
28 | * Monolog instance for update |
||
29 | * |
||
30 | * @var \Monolog\Logger |
||
31 | */ |
||
32 | static protected $monolog_update = null; |
||
33 | |||
34 | /** |
||
35 | * Path for error log |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | static protected $error_log_path = null; |
||
40 | |||
41 | /** |
||
42 | * Path for debug log |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | static protected $debug_log_path = null; |
||
47 | |||
48 | /** |
||
49 | * Path for update log |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | static protected $update_log_path = null; |
||
54 | |||
55 | /** |
||
56 | * Temporary stream handle for debug log |
||
57 | * |
||
58 | * @var null |
||
59 | */ |
||
60 | static protected $debug_log_temp_stream_handle = null; |
||
61 | |||
62 | /** |
||
63 | * Initialize |
||
64 | * |
||
65 | * Initilize monolog instance. Singleton |
||
66 | * Is possbile provide an external monolog instance |
||
67 | * |
||
68 | * @param \Monolog\Logger |
||
69 | * |
||
70 | * @return \Monolog\Logger |
||
71 | */ |
||
72 | 3 | public static function initialize(Logger $external_monolog = null) |
|
92 | |||
93 | /** |
||
94 | * Initialize error log |
||
95 | * |
||
96 | * @param string $path |
||
97 | * |
||
98 | * @return \Monolog\Logger |
||
99 | * @throws \Longman\TelegramBot\Exception\TelegramLogException |
||
100 | */ |
||
101 | 2 | View Code Duplication | public static function initErrorLog($path) |
114 | |||
115 | /** |
||
116 | * Initialize debug log |
||
117 | * |
||
118 | * @param string $path |
||
119 | * |
||
120 | * @return \Monolog\Logger |
||
121 | * @throws \Longman\TelegramBot\Exception\TelegramLogException |
||
122 | */ |
||
123 | 2 | View Code Duplication | public static function initDebugLog($path) |
136 | |||
137 | /** |
||
138 | * Get the stream handle of the temporary debug output |
||
139 | * |
||
140 | * @return mixed The stream if debug is active, else false |
||
141 | */ |
||
142 | public static function getDebugLogTempStream() |
||
153 | |||
154 | /** |
||
155 | * Write the temporary debug stream to log and close the stream handle |
||
156 | * |
||
157 | * @param string $message Message (with placeholder) to write to the debug log |
||
158 | */ |
||
159 | public static function endDebugLogTempStream($message = '%s') |
||
173 | |||
174 | /** |
||
175 | * Initialize update log |
||
176 | * |
||
177 | * Initilize monolog instance. Singleton |
||
178 | * Is possbile provide an external monolog instance |
||
179 | * |
||
180 | * @param string $path |
||
181 | * |
||
182 | * @return \Monolog\Logger |
||
183 | * @throws \Longman\TelegramBot\Exception\TelegramLogException |
||
184 | */ |
||
185 | 2 | public static function initUpdateLog($path) |
|
205 | |||
206 | /** |
||
207 | * Is error log active |
||
208 | * |
||
209 | * @return bool |
||
210 | */ |
||
211 | 4 | public static function isErrorLogActive() |
|
215 | |||
216 | /** |
||
217 | * Is debug log active |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 2 | public static function isDebugLogActive() |
|
225 | |||
226 | /** |
||
227 | * Is update log active |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | 1 | public static function isUpdateLogActive() |
|
235 | |||
236 | /** |
||
237 | * Report error log |
||
238 | * |
||
239 | * @param string $text |
||
240 | */ |
||
241 | 4 | public static function error($text) |
|
247 | |||
248 | /** |
||
249 | * Report debug log |
||
250 | * |
||
251 | * @param string $text |
||
252 | */ |
||
253 | 2 | public static function debug($text) |
|
259 | |||
260 | /** |
||
261 | * Report update log |
||
262 | * |
||
263 | * @param string $text |
||
264 | */ |
||
265 | 1 | public static function update($text) |
|
271 | } |
||
272 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: