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) { |
|
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) { |
|
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) { |
||
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) { |
|
254 | |||
255 | /** |
||
256 | * Output a message. |
||
257 | * |
||
258 | * @param string $str The message to output. |
||
259 | * @param bool $force Whether or not to force output of the message even if it's past the max depth. |
||
260 | * @return $this Returns `$this` for fluent calls. |
||
261 | */ |
||
262 | 10 | public function message($str, $force = false) { |
|
293 | |||
294 | /** |
||
295 | * Get whether or not output should be formatted. |
||
296 | * |
||
297 | * @return boolean Returns **true** if output should be formatted or **false** otherwise. |
||
298 | */ |
||
299 | public function getFormatOutput() { |
||
302 | |||
303 | /** |
||
304 | * Set whether or not output should be formatted. |
||
305 | * |
||
306 | * @param boolean $formatOutput Whether or not to format output. |
||
307 | * @return LogFormatter Returns `$this` for fluent calls. |
||
308 | */ |
||
309 | 11 | public function setFormatOutput($formatOutput) { |
|
313 | |||
314 | 11 | protected function fullMessageStr($timestamp, $str, $indent = null, $eol = true) { |
|
338 | |||
339 | /** |
||
340 | * Create a message string. |
||
341 | * |
||
342 | * @param string $str The message to output. |
||
343 | * @param bool $eol Whether or not to add an EOL. |
||
344 | * @return string Returns the message. |
||
345 | */ |
||
346 | 11 | protected function messageStr($str, $eol = true) { |
|
349 | |||
350 | /** |
||
351 | * Format some text for the console. |
||
352 | * |
||
353 | * @param string $text The text to format. |
||
354 | * @param string[] $wrap The format to wrap in the form ['before', 'after']. |
||
355 | * @return string Returns the string formatted according to {@link Cli::$format}. |
||
356 | */ |
||
357 | 4 | protected function formatString($text, array $wrap) { |
|
364 | |||
365 | /** |
||
366 | * Get the maxLevel. |
||
367 | * |
||
368 | * @return int Returns the maxLevel. |
||
369 | */ |
||
370 | 11 | public function getMaxLevel() { |
|
373 | |||
374 | /** |
||
375 | * @param int $maxLevel |
||
376 | * @return LogFormatter |
||
377 | */ |
||
378 | 12 | public function setMaxLevel($maxLevel) { |
|
386 | |||
387 | /** |
||
388 | * Get the date format as passed to {@link strftime()}. |
||
389 | * |
||
390 | * @return string Returns the date format. |
||
391 | * @see strftime() |
||
392 | */ |
||
393 | 11 | public function getDateFormat() { |
|
396 | |||
397 | /** |
||
398 | * Set the date format as passed to {@link strftime()}. |
||
399 | * |
||
400 | * @param string $dateFormat |
||
401 | * @return LogFormatter Returns `$this` for fluent calls. |
||
402 | * @see strftime() |
||
403 | */ |
||
404 | 11 | public function setDateFormat($dateFormat) { |
|
408 | |||
409 | /** |
||
410 | * Get the end of line string to use. |
||
411 | * |
||
412 | * @return string Returns the eol string. |
||
413 | */ |
||
414 | 6 | public function getEol() { |
|
417 | |||
418 | /** |
||
419 | * Set the end of line string. |
||
420 | * |
||
421 | * @param string $eol The end of line string to use. |
||
422 | * @return LogFormatter Returns `$this` for fluent calls. |
||
423 | */ |
||
424 | 11 | public function setEol($eol) { |
|
428 | |||
429 | /** |
||
430 | * Get the showDurations. |
||
431 | * |
||
432 | * @return boolean Returns the showDurations. |
||
433 | */ |
||
434 | 8 | public function getShowDurations() { |
|
437 | |||
438 | /** |
||
439 | * Set the showDurations. |
||
440 | * |
||
441 | * @param boolean $showDurations |
||
442 | * @return LogFormatter Returns `$this` for fluent calls. |
||
443 | */ |
||
444 | 11 | public function setShowDurations($showDurations) { |
|
448 | |||
449 | /** |
||
450 | * Set the output file handle. |
||
451 | * |
||
452 | * @param resource $handle |
||
453 | * @return LogFormatter Returns `$this` for fluent calls. |
||
454 | */ |
||
455 | public function setOutputHandle($handle) { |
||
462 | |||
463 | /** |
||
464 | * Write a string to the CLI. |
||
465 | * |
||
466 | * This method is intended to centralize the echoing of output in case the class is subclassed and the behaviour |
||
467 | * needs to change. |
||
468 | * |
||
469 | * @param string $str The string to write. |
||
470 | */ |
||
471 | 11 | public function write($str) { |
|
478 | } |
||
479 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.