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 | * Get the current depth of tasks. |
||
| 89 | * |
||
| 90 | * @return int Returns the current level. |
||
| 91 | */ |
||
| 92 | 11 | protected function currentLevel() { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Output a message that designates the beginning of a task. |
||
| 98 | * |
||
| 99 | * @param string $str The message to output. |
||
| 100 | * @return $this Returns `$this` for fluent calls. |
||
| 101 | */ |
||
| 102 | 8 | public function begin($str) { |
|
| 103 | 8 | $output = $this->currentLevel() <= $this->getMaxLevel(); |
|
| 104 | 8 | $task = [$str, microtime(true), $output]; |
|
|
|
|||
| 105 | |||
| 106 | 8 | if ($output) { |
|
| 107 | 8 | if (!$this->isNewline) { |
|
| 108 | 2 | $this->write($this->getEol()); |
|
| 109 | 2 | $this->isNewline = true; |
|
| 110 | } |
||
| 111 | |||
| 112 | 8 | $this->write($this->messageStr($str, false)); |
|
| 113 | 8 | $this->isNewline = false; |
|
| 114 | } |
||
| 115 | |||
| 116 | 8 | array_push($this->taskStack, $task); |
|
| 117 | |||
| 118 | 8 | return $this; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Output a message that designates a task being completed. |
||
| 123 | * |
||
| 124 | * @param string $str The message to output. |
||
| 125 | * @param bool $force Whether or not to always output the message even if the task is past the max depth. |
||
| 126 | * @return $this Returns `$this` for fluent calls. |
||
| 127 | */ |
||
| 128 | 8 | public function end($str = '', $force = false) { |
|
| 129 | // Find the task we are finishing. |
||
| 130 | 8 | $task = array_pop($this->taskStack); |
|
| 131 | 8 | if ($task !== null) { |
|
| 132 | 8 | list($taskStr, $taskTimestamp, $taskOutput) = $task; |
|
| 133 | 8 | $timespan = microtime(true) - $taskTimestamp; |
|
| 134 | } else { |
||
| 135 | 1 | trigger_error('Called LogFormatter::end() without calling LogFormatter::begin()', E_USER_NOTICE); |
|
| 136 | } |
||
| 137 | |||
| 138 | 8 | $pastMaxLevel = $this->currentLevel() > $this->getMaxLevel(); |
|
| 139 | 8 | if ($pastMaxLevel) { |
|
| 140 | 3 | if ($force && isset($taskStr) && isset($taskOutput)) { |
|
| 141 | 1 | if (!$taskOutput) { |
|
| 142 | // Output the full task string if it hasn't already been output. |
||
| 143 | 1 | $str = trim($taskStr.' '.$str); |
|
| 144 | } |
||
| 145 | 1 | if (!$this->isNewline) { |
|
| 146 | 1 | $this->write($this->getEol()); |
|
| 147 | 1 | $this->isNewline = true; |
|
| 148 | } |
||
| 149 | } else { |
||
| 150 | 2 | return $this; |
|
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | 8 | if (!empty($timespan) && $this->getShowDurations()) { |
|
| 155 | $str = trim($str.' '.$this->formatString($this->formatDuration($timespan), ["\033[1;34m", "\033[0m"])); |
||
| 156 | } |
||
| 157 | |||
| 158 | 8 | if ($this->isNewline) { |
|
| 159 | // Output the end message as a normal message. |
||
| 160 | 6 | $this->message($str, $force); |
|
| 161 | } else { |
||
| 162 | // Output the end message on the same line. |
||
| 163 | 4 | $this->write(' '.$str.$this->getEol()); |
|
| 164 | 4 | $this->isNewline = true; |
|
| 165 | } |
||
| 166 | |||
| 167 | 8 | return $this; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Output a message that represents a task being completed in success. |
||
| 172 | * |
||
| 173 | * When formatting is turned on, success messages are output in green. |
||
| 174 | * |
||
| 175 | * @param string $str The message to output. |
||
| 176 | * @param bool $force Whether or not to force a message past the max level to be output. |
||
| 177 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 178 | */ |
||
| 179 | public function endSuccess($str, $force = false) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Output a message that represents a task being completed in an error. |
||
| 185 | * |
||
| 186 | * When formatting is turned on, error messages are output in red. Error messages are always output even if they are |
||
| 187 | * past the maximum depth. |
||
| 188 | * |
||
| 189 | * @param string $str The message to output. |
||
| 190 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 191 | */ |
||
| 192 | 1 | public function endError($str) { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Output a message that ends a task with an HTTP status code. |
||
| 198 | * |
||
| 199 | * 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 |
||
| 200 | * response code to this message. |
||
| 201 | * |
||
| 202 | * @param int $httpStatus The HTTP status code that represents the completion of a task. |
||
| 203 | * @param bool $force Whether or not to force message output. |
||
| 204 | * @return $this Returns `$this` for fluent calls. |
||
| 205 | * @see LogFormatter::endSuccess(), LogFormatter::endError(). |
||
| 206 | */ |
||
| 207 | public function endHttpStatus($httpStatus, $force = false) { |
||
| 208 | $statusStr = sprintf('%03d', $httpStatus); |
||
| 209 | |||
| 210 | if ($httpStatus == 0 || $httpStatus >= 400) { |
||
| 211 | $this->endError($statusStr); |
||
| 212 | } elseif ($httpStatus >= 200 && $httpStatus < 300) { |
||
| 213 | $this->endSuccess($statusStr, $force); |
||
| 214 | } else { |
||
| 215 | $this->end($statusStr, $force); |
||
| 216 | } |
||
| 217 | |||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Format a time duration. |
||
| 223 | * |
||
| 224 | * @param float $duration The duration in seconds and fractions of a second. |
||
| 225 | * @return string Returns the duration formatted for humans. |
||
| 226 | * @see microtime() |
||
| 227 | */ |
||
| 228 | 1 | public function formatDuration($duration) { |
|
| 229 | 1 | if ($duration < 1.0e-3) { |
|
| 230 | 1 | $n = number_format($duration * 1.0e6, 0); |
|
| 231 | 1 | $sx = 'μs'; |
|
| 232 | 1 | } elseif ($duration < 1) { |
|
| 233 | 1 | $n = number_format($duration * 1000, 0); |
|
| 234 | 1 | $sx = 'ms'; |
|
| 235 | 1 | } elseif ($duration < 60) { |
|
| 236 | 1 | $n = number_format($duration, 1); |
|
| 237 | 1 | $sx = 's'; |
|
| 238 | 1 | } elseif ($duration < 3600) { |
|
| 239 | 1 | $n = number_format($duration / 60, 1); |
|
| 240 | 1 | $sx = 'm'; |
|
| 241 | 1 | } elseif ($duration < 86400) { |
|
| 242 | 1 | $n = number_format($duration / 3600, 1); |
|
| 243 | 1 | $sx = 'h'; |
|
| 244 | } else { |
||
| 245 | 1 | $n = number_format($duration / 86400, 1); |
|
| 246 | 1 | $sx = 'd'; |
|
| 247 | } |
||
| 248 | |||
| 249 | 1 | $result = rtrim($n, '0.').$sx; |
|
| 250 | 1 | return $result; |
|
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Output a message. |
||
| 255 | * |
||
| 256 | * @param string $str The message to output. |
||
| 257 | * @param bool $force Whether or not to force output of the message even if it's past the max depth. |
||
| 258 | * @return $this Returns `$this` for fluent calls. |
||
| 259 | */ |
||
| 260 | 10 | public function message($str, $force = false) { |
|
| 261 | 10 | $pastMaxLevel = $this->currentLevel() > $this->getMaxLevel(); |
|
| 262 | |||
| 263 | 10 | if ($pastMaxLevel) { |
|
| 264 | 5 | if ($force) { |
|
| 265 | // Trace down the task list and output everything that hasn't already been output. |
||
| 266 | 3 | foreach ($this->taskStack as $indent => $task) { |
|
| 267 | 3 | list($taskStr, $taskTimestamp, $taskOutput) = $this->taskStack[$indent]; |
|
| 268 | 3 | if (!$taskOutput) { |
|
| 269 | 1 | if (!$this->isNewline) { |
|
| 270 | 1 | $this->write($this->eol); |
|
| 271 | 1 | $this->isNewline = true; |
|
| 272 | } |
||
| 273 | 1 | $this->write($this->fullMessageStr($taskTimestamp, $taskStr, $indent, true)); |
|
| 274 | 1 | $this->taskStack[$indent][2] = true; |
|
| 275 | } else { |
||
| 276 | 3 | continue; |
|
| 277 | } |
||
| 278 | } |
||
| 279 | } else { |
||
| 280 | 5 | return $this; |
|
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | 9 | if (!$this->isNewline) { |
|
| 285 | 2 | $this->write($this->eol); |
|
| 286 | 2 | $this->isNewline = true; |
|
| 287 | } |
||
| 288 | 9 | $this->write($this->messageStr($str, true)); |
|
| 289 | 9 | return $this; |
|
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get whether or not output should be formatted. |
||
| 294 | * |
||
| 295 | * @return boolean Returns **true** if output should be formatted or **false** otherwise. |
||
| 296 | */ |
||
| 297 | public function getFormatOutput() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set whether or not output should be formatted. |
||
| 303 | * |
||
| 304 | * @param boolean $formatOutput Whether or not to format output. |
||
| 305 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 306 | */ |
||
| 307 | 11 | public function setFormatOutput($formatOutput) { |
|
| 311 | |||
| 312 | 11 | protected function fullMessageStr($timestamp, $str, $indent = null, $eol = true) { |
|
| 313 | 11 | if ($indent === null) { |
|
| 314 | 11 | $indent = $this->currentLevel() - 1; |
|
| 315 | } |
||
| 316 | |||
| 317 | 11 | if ($indent <= 0) { |
|
| 318 | 11 | $indentStr = ''; |
|
| 319 | 5 | } elseif ($indent === 1) { |
|
| 320 | 5 | $indentStr = '- '; |
|
| 321 | } else { |
||
| 322 | 2 | $indentStr = str_repeat(' ', $indent - 1).'- '; |
|
| 323 | } |
||
| 324 | |||
| 325 | 11 | $result = $indentStr.$str; |
|
| 326 | |||
| 327 | 11 | if ($this->getDateFormat()) { |
|
| 328 | 9 | $result = strftime($this->getDateFormat(), $timestamp).' '.$result; |
|
| 329 | } |
||
| 330 | |||
| 331 | 11 | if ($eol) { |
|
| 332 | 9 | $result .= $this->eol; |
|
| 333 | } |
||
| 334 | 11 | return $result; |
|
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Create a message string. |
||
| 339 | * |
||
| 340 | * @param string $str The message to output. |
||
| 341 | * @param bool $eol Whether or not to add an EOL. |
||
| 342 | * @return string Returns the message. |
||
| 343 | */ |
||
| 344 | 11 | protected function messageStr($str, $eol = true) { |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Format some text for the console. |
||
| 350 | * |
||
| 351 | * @param string $text The text to format. |
||
| 352 | * @param string[] $wrap The format to wrap in the form ['before', 'after']. |
||
| 353 | * @return string Returns the string formatted according to {@link Cli::$format}. |
||
| 354 | */ |
||
| 355 | 4 | protected function formatString($text, array $wrap) { |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get the maxLevel. |
||
| 365 | * |
||
| 366 | * @return int Returns the maxLevel. |
||
| 367 | */ |
||
| 368 | 11 | public function getMaxLevel() { |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param int $maxLevel |
||
| 374 | * @return LogFormatter |
||
| 375 | */ |
||
| 376 | 12 | public function setMaxLevel($maxLevel) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get the date format as passed to {@link strftime()}. |
||
| 387 | * |
||
| 388 | * @return string Returns the date format. |
||
| 389 | * @see strftime() |
||
| 390 | */ |
||
| 391 | 11 | public function getDateFormat() { |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Set the date format as passed to {@link strftime()}. |
||
| 397 | * |
||
| 398 | * @param string $dateFormat |
||
| 399 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 400 | * @see strftime() |
||
| 401 | */ |
||
| 402 | 11 | public function setDateFormat($dateFormat) { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Get the end of line string to use. |
||
| 409 | * |
||
| 410 | * @return string Returns the eol string. |
||
| 411 | */ |
||
| 412 | 6 | public function getEol() { |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Set the end of line string. |
||
| 418 | * |
||
| 419 | * @param string $eol The end of line string to use. |
||
| 420 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 421 | */ |
||
| 422 | 11 | public function setEol($eol) { |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Get the showDurations. |
||
| 429 | * |
||
| 430 | * @return boolean Returns the showDurations. |
||
| 431 | */ |
||
| 432 | 8 | public function getShowDurations() { |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Set the showDurations. |
||
| 438 | * |
||
| 439 | * @param boolean $showDurations |
||
| 440 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 441 | */ |
||
| 442 | 11 | public function setShowDurations($showDurations) { |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Set the output file handle. |
||
| 449 | * |
||
| 450 | * @param resource $handle |
||
| 451 | * @return LogFormatter Returns `$this` for fluent calls. |
||
| 452 | */ |
||
| 453 | public function setOutputHandle($handle) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Write a string to the CLI. |
||
| 463 | * |
||
| 464 | * This method is intended to centralize the echoing of output in case the class is subclassed and the behaviour |
||
| 465 | * needs to change. |
||
| 466 | * |
||
| 467 | * @param string $str The string to write. |
||
| 468 | */ |
||
| 469 | 11 | public function write($str) { |
|
| 476 | } |
||
| 477 |
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.