| Conditions | 10 |
| Paths | 145 |
| Total Lines | 59 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 168 | public function writeProgression(bool $force = false): self |
||
| 169 | { |
||
| 170 | # Show Progress bar each second |
||
| 171 | if (!$force && $this->lastShown == time()) { |
||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($this->length == 0) { |
||
| 176 | return $this; |
||
| 177 | } |
||
| 178 | |||
| 179 | try { |
||
| 180 | $nbrRemaining = $this->length - $this->getNbrDone(); |
||
| 181 | |||
| 182 | $timeElapsed = time() - $this->time; |
||
| 183 | $timeETA = $nbrRemaining * ($timeElapsed / $this->getNbrSuccess(true)); |
||
| 184 | |||
| 185 | $percentPassed = ($this->nbrPassed / $this->length); |
||
| 186 | $percentWarning = ($this->nbrWarning / $this->length); |
||
| 187 | $percentDone = ($this->nbrSuccess / $this->length); |
||
| 188 | $percentError = ($this->nbrError / $this->length); |
||
| 189 | |||
| 190 | $percent = $this->getNbrDone() / $this->length; |
||
| 191 | |||
| 192 | $lengthPassed = (int) ($percentPassed * self::PROGRESSION_LENGTH); |
||
| 193 | $lengthWarning = (int) ($percentWarning * self::PROGRESSION_LENGTH); |
||
| 194 | $lengthDone = (int) ($percentDone * self::PROGRESSION_LENGTH); |
||
| 195 | $lengthError = (int) ($percentError * self::PROGRESSION_LENGTH); |
||
| 196 | |||
| 197 | $lengthLeft = self::PROGRESSION_LENGTH - $lengthPassed - $lengthDone - $lengthError - $lengthWarning; |
||
| 198 | $lengthLeft = ($lengthLeft < 0 ) ? 0 : $lengthLeft; |
||
| 199 | |||
| 200 | $msg = "\r" . sprintf('%3d', round($percent*100, 0)) . "%: ["; |
||
| 201 | |||
| 202 | $msg .= '<info>' . str_repeat('#', $lengthDone) . '</info>'; |
||
| 203 | $msg .= '<comment>' . str_repeat('#', $lengthPassed) . '</comment>'; |
||
| 204 | $msg .= '<err>' . str_repeat('#', $lengthError) . '</err>'; |
||
| 205 | |||
| 206 | $msg .= str_repeat('-', $lengthLeft) . '] '; |
||
| 207 | $msg .= ($this->nbrSuccess) ? " Done:<info>$this->nbrSuccess</info>" : ''; |
||
| 208 | $msg .= ($this->nbrWarning) ? " Warn:<comment>$this->nbrWarning</comment>" : ''; |
||
| 209 | $msg .= ($this->nbrPassed) ? " Pass:$this->nbrPassed" : ''; |
||
| 210 | $msg .= ($this->nbrError) ? " Err:<err>$this->nbrError</err>" : ''; |
||
| 211 | $msg .= ' Remaining:' . ($nbrRemaining); |
||
| 212 | |||
| 213 | $msg .= ' Elapsed:<info>' . $this->formatTime($timeElapsed) . '</info>'; |
||
| 214 | $msg .= ' ETA:<comment>' . $this->formatTime($timeETA) . '</comment>'; |
||
| 215 | |||
| 216 | $msg .= " \r"; |
||
| 217 | |||
| 218 | $this->output->write($msg); |
||
| 219 | |||
| 220 | # Show Progress bar each second |
||
| 221 | $this->lastShown = time(); |
||
| 222 | } catch (\Exception $e) { |
||
| 223 | # Do not break your stuff |
||
| 224 | } |
||
| 225 | |||
| 226 | return $this; |
||
| 227 | } |
||
| 288 |