Complex classes like ProgressBar 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 ProgressBar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | final class ProgressBar |
||
| 28 | { |
||
| 29 | private $barWidth = 28; |
||
| 30 | private $barChar; |
||
| 31 | private $emptyBarChar = '-'; |
||
| 32 | private $progressChar = '>'; |
||
| 33 | private $format; |
||
| 34 | private $internalFormat; |
||
| 35 | private $redrawFreq = 1; |
||
| 36 | private $writeCount; |
||
| 37 | private $lastWriteTime; |
||
| 38 | private $minSecondsBetweenRedraws = 0; |
||
| 39 | private $maxSecondsBetweenRedraws = 1; |
||
| 40 | private $output; |
||
| 41 | private $step = 0; |
||
| 42 | private $max; |
||
| 43 | private $startTime; |
||
| 44 | private $stepWidth; |
||
| 45 | private $percent = 0.0; |
||
| 46 | private $formatLineCount; |
||
| 47 | private $messages = []; |
||
| 48 | private $overwrite = true; |
||
| 49 | private $terminal; |
||
| 50 | private $previousMessage; |
||
| 51 | private $cursor; |
||
| 52 | |||
| 53 | private static $formatters; |
||
| 54 | private static $formats; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param int $max Maximum steps (0 if unknown) |
||
| 58 | */ |
||
| 59 | public function __construct(OutputInterface $output, int $max = 0, float $minSecondsBetweenRedraws = 0.1) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Sets a placeholder formatter for a given name. |
||
| 88 | * |
||
| 89 | * This method also allow you to override an existing placeholder. |
||
| 90 | * |
||
| 91 | * @param string $name The placeholder name (including the delimiter char like %) |
||
| 92 | * @param callable $callable A PHP callable |
||
| 93 | */ |
||
| 94 | public static function setPlaceholderFormatterDefinition(string $name, callable $callable): void |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Gets the placeholder formatter for a given name. |
||
| 105 | * |
||
| 106 | * @param string $name The placeholder name (including the delimiter char like %) |
||
| 107 | * |
||
| 108 | * @return callable|null A PHP callable |
||
| 109 | */ |
||
| 110 | public static function getPlaceholderFormatterDefinition(string $name): ?callable |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sets a format for a given name. |
||
| 121 | * |
||
| 122 | * This method also allow you to override an existing format. |
||
| 123 | * |
||
| 124 | * @param string $name The format name |
||
| 125 | * @param string $format A format string |
||
| 126 | */ |
||
| 127 | public static function setFormatDefinition(string $name, string $format): void |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Gets the format for a given name. |
||
| 138 | * |
||
| 139 | * @param string $name The format name |
||
| 140 | * |
||
| 141 | * @return string|null A format string |
||
| 142 | */ |
||
| 143 | public static function getFormatDefinition(string $name): ?string |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Associates a text with a named placeholder. |
||
| 154 | * |
||
| 155 | * The text is displayed when the progress bar is rendered but only |
||
| 156 | * when the corresponding placeholder is part of the custom format line |
||
| 157 | * (by wrapping the name with %). |
||
| 158 | * |
||
| 159 | * @param string $message The text to associate with the placeholder |
||
| 160 | * @param string $name The name of the placeholder |
||
| 161 | */ |
||
| 162 | public function setMessage(string $message, string $name = 'message') |
||
| 166 | |||
| 167 | public function getMessage(string $name = 'message') |
||
| 171 | |||
| 172 | public function getStartTime(): int |
||
| 176 | |||
| 177 | public function getMaxSteps(): int |
||
| 181 | |||
| 182 | public function getProgress(): int |
||
| 186 | |||
| 187 | private function getStepWidth(): int |
||
| 191 | |||
| 192 | public function getProgressPercent(): float |
||
| 196 | |||
| 197 | public function getBarOffset(): float |
||
| 201 | |||
| 202 | public function getEstimated(): float |
||
| 210 | |||
| 211 | public function getRemaining(): float |
||
| 219 | |||
| 220 | public function setBarWidth(int $size) |
||
| 224 | |||
| 225 | public function getBarWidth(): int |
||
| 229 | |||
| 230 | public function setBarCharacter(string $char) |
||
| 234 | |||
| 235 | public function getBarCharacter(): string |
||
| 243 | |||
| 244 | public function setEmptyBarCharacter(string $char) |
||
| 248 | |||
| 249 | public function getEmptyBarCharacter(): string |
||
| 253 | |||
| 254 | public function setProgressCharacter(string $char) |
||
| 258 | |||
| 259 | public function getProgressCharacter(): string |
||
| 263 | |||
| 264 | public function setFormat(string $format) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Sets the redraw frequency. |
||
| 272 | * |
||
| 273 | * @param int|float $freq The frequency in steps |
||
| 274 | */ |
||
| 275 | public function setRedrawFrequency(?int $freq) |
||
| 279 | |||
| 280 | public function minSecondsBetweenRedraws(float $seconds): void |
||
| 284 | |||
| 285 | public function maxSecondsBetweenRedraws(float $seconds): void |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns an iterator that will automatically update the progress bar when iterated. |
||
| 292 | * |
||
| 293 | * @param int|null $max Number of steps to complete the bar (0 if indeterminate), if null it will be inferred from $iterable |
||
| 294 | */ |
||
| 295 | public function iterate(iterable $iterable, int $max = null): iterable |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Starts the progress output. |
||
| 310 | * |
||
| 311 | * @param int|null $max Number of steps to complete the bar (0 if indeterminate), null to leave unchanged |
||
| 312 | */ |
||
| 313 | public function start(int $max = null) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Advances the progress output X steps. |
||
| 328 | * |
||
| 329 | * @param int $step Number of steps to advance |
||
| 330 | */ |
||
| 331 | public function advance(int $step = 1) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Sets whether to overwrite the progressbar, false for new line. |
||
| 338 | */ |
||
| 339 | public function setOverwrite(bool $overwrite) |
||
| 343 | |||
| 344 | public function setProgress(int $step) |
||
| 376 | |||
| 377 | public function setMaxSteps(int $max) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Finishes the progress output. |
||
| 386 | */ |
||
| 387 | public function finish(): void |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Outputs the current progress string. |
||
| 403 | */ |
||
| 404 | public function display(): void |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Removes the progress bar from the current line. |
||
| 419 | * |
||
| 420 | * This is useful if you wish to write some output |
||
| 421 | * while a progress bar is running. |
||
| 422 | * Call display() to show the progress bar again. |
||
| 423 | */ |
||
| 424 | public function clear(): void |
||
| 436 | |||
| 437 | private function setRealFormat(string $format) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Overwrites a previous message to the output. |
||
| 453 | */ |
||
| 454 | private function overwrite(string $message): void |
||
| 486 | |||
| 487 | private function determineBestFormat(): string |
||
| 501 | |||
| 502 | private static function initPlaceholderFormatters(): array |
||
| 546 | |||
| 547 | private static function initFormats(): array |
||
| 563 | |||
| 564 | private function buildLine(): string |
||
| 600 | } |
||
| 601 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.