Total Complexity | 55 |
Total Lines | 295 |
Duplicated Lines | 0 % |
Coverage | 73.32% |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
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.
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 | 6 | public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null) |
|
90 | { |
||
91 | // Clearly deprecated code still being executed. |
||
92 | 6 | if ($logger === null) { |
|
93 | 3 | (defined('PHPUNIT_TESTSUITE') && PHPUNIT_TESTSUITE) || trigger_error('A PSR-3 compatible LoggerInterface object must be provided. Initialise with a preconfigured logger instance.', E_USER_DEPRECATED); |
|
1 ignored issue
–
show
|
|||
94 | 3 | $logger = new Logger('bot_log'); |
|
95 | 6 | } elseif ($logger instanceof Logger) { |
|
96 | 6 | foreach ($logger->getHandlers() as $handler) { |
|
97 | 5 | if (method_exists($handler, 'getLevel') && $handler->getLevel() === Logger::ERROR) { |
|
98 | 4 | self::$error_log_path = 'true'; |
|
99 | } |
||
100 | 5 | if (method_exists($handler, 'getLevel') && $handler->getLevel() === Logger::DEBUG) { |
|
101 | 4 | self::$debug_log_path = 'true'; |
|
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // Fallback to NullLogger. |
||
107 | 6 | self::$logger = $logger ?: new NullLogger(); |
|
108 | 6 | self::$update_logger = $update_logger ?: new NullLogger(); |
|
109 | 6 | } |
|
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 | public static function initErrorLog($path) |
||
122 | { |
||
123 | (defined('PHPUNIT_TESTSUITE') && PHPUNIT_TESTSUITE) || trigger_error(__METHOD__ . ' is deprecated and will be removed soon. Initialise with a preconfigured logger instance instead using "TelegramLog::initialize($logger)".', E_USER_DEPRECATED); |
||
1 ignored issue
–
show
|
|||
124 | |||
125 | if ($path === null || $path === '') { |
||
126 | throw new TelegramLogException('Empty path for error log'); |
||
127 | } |
||
128 | self::initialize(); |
||
129 | |||
130 | // Deprecated code used as fallback. |
||
131 | if (self::$logger instanceof Logger) { |
||
132 | self::$error_log_path = $path; |
||
133 | |||
134 | self::$logger->pushHandler( |
||
135 | (new StreamHandler(self::$error_log_path, Logger::ERROR)) |
||
136 | ->setFormatter(new LineFormatter(null, null, true)) |
||
137 | ); |
||
138 | } |
||
139 | |||
140 | return self::$logger; |
||
141 | } |
||
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 | public static function initDebugLog($path) |
||
154 | { |
||
155 | (defined('PHPUNIT_TESTSUITE') && PHPUNIT_TESTSUITE) || trigger_error(__METHOD__ . ' is deprecated and will be removed soon. Initialise with a preconfigured logger instance instead using "TelegramLog::initialize($logger)".', E_USER_DEPRECATED); |
||
1 ignored issue
–
show
|
|||
156 | |||
157 | if ($path === null || $path === '') { |
||
158 | throw new TelegramLogException('Empty path for debug log'); |
||
159 | } |
||
160 | self::initialize(); |
||
161 | |||
162 | // Deprecated code used as fallback. |
||
163 | if (self::$logger instanceof Logger) { |
||
164 | self::$debug_log_path = $path; |
||
165 | |||
166 | self::$logger->pushHandler( |
||
167 | (new StreamHandler(self::$debug_log_path, Logger::DEBUG)) |
||
168 | ->setFormatter(new LineFormatter(null, null, true)) |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | return self::$logger; |
||
173 | } |
||
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) |
||
204 | } |
||
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() |
||
212 | { |
||
213 | if ((self::$debug_log_temp_stream_handle === null) && $temp_stream_handle = fopen('php://temp', 'wb+')) { |
||
214 | self::$debug_log_temp_stream_handle = $temp_stream_handle; |
||
215 | } |
||
216 | |||
217 | return self::$debug_log_temp_stream_handle; |
||
218 | } |
||
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') |
||
226 | { |
||
227 | if (is_resource(self::$debug_log_temp_stream_handle)) { |
||
228 | rewind(self::$debug_log_temp_stream_handle); |
||
229 | self::debug(sprintf($message, stream_get_contents(self::$debug_log_temp_stream_handle))); |
||
230 | fclose(self::$debug_log_temp_stream_handle); |
||
231 | self::$debug_log_temp_stream_handle = null; |
||
232 | } |
||
233 | } |
||
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() |
||
246 | } |
||
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() |
||
256 | { |
||
257 | (defined('PHPUNIT_TESTSUITE') && PHPUNIT_TESTSUITE) || trigger_error(__METHOD__ . ' is deprecated and will be removed soon. Initialise with a preconfigured logger instance instead using "TelegramLog::initialize($logger)".', E_USER_DEPRECATED); |
||
1 ignored issue
–
show
|
|||
258 | return self::$debug_log_path !== null; |
||
259 | } |
||
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() |
||
269 | { |
||
270 | (defined('PHPUNIT_TESTSUITE') && PHPUNIT_TESTSUITE) || trigger_error(__METHOD__ . ' is deprecated and will be removed soon. Initialise with a preconfigured logger instance instead using "TelegramLog::initialize($logger)".', E_USER_DEPRECATED); |
||
1 ignored issue
–
show
|
|||
271 | return self::$update_log_path !== null; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Handle any logging method call. |
||
276 | * |
||
277 | * @param string $name |
||
278 | * @param array $arguments |
||
279 | */ |
||
280 | 6 | public static function __callStatic($name, array $arguments) |
|
306 | 6 | } |
|
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 | 4 | protected static function interpolate($message, array $context = []) |
|
331 | } |
||
332 | } |
||
333 |