Complex classes like XoopsLogger 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 XoopsLogger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class XoopsLogger |
||
34 | { |
||
35 | /** |
||
36 | * *#@+ |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | public $queries = array(); |
||
41 | public $blocks = array(); |
||
42 | public $extra = array(); |
||
43 | public $logstart = array(); |
||
44 | public $logend = array(); |
||
45 | public $errors = array(); |
||
46 | public $deprecated = array(); |
||
47 | /** |
||
48 | * *#@- |
||
49 | */ |
||
50 | |||
51 | public $usePopup = false; |
||
52 | public $activated = true; |
||
53 | |||
54 | /** |
||
55 | * *@access protected |
||
56 | */ |
||
57 | public $renderingEnabled = false; |
||
58 | |||
59 | /** |
||
60 | * XoopsLogger::__construct() |
||
61 | */ |
||
62 | public function __construct() |
||
65 | |||
66 | /** |
||
67 | * XoopsLogger::XoopsLogger() |
||
68 | */ |
||
69 | public function XoopsLogger() |
||
72 | |||
73 | /** |
||
74 | * Deprecated, use getInstance() instead |
||
75 | */ |
||
76 | public function instance() |
||
80 | |||
81 | /** |
||
82 | * Get a reference to the only instance of this class |
||
83 | * |
||
84 | * @return object XoopsLogger reference to the only instance |
||
85 | */ |
||
86 | public static function getInstance() |
||
99 | |||
100 | /** |
||
101 | * Enable logger output rendering |
||
102 | * When output rendering is enabled, the logger will insert its output within the page content. |
||
103 | * If the string <!--{xo-logger-output}--> is found in the page content, the logger output will |
||
104 | * replace it, otherwise it will be inserted after all the page output. |
||
105 | */ |
||
106 | public function enableRendering() |
||
113 | |||
114 | /** |
||
115 | * Returns the current microtime in seconds. |
||
116 | * |
||
117 | * @return float |
||
118 | */ |
||
119 | public function microtime() |
||
125 | |||
126 | /** |
||
127 | * Start a timer |
||
128 | * |
||
129 | * @param string $name name of the timer |
||
130 | */ |
||
131 | public function startTime($name = 'XOOPS') |
||
137 | |||
138 | /** |
||
139 | * Stop a timer |
||
140 | * |
||
141 | * @param string $name name of the timer |
||
142 | */ |
||
143 | public function stopTime($name = 'XOOPS') |
||
149 | |||
150 | /** |
||
151 | * Log a database query |
||
152 | * |
||
153 | * @param string $sql SQL string |
||
154 | * @param string $error error message (if any) |
||
155 | * @param int $errno error number (if any) |
||
156 | * @param null $query_time |
||
157 | */ |
||
158 | public function addQuery($sql, $error = null, $errno = null, $query_time = null) |
||
164 | |||
165 | /** |
||
166 | * Log display of a block |
||
167 | * |
||
168 | * @param string $name name of the block |
||
169 | * @param bool $cached was the block cached? |
||
170 | * @param int $cachetime cachetime of the block |
||
171 | */ |
||
172 | public function addBlock($name, $cached = false, $cachetime = 0) |
||
178 | |||
179 | /** |
||
180 | * Log extra information |
||
181 | * |
||
182 | * @param string $name name for the entry |
||
183 | * @param int $msg text message for the entry |
||
184 | */ |
||
185 | public function addExtra($name, $msg) |
||
191 | |||
192 | /** |
||
193 | * Log messages for deprecated functions |
||
194 | * |
||
195 | * @deprecated |
||
196 | * |
||
197 | * @param int $msg text message for the entry |
||
198 | * |
||
199 | */ |
||
200 | public function addDeprecated($msg) |
||
206 | |||
207 | /** |
||
208 | * Error handling callback (called by the zend engine) |
||
209 | * |
||
210 | * @param integer $errno |
||
211 | * @param string $errstr |
||
212 | * @param string $errfile |
||
213 | * @param string $errline |
||
214 | */ |
||
215 | public function handleError($errno, $errstr, $errfile, $errline) |
||
243 | |||
244 | /** |
||
245 | * Exception handling callback. |
||
246 | * |
||
247 | * @param \Exception|\Throwable $e uncaught Exception or Error |
||
248 | * |
||
249 | * @return void |
||
250 | */ |
||
251 | public function handleException($e) |
||
258 | |||
259 | /** |
||
260 | * Determine if an object implements Throwable (or is an Exception that would under PHP 7.) |
||
261 | * |
||
262 | * @param mixed $e Expected to be an object related to Exception or Throwable |
||
263 | * |
||
264 | * @return bool true if related to Throwable or Exception, otherwise false |
||
265 | */ |
||
266 | protected function isThrowable($e) |
||
271 | |||
272 | /** |
||
273 | * |
||
274 | * @access protected |
||
275 | * |
||
276 | * @param string $path |
||
277 | * |
||
278 | * @return mixed|string |
||
279 | */ |
||
280 | public function sanitizePath($path) |
||
286 | |||
287 | /** |
||
288 | * Output buffering callback inserting logger dump in page output |
||
289 | * @param $output |
||
290 | * @return string |
||
291 | */ |
||
292 | public function render($output) |
||
309 | |||
310 | /** |
||
311 | * *#@+ |
||
312 | * |
||
313 | * @protected |
||
314 | * @param string $mode |
||
315 | * @return |
||
316 | */ |
||
317 | public function dump($mode = '') |
||
323 | |||
324 | /** |
||
325 | * get the current execution time of a timer |
||
326 | * |
||
327 | * @param string $name name of the counter |
||
328 | * @param bool $unset removes counter from global log |
||
329 | * @return float current execution time of the counter |
||
330 | */ |
||
331 | public function dumpTime($name = 'XOOPS', $unset = false) |
||
349 | |||
350 | /** |
||
351 | * XoopsLogger::triggerError() |
||
352 | * |
||
353 | * @deprecated |
||
354 | * @param int $errkey |
||
355 | * @param string $errStr |
||
356 | * @param string $errFile |
||
357 | * @param string $errLine |
||
358 | * @param integer $errNo |
||
359 | * @return void |
||
360 | */ |
||
361 | public function triggerError($errkey = 0, $errStr = '', $errFile = '', $errLine = '', $errNo = 0) |
||
371 | |||
372 | /** |
||
373 | * *#@+ |
||
374 | * |
||
375 | * @deprecated |
||
376 | */ |
||
377 | public function dumpAll() |
||
383 | |||
384 | /** |
||
385 | * dnmp Blocks @deprecated |
||
386 | * |
||
387 | * @return dump |
||
388 | */ |
||
389 | public function dumpBlocks() |
||
395 | |||
396 | /** |
||
397 | * dumpExtra @deprecated |
||
398 | * |
||
399 | * @return dimp |
||
400 | */ |
||
401 | public function dumpExtra() |
||
407 | |||
408 | /** |
||
409 | * dump Queries @deprecated |
||
410 | * |
||
411 | * @return unknown |
||
412 | */ |
||
413 | public function dumpQueries() |
||
419 | /** |
||
420 | * *#@- |
||
421 | */ |
||
422 | } |
||
423 | |||
452 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.