Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ConsoleProgressBar 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 ConsoleProgressBar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ConsoleProgressBar |
||
| 28 | {
|
||
| 29 | |||
| 30 | /** |
||
| 31 | * The current step the progress bar should show. |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $currentStep = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The maximum number of steps to go. |
||
| 38 | * Calculated once from the settings. |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $numSteps = 0; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Indicates if the starting point for the bar has been stored. |
||
| 45 | * Per default this is false to indicate that no start position has been |
||
| 46 | * stored, yet. |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $started = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Whether a position has been stored before, using the storePos() method. |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $positionStored = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Container to hold the options |
||
| 59 | * @var array(string=>mixed) |
||
| 60 | */ |
||
| 61 | protected $options = array( |
||
| 62 | 'barChar' => "=", |
||
| 63 | 'emptyChar' => "-", |
||
| 64 | 'formatString' => "%act% / %max% [%bar%] %fraction%% %memory%", |
||
| 65 | 'fractionFormat' => "%01.2f", |
||
| 66 | 'progressChar' => ">", |
||
| 67 | 'redrawFrequency' => 1, |
||
| 68 | 'step' => 1, |
||
| 69 | 'width' => 100, |
||
| 70 | 'actFormat' => '%.0f', |
||
| 71 | 'maxFormat' => '%.0f', |
||
| 72 | 'max' => 100, |
||
| 73 | ); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Storage for actual values to be replaced in the format string. |
||
| 77 | * Actual values are stored here and will be inserted into the bar |
||
| 78 | * before printing it. |
||
| 79 | * @var array(string=>string) |
||
| 80 | */ |
||
| 81 | protected $valueMap = array( |
||
| 82 | 'bar' => '', |
||
| 83 | 'fraction' => '', |
||
| 84 | 'act' => '', |
||
| 85 | 'max' => '', |
||
| 86 | 'memory' => '' |
||
| 87 | ); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Stores the bar utilization. |
||
| 91 | * This array saves how much space a specific part of the bar utilizes to not |
||
| 92 | * recalculate those on every step. |
||
| 93 | * @var array(string=>int) |
||
| 94 | */ |
||
| 95 | protected $measures = array( |
||
| 96 | 'barSpace' => 0, |
||
| 97 | 'fractionSpace' => 0, |
||
| 98 | 'actSpace' => 0, |
||
| 99 | 'maxSpace' => 0, |
||
| 100 | 'fixedCharSpace' => 0, |
||
| 101 | ); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Creates a new progress bar. |
||
| 105 | * |
||
| 106 | * @param int $max Maximum value, where progressbar |
||
| 107 | * reaches 100%. |
||
| 108 | * @param array(string=>string) $options Options |
||
|
|
|||
| 109 | */ |
||
| 110 | public function __construct($max = null, $options = array()) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set new options. |
||
| 125 | * This method allows you to change the options of progressbar. |
||
| 126 | * |
||
| 127 | * @param $key either an array of key=>value pairs or a string of the option |
||
| 128 | * @param $value only needed if the key is a string |
||
| 129 | */ |
||
| 130 | public function setOptions($key, $value = null) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns the current options. |
||
| 144 | * Returns the options currently set for this progressbar. |
||
| 145 | * @return The current options. |
||
| 146 | */ |
||
| 147 | public function getOptions() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Property read access. |
||
| 154 | * |
||
| 155 | * @param string $key Name of the property. |
||
| 156 | * |
||
| 157 | * @return mixed Value of the property or null. |
||
| 158 | */ |
||
| 159 | public function __get($key) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Property write access. |
||
| 182 | * |
||
| 183 | * @param string $key Name of the property. |
||
| 184 | * @param mixed $val The value for the property. |
||
| 185 | * |
||
| 186 | * @throws Exception |
||
| 187 | * If a desired property could not be found. |
||
| 188 | */ |
||
| 189 | public function __set($key, $val) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Property isset access. |
||
| 222 | * |
||
| 223 | * @param string $key Name of the property. |
||
| 224 | * |
||
| 225 | * @return bool True is the property is set, otherwise false. |
||
| 226 | */ |
||
| 227 | public function __isset($key) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Start the progress bar |
||
| 242 | * Starts the progress bar and sticks it to the current line. |
||
| 243 | * No output will be done yet. |
||
| 244 | * to print the bar. |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | public function start() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Advance the progress bar. |
||
| 256 | * Advances the progress bar by $step steps. Redraws the bar by default, |
||
| 257 | * using the output method. |
||
| 258 | * |
||
| 259 | * @param bool $redraw Whether to redraw the bar immediately. |
||
| 260 | * @param int $step How many steps to advance. |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public function advance($redraw = true, $step = 1) |
||
| 272 | |||
| 273 | public function setValueMap($key, $value) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Finish the progress bar. |
||
| 280 | * Finishes the bar (jump to 100% if not happened yet,...) and jumps |
||
| 281 | * to the next line to allow new output. Also resets the values of the |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function finish() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Draw the progress bar. |
||
| 293 | * Prints the progress-bar to the screen. If start() has not been called |
||
| 294 | * yet, the current line is used for start |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | protected function output() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Stores the current cursor position. |
||
| 316 | * Saves the current cursor position to return to it using |
||
| 317 | * restorePos. Multiple calls |
||
| 318 | * to this method will override each other. Only the last |
||
| 319 | * position is saved. |
||
| 320 | * @return void |
||
| 321 | */ |
||
| 322 | protected function storePos() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Restores a cursor position. |
||
| 333 | * Restores the cursor position last saved using storePos. |
||
| 334 | * @throws Exception |
||
| 335 | * If no position is saved. |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | |||
| 339 | protected function restorePos() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Generate all values to be replaced in the format string. |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | protected function generateValues() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Insert values into bar format string. |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | protected function insertValues() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Calculate several measures necessary to generate a bar. |
||
| 417 | * @return void |
||
| 418 | */ |
||
| 419 | protected function calculateMeasures() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Strip all escape sequences from a string to measure it's size correctly. |
||
| 442 | * |
||
| 443 | * @param mixed $str |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | protected function stripEscapeSequences($str) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Check if we currently running under windows |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | protected function isWindows() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Binary safe str_pad() replacement. |
||
| 463 | * This method is a multi-byte encoding safe replacement for the PHP |
||
| 464 | * function str_pad(). It mimics exactly the behavior of str_pad(), but |
||
| 465 | * uses iconv_* functions with UTF-8 encoding. The parameters received by |
||
| 466 | * this method equal the parameters of {@link http://php.net/str_pad
|
||
| 467 | * str_pad()}. Note: Make sure to hand only UTF-8 encoded content to this |
||
| 468 | * method. |
||
| 469 | * |
||
| 470 | * @param string $input |
||
| 471 | * @param int $padLength |
||
| 472 | * @param string $padString |
||
| 473 | * @param int $padType |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | protected function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT) |
||
| 521 | } |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.