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 TelegramLog 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 TelegramLog, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class TelegramLog |
||
37 | { |
||
38 | /** |
||
39 | * Logger instance |
||
40 | * |
||
41 | * @var LoggerInterface|Logger |
||
42 | */ |
||
43 | protected static $logger; |
||
44 | |||
45 | /** |
||
46 | * Logger instance for update |
||
47 | * |
||
48 | * @var LoggerInterface|Logger |
||
49 | */ |
||
50 | protected static $update_logger; |
||
51 | |||
52 | /** |
||
53 | * Path for error log |
||
54 | * |
||
55 | * @var string |
||
56 | * @deprecated |
||
57 | */ |
||
58 | protected static $error_log_path; |
||
59 | |||
60 | /** |
||
61 | * Path for debug log |
||
62 | * |
||
63 | * @var string |
||
64 | * @deprecated |
||
65 | */ |
||
66 | protected static $debug_log_path; |
||
67 | |||
68 | /** |
||
69 | * Path for update log |
||
70 | * |
||
71 | * @var string |
||
72 | * @deprecated |
||
73 | */ |
||
74 | protected static $update_log_path; |
||
75 | |||
76 | /** |
||
77 | * Temporary stream handle for debug log |
||
78 | * |
||
79 | * @var resource|null |
||
80 | */ |
||
81 | protected static $debug_log_temp_stream_handle; |
||
82 | |||
83 | /** |
||
84 | * Initialise Logger instance, optionally passing an existing one. |
||
85 | * |
||
86 | * @param LoggerInterface|null $logger |
||
87 | * @param LoggerInterface|null $update_logger |
||
88 | */ |
||
89 | public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null) |
||
110 | |||
111 | /** |
||
112 | * Initialise error log (deprecated) |
||
113 | * |
||
114 | * @param string $path |
||
115 | * |
||
116 | * @return LoggerInterface |
||
117 | * @throws Exception |
||
118 | * |
||
119 | * @deprecated Initialise a preconfigured logger instance instead. |
||
120 | */ |
||
121 | View Code Duplication | public static function initErrorLog($path) |
|
142 | |||
143 | /** |
||
144 | * Initialise debug log (deprecated) |
||
145 | * |
||
146 | * @param string $path |
||
147 | * |
||
148 | * @return LoggerInterface |
||
149 | * @throws Exception |
||
150 | * |
||
151 | * @deprecated Initialise a preconfigured logger instance instead. |
||
152 | */ |
||
153 | View Code Duplication | public static function initDebugLog($path) |
|
174 | |||
175 | /** |
||
176 | * Initialise update log (deprecated) |
||
177 | * |
||
178 | * @param string $path |
||
179 | * |
||
180 | * @return LoggerInterface |
||
181 | * @throws Exception |
||
182 | * |
||
183 | * @deprecated Initialise a preconfigured logger instance instead. |
||
184 | */ |
||
185 | public static function initUpdateLog($path) |
||
205 | |||
206 | /** |
||
207 | * Get the stream handle of the temporary debug output |
||
208 | * |
||
209 | * @return mixed The stream if debug is active, else false |
||
210 | */ |
||
211 | public static function getDebugLogTempStream() |
||
219 | |||
220 | /** |
||
221 | * Write the temporary debug stream to log and close the stream handle |
||
222 | * |
||
223 | * @param string $message Message (with placeholder) to write to the debug log |
||
224 | */ |
||
225 | public static function endDebugLogTempStream($message = '%s') |
||
234 | |||
235 | /** |
||
236 | * Is error log active |
||
237 | * |
||
238 | * @return bool |
||
239 | * |
||
240 | * @deprecated Initialise a preconfigured logger instance instead. |
||
241 | */ |
||
242 | public static function isErrorLogActive() |
||
247 | |||
248 | /** |
||
249 | * Is debug log active |
||
250 | * |
||
251 | * @return bool |
||
252 | * |
||
253 | * @deprecated Initialise a preconfigured logger instance instead. |
||
254 | */ |
||
255 | public static function isDebugLogActive() |
||
260 | |||
261 | /** |
||
262 | * Is update log active |
||
263 | * |
||
264 | * @return bool |
||
265 | * |
||
266 | * @deprecated Initialise a preconfigured logger instance instead. |
||
267 | */ |
||
268 | public static function isUpdateLogActive() |
||
273 | |||
274 | /** |
||
275 | * Handle any logging method call. |
||
276 | * |
||
277 | * @param string $name |
||
278 | * @param array $arguments |
||
279 | */ |
||
280 | public static function __callStatic($name, array $arguments) |
||
307 | |||
308 | /** |
||
309 | * Interpolates context values into the message placeholders. |
||
310 | * |
||
311 | * @see https://www.php-fig.org/psr/psr-3/#12-message |
||
312 | * |
||
313 | * @param string $message |
||
314 | * @param array $context |
||
315 | * |
||
316 | * @return string |
||
317 | */ |
||
318 | protected static function interpolate($message, array $context = []) |
||
332 | } |
||
333 |
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: