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 |
||
16 | class LogFormatter implements LoggerInterface { |
||
17 | /** |
||
18 | * @var string The date format as passed to {@link strftime()}. |
||
19 | */ |
||
20 | protected $dateFormat = '[%F %T]'; |
||
21 | |||
22 | /** |
||
23 | * @var string The end of line string to use. |
||
24 | */ |
||
25 | protected $eol = PHP_EOL; |
||
26 | |||
27 | /** |
||
28 | * @var bool Whether or not to format output. |
||
29 | */ |
||
30 | protected $formatOutput; |
||
31 | |||
32 | /** |
||
33 | * @var resource The output file handle. |
||
34 | */ |
||
35 | protected $outputHandle; |
||
36 | |||
37 | /** |
||
38 | * @var bool Whether or not the console is on a new line. |
||
39 | */ |
||
40 | protected $isNewline = true; |
||
41 | |||
42 | /** |
||
43 | * @var int The maximum level deep to output. |
||
44 | */ |
||
45 | protected $maxLevel = 2; |
||
46 | |||
47 | /** |
||
48 | * @var bool Whether or not to show durations for tasks. |
||
49 | */ |
||
50 | protected $showDurations = true; |
||
51 | |||
52 | /** |
||
53 | * @var array An array of currently running tasks. |
||
54 | */ |
||
55 | protected $taskStack = []; |
||
56 | |||
57 | /** |
||
58 | * LogFormatter constructor. |
||
59 | */ |
||
60 | 13 | public function __construct() { |
|
64 | |||
65 | /** |
||
66 | * Output an error message. |
||
67 | * |
||
68 | * When formatting is turned on, error messages are displayed in red. Error messages are always output, even if they |
||
69 | * are past the maximum display level. |
||
70 | * |
||
71 | * @param string $str The message to output. |
||
72 | * @return $this |
||
73 | */ |
||
74 | 3 | public function error(string $str) { |
|
77 | |||
78 | /** |
||
79 | * Output a success message. |
||
80 | * |
||
81 | * When formatting is turned on, success messages are displayed in green. |
||
82 | * |
||
83 | * @param string $str The message to output. |
||
84 | * @return $this |
||
85 | */ |
||
86 | 1 | public function success(string $str) { |
|
89 | |||
90 | /** |
||
91 | * Output a warning message. |
||
92 | * |
||
93 | * When formatting is turned on, warning messages are displayed in yellow. |
||
94 | * |
||
95 | * @param string $str The message to output. |
||
96 | * @return LogFormatter Returns `$this` for fluent calls. |
||
97 | */ |
||
98 | public function warn(string $str) { |
||
101 | |||
102 | /** |
||
103 | * Get the current depth of tasks. |
||
104 | * |
||
105 | * @return int Returns the current level. |
||
106 | */ |
||
107 | 11 | protected function currentLevel() { |
|
110 | |||
111 | /** |
||
112 | * Output a message that designates the beginning of a task. |
||
113 | * |
||
114 | * @param string $str The message to output. |
||
115 | * @return $this Returns `$this` for fluent calls. |
||
116 | */ |
||
117 | 8 | public function begin(string $str) { |
|
135 | |||
136 | /** |
||
137 | * Output a message that designates a task being completed. |
||
138 | * |
||
139 | * @param string $str The message to output. |
||
140 | * @param bool $force Whether or not to always output the message even if the task is past the max depth. |
||
141 | * @param string $logLevel An unused parameter to keep the interface happy. |
||
142 | * |
||
143 | * @return $this Returns `$this` for fluent calls. |
||
144 | */ |
||
145 | 8 | public function end(string $str = '', bool $force = false, $logLevel = LogLevels::INFO) { |
|
186 | |||
187 | /** |
||
188 | * Output a message that represents a task being completed in success. |
||
189 | * |
||
190 | * When formatting is turned on, success messages are output in green. |
||
191 | * |
||
192 | * @param string $str The message to output. |
||
193 | * @param bool $force Whether or not to force a message past the max level to be output. |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function endSuccess(string $str, bool $force = false) { |
||
199 | |||
200 | /** |
||
201 | * Output a message that represents a task being completed in an error. |
||
202 | * |
||
203 | * When formatting is turned on, error messages are output in red. Error messages are always output even if they are |
||
204 | * past the maximum depth. |
||
205 | * |
||
206 | * @param string $str The message to output. |
||
207 | * @return $this |
||
208 | */ |
||
209 | 1 | public function endError(string $str) { |
|
212 | |||
213 | /** |
||
214 | * Output a message that ends a task with an HTTP status code. |
||
215 | * |
||
216 | * 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 |
||
217 | * response code to this message. |
||
218 | * |
||
219 | * @param int $httpStatus The HTTP status code that represents the completion of a task. |
||
220 | * @param bool $force Whether or not to force message output. |
||
221 | * @return $this Returns `$this` for fluent calls. |
||
222 | * @see LogFormatter::endSuccess(), LogFormatter::endError(). |
||
223 | */ |
||
224 | public function endHttpStatus(int $httpStatus, bool $force = false) { |
||
237 | |||
238 | /** |
||
239 | * Format a time duration. |
||
240 | * |
||
241 | * @param float $duration The duration in seconds and fractions of a second. |
||
242 | * @return string Returns the duration formatted for humans. |
||
243 | * @see microtime() |
||
244 | */ |
||
245 | 1 | public function formatDuration(float $duration) { |
|
269 | |||
270 | /** |
||
271 | * Output a message. |
||
272 | * |
||
273 | * @param string $str The message to output. |
||
274 | * @param bool $force Whether or not to force output of the message even if it's past the max depth. |
||
275 | * @return $this Returns `$this` for fluent calls. |
||
276 | */ |
||
277 | 10 | public function message(string $str, bool $force = false) { |
|
308 | |||
309 | /** |
||
310 | * Get whether or not output should be formatted. |
||
311 | * |
||
312 | * @return boolean Returns **true** if output should be formatted or **false** otherwise. |
||
313 | */ |
||
314 | public function getFormatOutput() { |
||
317 | |||
318 | /** |
||
319 | * Set whether or not output should be formatted. |
||
320 | * |
||
321 | * @param boolean $formatOutput Whether or not to format output. |
||
322 | * @return $this |
||
323 | */ |
||
324 | 11 | public function setFormatOutput(bool $formatOutput) { |
|
328 | |||
329 | 11 | protected function fullMessageStr($timestamp, $str, $indent = null, $eol = true) { |
|
353 | |||
354 | /** |
||
355 | * Create a message string. |
||
356 | * |
||
357 | * @param string $str The message to output. |
||
358 | * @param bool $eol Whether or not to add an EOL. |
||
359 | * @return string Returns the message. |
||
360 | */ |
||
361 | 11 | protected function messageStr($str, $eol = true) { |
|
364 | |||
365 | /** |
||
366 | * Format some text for the console. |
||
367 | * |
||
368 | * @param string $text The text to format. |
||
369 | * @param string[] $wrap The format to wrap in the form ['before', 'after']. |
||
370 | * @return string Returns the string formatted according to {@link Cli::$format}. |
||
371 | */ |
||
372 | 4 | protected function formatString($text, array $wrap) { |
|
379 | |||
380 | /** |
||
381 | * Get the maxLevel. |
||
382 | * |
||
383 | * @return int Returns the maxLevel. |
||
384 | */ |
||
385 | 11 | public function getMaxLevel() { |
|
388 | |||
389 | /** |
||
390 | * @param int $maxLevel |
||
391 | * @return LogFormatter |
||
392 | */ |
||
393 | 12 | public function setMaxLevel(int $maxLevel) { |
|
401 | |||
402 | /** |
||
403 | * Get the date format as passed to {@link strftime()}. |
||
404 | * |
||
405 | * @return string Returns the date format. |
||
406 | * @see strftime() |
||
407 | */ |
||
408 | 11 | public function getDateFormat() { |
|
411 | |||
412 | /** |
||
413 | * Set the date format as passed to {@link strftime()}. |
||
414 | * |
||
415 | * @param string $dateFormat |
||
416 | * @return $this |
||
417 | * @see strftime() |
||
418 | */ |
||
419 | 11 | public function setDateFormat(string $dateFormat) { |
|
423 | |||
424 | /** |
||
425 | * Get the end of line string to use. |
||
426 | * |
||
427 | * @return string Returns the eol string. |
||
428 | */ |
||
429 | 6 | public function getEol() { |
|
432 | |||
433 | /** |
||
434 | * Set the end of line string. |
||
435 | * |
||
436 | * @param string $eol The end of line string to use. |
||
437 | * @return $this |
||
438 | */ |
||
439 | 11 | public function setEol($eol) { |
|
443 | |||
444 | /** |
||
445 | * Get the showDurations. |
||
446 | * |
||
447 | * @return boolean Returns the showDurations. |
||
448 | */ |
||
449 | 8 | public function getShowDurations() { |
|
452 | |||
453 | /** |
||
454 | * Set the showDurations. |
||
455 | * |
||
456 | * @param boolean $showDurations |
||
457 | * @return $this |
||
458 | */ |
||
459 | 11 | public function setShowDurations(bool $showDurations) { |
|
463 | |||
464 | /** |
||
465 | * Set the output file handle. |
||
466 | * |
||
467 | * @param resource $handle |
||
468 | * @return $this |
||
469 | */ |
||
470 | public function setOutputHandle($handle) { |
||
477 | |||
478 | /** |
||
479 | * Write a string to the CLI. |
||
480 | * |
||
481 | * This method is intended to centralize the echoing of output in case the class is subclassed and the behaviour |
||
482 | * needs to change. |
||
483 | * |
||
484 | * @param string $str The string to write. |
||
485 | */ |
||
486 | 11 | public function write($str) { |
|
493 | } |
||
494 |
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.