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 LegacyLogger 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 LegacyLogger, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class LegacyLogger implements LoggerInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var $queries array of query log lines |
||
| 35 | */ |
||
| 36 | protected $queries = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var $blocks array of block log lines |
||
| 40 | */ |
||
| 41 | protected $blocks = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var $extra array of extra log lines |
||
| 45 | */ |
||
| 46 | protected $extra = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var start time information by name |
||
| 50 | */ |
||
| 51 | protected $logstart = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var end time information by name |
||
| 55 | */ |
||
| 56 | protected $logend = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var $errors array of error log lines |
||
| 60 | */ |
||
| 61 | protected $errors = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var $deprecated array of deprecated log lines |
||
| 65 | */ |
||
| 66 | protected $deprecated = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var true if rendering enables |
||
| 70 | */ |
||
| 71 | protected $renderingEnabled = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var true is logging is activated |
||
| 75 | */ |
||
| 76 | protected $activated = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var mixed boolean false if configs no set or read, array of module/user configs |
||
| 80 | */ |
||
| 81 | protected $configs = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * constructor |
||
| 85 | */ |
||
| 86 | public function __construct() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var true if rendering enables |
||
| 93 | */ |
||
| 94 | protected $usePopup = false; |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Get a reference to the only instance of this class |
||
| 99 | * |
||
| 100 | * @return object LoggerAbstract reference to the only instance |
||
| 101 | */ |
||
| 102 | public static function getInstance() |
||
| 103 | { |
||
| 104 | static $instance; |
||
| 105 | if (!isset($instance)) { |
||
| 106 | $class = __CLASS__; |
||
| 107 | $instance = new $class(); |
||
| 108 | } |
||
| 109 | |||
| 110 | return $instance; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Save a copy of our config array |
||
| 115 | * |
||
| 116 | * @param array $configs array of module/user config options |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | public function setConfigs($configs) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * disable logging |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function disable() |
||
| 131 | { |
||
| 132 | //error_reporting(0); |
||
| 133 | $this->activated = false; |
||
|
|
|||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Enable logger output rendering |
||
| 138 | * When output rendering is enabled, the logger will insert its output within the page content. |
||
| 139 | * If the string <!--{xo-logger-output}--> is found in the page content, the logger output will |
||
| 140 | * replace it, otherwise it will be inserted after all the page output. |
||
| 141 | * |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public function enable() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * report enabled status |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function isEnable() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * disable output for the benefit of ajax scripts |
||
| 169 | * |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function quiet() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Add our resources to the theme as soon as it is available, otherwise return |
||
| 179 | * |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | private function addToTheme() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Start a timer |
||
| 201 | * |
||
| 202 | * @param string $name name of the timer |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | public function startTime($name = 'XOOPS') |
||
| 207 | { |
||
| 208 | if ($this->activated) { |
||
| 209 | $this->logstart[$name] = microtime(true); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Stop a timer |
||
| 215 | * |
||
| 216 | * @param string $name name of the timer |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function stopTime($name = 'XOOPS') |
||
| 221 | { |
||
| 222 | if ($this->activated) { |
||
| 223 | $this->logend[$name] = microtime(true); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Log a database query |
||
| 229 | * |
||
| 230 | * @param string $sql sql that was processed |
||
| 231 | * @param string $error error message |
||
| 232 | * @param int $errno error number |
||
| 233 | * @param float $query_time execution time |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | public function addQuery($sql, $error = null, $errno = null, $query_time = null) |
||
| 238 | { |
||
| 239 | if ($this->activated) { |
||
| 240 | $this->queries[] = array( |
||
| 241 | 'sql' => $sql, 'error' => $error, 'errno' => $errno, 'query_time' => $query_time |
||
| 242 | ); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Log display of a block |
||
| 248 | * |
||
| 249 | * @param string $name name of the block |
||
| 250 | * @param bool $cached was the block cached? |
||
| 251 | * @param int $cachetime cachetime of the block |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | public function addBlock($name, $cached = false, $cachetime = 0) |
||
| 256 | { |
||
| 257 | if ($this->activated) { |
||
| 258 | $this->blocks[] = array('name' => $name, 'cached' => $cached, 'cachetime' => $cachetime); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Log extra information |
||
| 264 | * |
||
| 265 | * @param string $name name for the entry |
||
| 266 | * @param string $msg text message for the entry |
||
| 267 | * |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | public function addExtra($name, $msg) |
||
| 271 | { |
||
| 272 | if ($this->activated) { |
||
| 273 | $this->extra[] = array('name' => $name, 'msg' => $msg); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Log messages for deprecated functions |
||
| 279 | * |
||
| 280 | * @param string $msg name for the entry |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function addDeprecated($msg) |
||
| 285 | { |
||
| 286 | if ($this->activated) { |
||
| 287 | $this->deprecated[] = $msg; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Log exceptions |
||
| 293 | * |
||
| 294 | * @param Exception $e name for the entry |
||
| 295 | * |
||
| 296 | * @return void |
||
| 297 | */ |
||
| 298 | public function addException($e) |
||
| 299 | { |
||
| 300 | if ($this->activated) { |
||
| 301 | $this->log( |
||
| 302 | LogLevel::ERROR, |
||
| 303 | 'Exception: ' . $e->getMessage() . ' - ' . |
||
| 304 | $this->sanitizePath($e->getFile()) . ' ' . $e->getLine() |
||
| 305 | ); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * sanitizePath |
||
| 311 | * |
||
| 312 | * @param string $path path name to sanitize |
||
| 313 | * |
||
| 314 | * @return string path with top levels removed |
||
| 315 | */ |
||
| 316 | View Code Duplication | public function sanitizePath($path) |
|
| 317 | { |
||
| 318 | $path = str_replace( |
||
| 319 | array( |
||
| 320 | '\\', |
||
| 321 | \XoopsBaseConfig::get('root-path'), |
||
| 322 | str_replace('\\', '/', realpath(\XoopsBaseConfig::get('root-path'))) |
||
| 323 | ), |
||
| 324 | array('/', '', ''), |
||
| 325 | $path |
||
| 326 | ); |
||
| 327 | return $path; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Enable logger output rendering |
||
| 332 | * When output rendering is enabled, the logger will insert its output within the page content. |
||
| 333 | * If the string <!--{xo-logger-output}--> is found in the page content, the logger output will |
||
| 334 | * replace it, otherwise it will be inserted after all the page output. |
||
| 335 | * |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | public function enableRendering() |
||
| 339 | { |
||
| 340 | if (!$this->renderingEnabled) { |
||
| 341 | ob_start(array(&$this, 'render')); |
||
| 342 | $this->renderingEnabled = true; |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Output buffering callback inserting logger dump in page output |
||
| 348 | * |
||
| 349 | * @param string $output output buffer to add logger rendering to |
||
| 350 | * |
||
| 351 | * @return string output |
||
| 352 | */ |
||
| 353 | public function render($output) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Dump output |
||
| 373 | * |
||
| 374 | * @param string $mode unused |
||
| 375 | * |
||
| 376 | * @return string output |
||
| 377 | */ |
||
| 378 | public function dump($mode = '') |
||
| 590 | |||
| 591 | /** |
||
| 592 | * get the current execution time of a timer |
||
| 593 | * |
||
| 594 | * @param string $name name of the counter |
||
| 595 | * @param bool $unset removes counter from global log |
||
| 596 | * |
||
| 597 | * @return float current execution time of the counter |
||
| 598 | */ |
||
| 599 | public function dumpTime($name = 'XOOPS', $unset = false) |
||
| 600 | { |
||
| 601 | if (!$this->activated) { |
||
| 618 | |||
| 619 | |||
| 620 | /** |
||
| 621 | * PSR-3 System is unusable. |
||
| 622 | * |
||
| 623 | * @param string $message message |
||
| 624 | * @param array $context array of additional context |
||
| 625 | * |
||
| 626 | * @return null |
||
| 627 | */ |
||
| 628 | public function emergency($message, array $context = array()) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * PSR-3 Action must be taken immediately. |
||
| 637 | * |
||
| 638 | * Example: Entire website down, database unavailable, etc. This should |
||
| 639 | * trigger the SMS alerts and wake you up. |
||
| 640 | * |
||
| 641 | * @param string $message message |
||
| 642 | * @param array $context array of additional context |
||
| 643 | * |
||
| 644 | * @return null |
||
| 645 | */ |
||
| 646 | public function alert($message, array $context = array()) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * PSR-3 Critical conditions. |
||
| 655 | * |
||
| 656 | * Example: Application component unavailable, unexpected exception. |
||
| 657 | * |
||
| 658 | * @param string $message message |
||
| 659 | * @param array $context array of additional context |
||
| 660 | * |
||
| 661 | * @return null |
||
| 662 | */ |
||
| 663 | public function critical($message, array $context = array()) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * PSR-3 Runtime errors that do not require immediate action but should typically |
||
| 672 | * be logged and monitored. |
||
| 673 | * |
||
| 674 | * @param string $message message |
||
| 675 | * @param array $context array of additional context |
||
| 676 | * |
||
| 677 | * @return null |
||
| 678 | */ |
||
| 679 | public function error($message, array $context = array()) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * PSR-3 Exceptional occurrences that are not errors. |
||
| 688 | * |
||
| 689 | * Example: Use of deprecated APIs, poor use of an API, undesirable things |
||
| 690 | * that are not necessarily wrong. |
||
| 691 | * |
||
| 692 | * @param string $message message |
||
| 693 | * @param array $context array of additional context |
||
| 694 | * |
||
| 695 | * @return null |
||
| 696 | */ |
||
| 697 | public function warning($message, array $context = array()) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * PSR-3 Normal but significant events. |
||
| 706 | * |
||
| 707 | * @param string $message message |
||
| 708 | * @param array $context array of additional context |
||
| 709 | * |
||
| 710 | * @return null |
||
| 711 | */ |
||
| 712 | public function notice($message, array $context = array()) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * PSR-3 Interesting events. |
||
| 721 | * |
||
| 722 | * Example: User logs in, SQL logs. |
||
| 723 | * |
||
| 724 | * @param string $message message |
||
| 725 | * @param array $context array of additional context |
||
| 726 | * |
||
| 727 | * @return null |
||
| 728 | */ |
||
| 729 | public function info($message, array $context = array()) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * PSR-3 Detailed debug information. |
||
| 738 | * |
||
| 739 | * @param string $message message |
||
| 740 | * @param array $context array of additional context |
||
| 741 | * |
||
| 742 | * @return null |
||
| 743 | */ |
||
| 744 | public function debug($message, array $context = array()) |
||
| 750 | |||
| 751 | /** |
||
| 752 | * PSR-3 Logs with an arbitrary level. |
||
| 753 | * |
||
| 754 | * @param mixed $level logging level |
||
| 755 | * @param string $message message |
||
| 756 | * @param array $context array of additional context |
||
| 757 | * |
||
| 758 | * @return null |
||
| 759 | */ |
||
| 760 | public function log($level, $message, array $context = array()) |
||
| 768 | } |
||
| 769 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..