Complex classes like LogFormatter 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 LogFormatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class LogFormatter { |
||
| 14 | /** |
||
| 15 | * @var string The date format as passed to {@link strftime()}. |
||
| 16 | */ |
||
| 17 | protected $dateFormat = '[%F %T]'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string The end of line string to use. |
||
| 21 | */ |
||
| 22 | protected $eol = PHP_EOL; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var bool Whether or not to format output. |
||
| 26 | */ |
||
| 27 | protected $formatOutput; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var resource The output file handle. |
||
| 31 | */ |
||
| 32 | protected $outputHandle; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool Whether or not the console is on a new line. |
||
| 36 | */ |
||
| 37 | protected $isNewline = true; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int The maximum level deep to output. |
||
| 41 | */ |
||
| 42 | protected $maxLevel = 2; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool Whether or not to show durations for tasks. |
||
| 46 | */ |
||
| 47 | protected $showDurations = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array An array of currently running tasks. |
||
| 51 | */ |
||
| 52 | protected $taskStack = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * LogFormatter constructor. |
||
| 56 | */ |
||
| 57 | 13 | public function __construct() { |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Output an error message. |
||
| 64 | * |
||
| 65 | * When formatting is turned on, error messages are displayed in red. Error messages are always output, even if they |
||
| 66 | * are past the maximum display level. |
||
| 67 | * |
||
| 68 | * @param string $str The message to output. |
||
| 69 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 70 | */ |
||
| 71 | 3 | public function error($str) { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Output a success message. |
||
| 77 | * |
||
| 78 | * When formatting is turned on, success messages are displayed in green. |
||
| 79 | * |
||
| 80 | * @param string $str The message to output. |
||
| 81 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 82 | */ |
||
| 83 | 1 | public function success($str) { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Output a warning message. |
||
| 89 | * |
||
| 90 | * When formatting is turned on, warning messages are displayed in yellow. |
||
| 91 | * |
||
| 92 | * @param string $str The message to output. |
||
| 93 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 94 | */ |
||
| 95 | public function warn($str) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the current depth of tasks. |
||
| 101 | * |
||
| 102 | * @return int Returns the current level. |
||
| 103 | */ |
||
| 104 | 11 | protected function currentLevel() { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Output a message that designates the beginning of a task. |
||
| 110 | * |
||
| 111 | * @param string $str The message to output. |
||
| 112 | * @return $this Returns `$this` for fluent calls. |
||
| 113 | */ |
||
| 114 | 8 | public function begin($str) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Output a message that designates a task being completed. |
||
| 135 | * |
||
| 136 | * @param string $str The message to output. |
||
| 137 | * @param bool $force Whether or not to always output the message even if the task is past the max depth. |
||
| 138 | * @return $this Returns `$this` for fluent calls. |
||
| 139 | */ |
||
| 140 | 8 | public function end($str = '', $force = false) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Output a message that represents a task being completed in success. |
||
| 184 | * |
||
| 185 | * When formatting is turned on, success messages are output in green. |
||
| 186 | * |
||
| 187 | * @param string $str The message to output. |
||
| 188 | * @param bool $force Whether or not to force a message past the max level to be output. |
||
| 189 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 190 | */ |
||
| 191 | public function endSuccess($str, $force = false) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Output a message that represents a task being completed in an error. |
||
| 197 | * |
||
| 198 | * When formatting is turned on, error messages are output in red. Error messages are always output even if they are |
||
| 199 | * past the maximum depth. |
||
| 200 | * |
||
| 201 | * @param string $str The message to output. |
||
| 202 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 203 | */ |
||
| 204 | 1 | public function endError($str) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Output a message that ends a task with an HTTP status code. |
||
| 210 | * |
||
| 211 | * This method is useful if you are making a call to an external API as a task. You can end the task by passing the |
||
| 212 | * response code to this message. |
||
| 213 | * |
||
| 214 | * @param int $httpStatus The HTTP status code that represents the completion of a task. |
||
| 215 | * @param bool $force Whether or not to force message output. |
||
| 216 | * @return $this Returns `$this` for fluent calls. |
||
| 217 | * @see LogFormatter::endSuccess(), LogFormatter::endError(). |
||
| 218 | */ |
||
| 219 | public function endHttpStatus($httpStatus, $force = false) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Format a time duration. |
||
| 235 | * |
||
| 236 | * @param float $duration The duration in seconds and fractions of a second. |
||
| 237 | * @return string Returns the duration formatted for humans. |
||
| 238 | * @see microtime() |
||
| 239 | */ |
||
| 240 | 1 | public function formatDuration($duration) { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Output a message. |
||
| 267 | * |
||
| 268 | * @param string $str The message to output. |
||
| 269 | * @param bool $force Whether or not to force output of the message even if it's past the max depth. |
||
| 270 | * @return $this Returns `$this` for fluent calls. |
||
| 271 | */ |
||
| 272 | 10 | public function message($str, $force = false) { |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Get whether or not output should be formatted. |
||
| 306 | * |
||
| 307 | * @return boolean Returns **true** if output should be formatted or **false** otherwise. |
||
| 308 | */ |
||
| 309 | public function getFormatOutput() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set whether or not output should be formatted. |
||
| 315 | * |
||
| 316 | * @param boolean $formatOutput Whether or not to format output. |
||
| 317 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 318 | */ |
||
| 319 | 11 | public function setFormatOutput($formatOutput) { |
|
| 323 | |||
| 324 | 11 | protected function fullMessageStr($timestamp, $str, $indent = null, $eol = true) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Create a message string. |
||
| 351 | * |
||
| 352 | * @param string $str The message to output. |
||
| 353 | * @param bool $eol Whether or not to add an EOL. |
||
| 354 | * @return string Returns the message. |
||
| 355 | */ |
||
| 356 | 11 | protected function messageStr($str, $eol = true) { |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Format some text for the console. |
||
| 362 | * |
||
| 363 | * @param string $text The text to format. |
||
| 364 | * @param string[] $wrap The format to wrap in the form ['before', 'after']. |
||
| 365 | * @return string Returns the string formatted according to {@link Cli::$format}. |
||
| 366 | */ |
||
| 367 | 4 | protected function formatString($text, array $wrap) { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Get the maxLevel. |
||
| 377 | * |
||
| 378 | * @return int Returns the maxLevel. |
||
| 379 | */ |
||
| 380 | 11 | public function getMaxLevel() { |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @param int $maxLevel |
||
| 386 | * @return LogFormatter |
||
| 387 | */ |
||
| 388 | 12 | public function setMaxLevel($maxLevel) { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get the date format as passed to {@link strftime()}. |
||
| 399 | * |
||
| 400 | * @return string Returns the date format. |
||
| 401 | * @see strftime() |
||
| 402 | */ |
||
| 403 | 11 | public function getDateFormat() { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Set the date format as passed to {@link strftime()}. |
||
| 409 | * |
||
| 410 | * @param string $dateFormat |
||
| 411 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 412 | * @see strftime() |
||
| 413 | */ |
||
| 414 | 11 | public function setDateFormat($dateFormat) { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Get the end of line string to use. |
||
| 421 | * |
||
| 422 | * @return string Returns the eol string. |
||
| 423 | */ |
||
| 424 | 6 | public function getEol() { |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Set the end of line string. |
||
| 430 | * |
||
| 431 | * @param string $eol The end of line string to use. |
||
| 432 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 433 | */ |
||
| 434 | 11 | public function setEol($eol) { |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Get the showDurations. |
||
| 441 | * |
||
| 442 | * @return boolean Returns the showDurations. |
||
| 443 | */ |
||
| 444 | 8 | public function getShowDurations() { |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Set the showDurations. |
||
| 450 | * |
||
| 451 | * @param boolean $showDurations |
||
| 452 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 453 | */ |
||
| 454 | 11 | public function setShowDurations($showDurations) { |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Set the output file handle. |
||
| 461 | * |
||
| 462 | * @param resource $handle |
||
| 463 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 464 | */ |
||
| 465 | public function setOutputHandle($handle) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Write a string to the CLI. |
||
| 475 | * |
||
| 476 | * This method is intended to centralize the echoing of output in case the class is subclassed and the behaviour |
||
| 477 | * needs to change. |
||
| 478 | * |
||
| 479 | * @param string $str The string to write. |
||
| 480 | */ |
||
| 481 | 11 | public function write($str) { |
|
| 488 | } |
||
| 489 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.