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() { |
|
58 | $this->formatOutput = Cli::guessFormatOutput(); |
||
59 | $this->outputHandle = fopen('php://output', 'w'); |
||
60 | 13 | } |
|
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 | 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 | public function success($str) { |
||
86 | |||
87 | /** |
||
88 | * Get the current depth of tasks. |
||
89 | * |
||
90 | * @return int Returns the current level. |
||
91 | */ |
||
92 | 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 | 6 | public function begin($str) { |
|
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 | 4 | public function end($str = '', $force = false) { |
|
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 | 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) { |
||
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 | if ($duration < 1.0e-3) { |
||
230 | $n = number_format($duration * 1.0e6, 0); |
||
1 ignored issue
–
show
|
|||
231 | 1 | $sx = 'μs'; |
|
232 | } elseif ($duration < 1) { |
||
233 | $n = number_format($duration * 1000, 0); |
||
1 ignored issue
–
show
|
|||
234 | 1 | $sx = 'ms'; |
|
235 | } elseif ($duration < 60) { |
||
236 | $n = number_format($duration, 1); |
||
1 ignored issue
–
show
|
|||
237 | 1 | $sx = 's'; |
|
238 | } elseif ($duration < 3600) { |
||
239 | $n = number_format($duration / 60, 1); |
||
1 ignored issue
–
show
|
|||
240 | 1 | $sx = 'm'; |
|
241 | } elseif ($duration < 86400) { |
||
242 | $n = number_format($duration / 3600, 1); |
||
1 ignored issue
–
show
|
|||
243 | 1 | $sx = 'h'; |
|
244 | } else { |
||
245 | $n = number_format($duration / 86400, 1); |
||
1 ignored issue
–
show
|
|||
246 | 1 | $sx = 'd'; |
|
247 | } |
||
248 | |||
249 | $result = rtrim($n, '0.').$sx; |
||
250 | 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 | 8 | public function message($str, $force = false) { |
|
261 | $pastMaxLevel = $this->currentLevel() > $this->getMaxLevel(); |
||
262 | |||
263 | 4 | if ($pastMaxLevel) { |
|
264 | 2 | 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 | 1 | list($taskStr, $taskTimestamp, $taskOutput) = $this->taskStack[$indent]; |
|
268 | 1 | if (!$taskOutput) { |
|
269 | 1 | if (!$this->isNewline) { |
|
270 | $this->write($this->eol); |
||
271 | 1 | $this->isNewline = true; |
|
272 | } |
||
273 | $this->write($this->fullMessageStr($taskTimestamp, $taskStr, $indent, true)); |
||
274 | 1 | $this->taskStack[$indent][2] = true; |
|
275 | } else { |
||
276 | 2 | continue; |
|
277 | 1 | } |
|
278 | } |
||
279 | } else { |
||
280 | 5 | return $this; |
|
281 | 3 | } |
|
282 | } |
||
283 | |||
284 | 4 | if (!$this->isNewline) { |
|
285 | $this->write($this->eol); |
||
286 | 2 | $this->isNewline = true; |
|
287 | } |
||
288 | $this->write($this->messageStr($str, true)); |
||
289 | 4 | return $this; |
|
290 | 8 | } |
|
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() { |
||
298 | return $this->formatOutput; |
||
299 | } |
||
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 | 10 | public function setFormatOutput($formatOutput) { |
|
311 | |||
312 | 4 | protected function fullMessageStr($timestamp, $str, $indent = null, $eol = true) { |
|
313 | 4 | if ($indent === null) { |
|
314 | $indent = $this->currentLevel() - 1; |
||
315 | } |
||
316 | |||
317 | 4 | if ($indent <= 0) { |
|
318 | 4 | $indentStr = ''; |
|
319 | 2 | } elseif ($indent === 1) { |
|
320 | 3 | $indentStr = '- '; |
|
321 | } else { |
||
322 | $indentStr = str_repeat(' ', $indent - 1).'- '; |
||
323 | 4 | } |
|
324 | |||
325 | 4 | $result = $indentStr.$str; |
|
326 | |||
327 | if ($this->getDateFormat()) { |
||
328 | $result = strftime($this->getDateFormat(), $timestamp).' '.$result; |
||
329 | } |
||
330 | |||
331 | 4 | if ($eol) { |
|
332 | 4 | $result .= $this->eol; |
|
333 | } |
||
334 | 4 | 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 | protected function messageStr($str, $eol = true) { |
||
345 | return $this->fullMessageStr(time(), $str, null, $eol); |
||
346 | } |
||
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 | 3 | protected function formatString($text, array $wrap) { |
|
362 | |||
363 | /** |
||
364 | * Get the maxLevel. |
||
365 | * |
||
366 | * @return int Returns the maxLevel. |
||
367 | */ |
||
368 | 2 | public function getMaxLevel() { |
|
371 | |||
372 | /** |
||
373 | * @param int $maxLevel |
||
374 | * @return LogFormatter |
||
375 | */ |
||
376 | 12 | public function setMaxLevel($maxLevel) { |
|
377 | 12 | if ($maxLevel < 0) { |
|
378 | throw new \InvalidArgumentException("The max level must be greater than zero.", 416); |
||
379 | } |
||
380 | |||
381 | 11 | $this->maxLevel = $maxLevel; |
|
382 | 11 | return $this; |
|
383 | } |
||
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 | 1 | 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 | 9 | 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 | 5 | 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 | 5 | 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 | 2 | 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.