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) |
|
| 73 | { |
||
| 74 | 3 | if (self::$monolog === null) { |
|
| 75 | 3 | if ($external_monolog !== null) { |
|
| 76 | 1 | self::$monolog = $external_monolog; |
|
| 77 | |||
| 78 | 1 | foreach (self::$monolog->getHandlers() as $handler) { |
|
| 79 | 1 | if ($handler->getLevel() == 400) { |
|
|
|
|||
| 80 | 1 | self::$error_log_path = true; |
|
| 81 | } |
||
| 82 | 1 | if ($handler->getLevel() == 100) { |
|
| 83 | 1 | self::$debug_log_path = true; |
|
| 84 | } |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | 2 | self::$monolog = new Logger('bot_log'); |
|
| 88 | } |
||
| 89 | } |
||
| 90 | 3 | return self::$monolog; |
|
| 91 | } |
||
| 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) |
| 102 | { |
||
| 103 | 2 | if ($path === null || $path === '') { |
|
| 104 | 1 | throw new TelegramLogException('Empty path for error log'); |
|
| 105 | } |
||
| 106 | 1 | self::initialize(); |
|
| 107 | 1 | self::$error_log_path = $path; |
|
| 108 | |||
| 109 | 1 | return self::$monolog->pushHandler( |
|
| 110 | 1 | (new StreamHandler(self::$error_log_path, Logger::ERROR)) |
|
| 111 | 1 | ->setFormatter(new LineFormatter(null, null, true)) |
|
| 112 | ); |
||
| 113 | } |
||
| 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) |
| 124 | { |
||
| 125 | 2 | if ($path === null || $path === '') { |
|
| 126 | 1 | throw new TelegramLogException('Empty path for debug log'); |
|
| 127 | } |
||
| 128 | 1 | self::initialize(); |
|
| 129 | 1 | self::$debug_log_path = $path; |
|
| 130 | |||
| 131 | 1 | return self::$monolog->pushHandler( |
|
| 132 | 1 | (new StreamHandler(self::$debug_log_path, Logger::DEBUG)) |
|
| 133 | 1 | ->setFormatter(new LineFormatter(null, null, true)) |
|
| 134 | ); |
||
| 135 | } |
||
| 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() |
||
| 143 | { |
||
| 144 | if (self::$debug_log_temp_stream_handle === null) { |
||
| 145 | if (self::isDebugLogActive()) { |
||
| 146 | self::$debug_log_temp_stream_handle = fopen('php://temp', 'w+'); |
||
| 147 | } else { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | return self::$debug_log_temp_stream_handle; |
||
| 152 | } |
||
| 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') |
||
| 160 | { |
||
| 161 | if (self::$debug_log_temp_stream_handle !== null) { |
||
| 162 | rewind(self::$debug_log_temp_stream_handle); |
||
| 163 | self::debug( |
||
| 164 | sprintf( |
||
| 165 | $message, |
||
| 166 | stream_get_contents(self::$debug_log_temp_stream_handle) |
||
| 167 | ) |
||
| 168 | ); |
||
| 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 | */ |
||
| 185 | 2 | public static function initUpdateLog($path) |
|
| 186 | { |
||
| 187 | 2 | if ($path === null || $path === '') { |
|
| 188 | 1 | throw new TelegramLogException('Empty path for update log'); |
|
| 189 | } |
||
| 190 | 1 | self::$update_log_path = $path; |
|
| 191 | 1 | if (self::$monolog_update === null) { |
|
| 192 | 1 | self::$monolog_update = new Logger('bot_update_log'); |
|
| 193 | // Create a formatter |
||
| 194 | 1 | $output = "%message%\n"; |
|
| 195 | 1 | $formatter = new LineFormatter($output); |
|
| 196 | |||
| 197 | // Update handler |
||
| 198 | 1 | $update_handler = new StreamHandler(self::$update_log_path, Logger::INFO); |
|
| 199 | 1 | $update_handler->setFormatter($formatter); |
|
| 200 | |||
| 201 | 1 | self::$monolog_update->pushHandler($update_handler); |
|
| 202 | } |
||
| 203 | 1 | return self::$monolog; |
|
| 204 | } |
||
| 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: