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 bool Whether or not the console is on a new line. |
||
| 31 | */ |
||
| 32 | protected $isNewline = true; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int The maximum level deep to output. |
||
| 36 | */ |
||
| 37 | protected $maxLevel = 2; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool Whether or not to show durations for tasks. |
||
| 41 | */ |
||
| 42 | protected $showDurations = true; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array An array of currently running tasks. |
||
| 46 | */ |
||
| 47 | protected $taskStack = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * LogFormatter constructor. |
||
| 51 | */ |
||
| 52 | public function __construct() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Output an error message. |
||
| 58 | * |
||
| 59 | * When formatting is turned on, error messages are displayed in red. Error messages are always output, even if they |
||
| 60 | * are past the maximum display level. |
||
| 61 | * |
||
| 62 | * @param string $str The message to output. |
||
| 63 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 64 | */ |
||
| 65 | public function error($str) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Output a success message. |
||
| 71 | * |
||
| 72 | * When formatting is turned on, success messages are displayed in green. |
||
| 73 | * |
||
| 74 | * @param string $str The message to output. |
||
| 75 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 76 | */ |
||
| 77 | public function success($str) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get the current depth of tasks. |
||
| 83 | * |
||
| 84 | * @return int Returns the current level. |
||
| 85 | */ |
||
| 86 | protected function currentLevel() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Output a message that designates the beginning of a task. |
||
| 92 | * |
||
| 93 | * @param string $str The message to output. |
||
| 94 | * @return $this Returns `$this` for fluent calls. |
||
| 95 | */ |
||
| 96 | public function begin($str) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Output a message that designates a task being completed. |
||
| 117 | * |
||
| 118 | * @param string $str The message to output. |
||
| 119 | * @param bool $force Whether or not to always output the message even if the task is past the max depth. |
||
| 120 | * @return $this Returns `$this` for fluent calls. |
||
| 121 | */ |
||
| 122 | public function end($str = '', $force = false) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Output a message that represents a task being completed in success. |
||
| 166 | * |
||
| 167 | * When formatting is turned on, success messages are output in green. |
||
| 168 | * |
||
| 169 | * @param string $str The message to output. |
||
| 170 | * @param bool $force Whether or not to force a message past the max level to be output. |
||
| 171 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 172 | */ |
||
| 173 | public function endSuccess($str, $force = false) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Output a message that represents a task being completed in an error. |
||
| 179 | * |
||
| 180 | * When formatting is turned on, error messages are output in red. Error messages are always output even if they are |
||
| 181 | * past the maximum depth. |
||
| 182 | * |
||
| 183 | * @param string $str The message to output. |
||
| 184 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 185 | */ |
||
| 186 | public function endError($str) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Output a message that ends a task with an HTTP status code. |
||
| 192 | * |
||
| 193 | * 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 |
||
| 194 | * response code to this message. |
||
| 195 | * |
||
| 196 | * @param int $httpStatus The HTTP status code that represents the completion of a task. |
||
| 197 | * @param bool $force Whether or not to force message output. |
||
| 198 | * @return $this Returns `$this` for fluent calls. |
||
| 199 | * @see LogFormatter::endSuccess(), LogFormatter::endError(). |
||
| 200 | */ |
||
| 201 | public function endHttpStatus($httpStatus, $force = false) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Format a time duration. |
||
| 217 | * |
||
| 218 | * @param float $duration The duration in seconds and fractions of a second. |
||
| 219 | * @return string Returns the duration formatted for humans. |
||
| 220 | * @see microtime() |
||
| 221 | */ |
||
| 222 | public function formatDuration($duration) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Output a message. |
||
| 251 | * |
||
| 252 | * @param string $str The message to output. |
||
| 253 | * @param bool $force Whether or not to force output of the message even if it's past the max depth. |
||
| 254 | * @return $this Returns `$this` for fluent calls. |
||
| 255 | */ |
||
| 256 | public function message($str, $force = false) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get whether or not output should be formatted. |
||
| 290 | * |
||
| 291 | * @return boolean Returns **true** if output should be formatted or **false** otherwise. |
||
| 292 | */ |
||
| 293 | public function getFormatOutput() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set whether or not output should be formatted. |
||
| 299 | * |
||
| 300 | * @param boolean $formatOutput Whether or not to format output. |
||
| 301 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 302 | */ |
||
| 303 | public function setFormatOutput($formatOutput) { |
||
| 307 | |||
| 308 | protected function fullMessageStr($timestamp, $str, $indent = null, $eol = true) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Create a message string. |
||
| 335 | * |
||
| 336 | * @param string $str The message to output. |
||
| 337 | * @param bool $eol Whether or not to add an EOL. |
||
| 338 | * @return string Returns the message. |
||
| 339 | */ |
||
| 340 | protected function messageStr($str, $eol = true) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Format some text for the console. |
||
| 346 | * |
||
| 347 | * @param string $text The text to format. |
||
| 348 | * @param string[] $wrap The format to wrap in the form ['before', 'after']. |
||
| 349 | * @return string Returns the string formatted according to {@link Cli::$format}. |
||
| 350 | */ |
||
| 351 | protected function formatString($text, array $wrap) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get the maxLevel. |
||
| 361 | * |
||
| 362 | * @return int Returns the maxLevel. |
||
| 363 | */ |
||
| 364 | public function getMaxLevel() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param int $maxLevel |
||
| 370 | * @return LogFormatter |
||
| 371 | */ |
||
| 372 | public function setMaxLevel($maxLevel) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the date format as passed to {@link strftime()}. |
||
| 383 | * |
||
| 384 | * @return string Returns the date format. |
||
| 385 | * @see strftime() |
||
| 386 | */ |
||
| 387 | public function getDateFormat() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set the date format as passed to {@link strftime()}. |
||
| 393 | * |
||
| 394 | * @param string $dateFormat |
||
| 395 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 396 | * @see strftime() |
||
| 397 | */ |
||
| 398 | public function setDateFormat($dateFormat) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get the end of line string to use. |
||
| 405 | * |
||
| 406 | * @return string Returns the eol string. |
||
| 407 | */ |
||
| 408 | public function getEol() { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Set the end of line string. |
||
| 414 | * |
||
| 415 | * @param string $eol The end of line string to use. |
||
| 416 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 417 | */ |
||
| 418 | public function setEol($eol) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get the showDurations. |
||
| 425 | * |
||
| 426 | * @return boolean Returns the showDurations. |
||
| 427 | */ |
||
| 428 | public function getShowDurations() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Set the showDurations. |
||
| 434 | * |
||
| 435 | * @param boolean $showDurations |
||
| 436 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 437 | */ |
||
| 438 | public function setShowDurations($showDurations) { |
||
| 442 | } |
||
| 443 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.