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; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Monolog instance for update |
||
| 29 | * |
||
| 30 | * @var \Monolog\Logger |
||
| 31 | */ |
||
| 32 | static protected $monolog_update; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Path for error log |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | static protected $error_log_path; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Path for debug log |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | static protected $debug_log_path; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Path for update log |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | static protected $update_log_path; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Temporary stream handle for debug log |
||
| 57 | * |
||
| 58 | * @var resource|null |
||
| 59 | */ |
||
| 60 | static protected $debug_log_temp_stream_handle; |
||
| 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) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Initialize error log |
||
| 96 | * |
||
| 97 | * @param string $path |
||
| 98 | * |
||
| 99 | * @return \Monolog\Logger |
||
| 100 | * @throws \Longman\TelegramBot\Exception\TelegramLogException |
||
| 101 | * @throws \InvalidArgumentException |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | 2 | View Code Duplication | public static function initErrorLog($path) |
| 117 | |||
| 118 | /** |
||
| 119 | * Initialize debug log |
||
| 120 | * |
||
| 121 | * @param string $path |
||
| 122 | * |
||
| 123 | * @return \Monolog\Logger |
||
| 124 | * @throws \Longman\TelegramBot\Exception\TelegramLogException |
||
| 125 | * @throws \InvalidArgumentException |
||
| 126 | * @throws \Exception |
||
| 127 | */ |
||
| 128 | 2 | View Code Duplication | public static function initDebugLog($path) |
| 141 | |||
| 142 | /** |
||
| 143 | * Get the stream handle of the temporary debug output |
||
| 144 | * |
||
| 145 | * @return mixed The stream if debug is active, else false |
||
| 146 | */ |
||
| 147 | public static function getDebugLogTempStream() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Write the temporary debug stream to log and close the stream handle |
||
| 161 | * |
||
| 162 | * @param string $message Message (with placeholder) to write to the debug log |
||
| 163 | */ |
||
| 164 | public static function endDebugLogTempStream($message = '%s') |
||
| 165 | { |
||
| 166 | if (is_resource(self::$debug_log_temp_stream_handle)) { |
||
| 167 | rewind(self::$debug_log_temp_stream_handle); |
||
| 168 | self::debug($message, stream_get_contents(self::$debug_log_temp_stream_handle)); |
||
| 169 | fclose(self::$debug_log_temp_stream_handle); |
||
| 170 | self::$debug_log_temp_stream_handle = null; |
||
| 171 | } |
||
| 172 | } |
||
| 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 | * @throws \InvalidArgumentException |
||
| 185 | * @throws \Exception |
||
| 186 | */ |
||
| 187 | 2 | public static function initUpdateLog($path) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Is error log active |
||
| 211 | * |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | 4 | public static function isErrorLogActive() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Is debug log active |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | 2 | public static function isDebugLogActive() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Is update log active |
||
| 231 | * |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | 1 | public static function isUpdateLogActive() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Report error log |
||
| 241 | * |
||
| 242 | * @param string $text |
||
| 243 | */ |
||
| 244 | 4 | public static function error($text) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Report debug log |
||
| 254 | * |
||
| 255 | * @param string $text |
||
| 256 | */ |
||
| 257 | 2 | public static function debug($text) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Report update log |
||
| 267 | * |
||
| 268 | * @param string $text |
||
| 269 | */ |
||
| 270 | 1 | public static function update($text) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Applies vsprintf to the text if placeholder replacements are passed along. |
||
| 280 | * |
||
| 281 | * @param string $text |
||
| 282 | * @param array $args |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 6 | protected static function getLogText($text, array $args = []) |
|
| 294 | } |
||
| 295 |
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: