1717use function count ;
1818use function iterator_to_array ;
1919use function min ;
20- use function round ;
20+ use function round ;
2121use function str_repeat ;
2222use function strlen ;
2323use function trim ;
3030class ProgressBar
3131{
3232 /**
33- * The total number of items involved
33+ * The total number of items involved.
3434 */
3535 protected int $ total = 0 ;
3636
3737 /**
38- * The current item that the progress bar represents
38+ * The current item that the progress bar represents.
3939 */
4040 protected int $ current = 0 ;
4141
4242 /**
43- * The current percentage displayed
43+ * The current percentage displayed.
4444 */
4545 protected string $ currentPercentage = '' ;
4646
4747 /**
48- * The string length of the bar when at 100%
48+ * The string length of the bar when at 100%.
4949 */
5050 protected int $ barStrLen = 0 ;
5151
5252 /**
53- * Flag indicating whether we are writing the bar for the first time
53+ * Flag indicating whether we are writing the bar for the first time.
5454 */
5555 protected bool $ firstLine = true ;
5656
5757 /**
58- * Current status bar label
58+ * Current status bar label.
5959 */
6060 protected string $ label = '' ;
6161
6262 /**
63- * Options for progress bar
63+ * Options for progress bar.
6464 */
6565 private array $ options = [
6666 'pointer ' => '> ' ,
@@ -70,7 +70,7 @@ class ProgressBar
7070 ];
7171
7272 /**
73- * Force a redraw every time
73+ * Force a redraw every time.
7474 */
7575 protected bool $ forceRedraw = false ;
7676
@@ -86,7 +86,7 @@ class ProgressBar
8686 protected Terminal $ terminal ;
8787
8888 /**
89- * If they pass in a total, set the total
89+ * If they pass in a total, set the total.
9090 */
9191 public function __construct (?int $ total = null , ?Writer $ writer = null )
9292 {
@@ -100,7 +100,7 @@ public function __construct(?int $total = null, ?Writer $writer = null)
100100 }
101101
102102 /**
103- * Set the total property
103+ * Set the total property.
104104 */
105105 public function total (int $ total ): self
106106 {
@@ -110,7 +110,7 @@ public function total(int $total): self
110110 }
111111
112112 /**
113- * Set progress bar options
113+ * Set progress bar options.
114114 */
115115 public function option (string |array $ key , ?string $ value = null ): self
116116 {
@@ -128,10 +128,11 @@ public function option(string|array $key, ?string $value = null): self
128128 }
129129
130130 /**
131- * Determines the current percentage we are at and re-writes the progress bar
131+ * Determines the current percentage we are at and re-writes the progress bar.
132132 *
133- * @return void
134133 * @throws UnexpectedValueException
134+ *
135+ * @return void
135136 */
136137 public function current (int $ current , string $ label = '' )
137138 {
@@ -151,17 +152,17 @@ public function current(int $current, string $label = '')
151152 }
152153
153154 /**
154- * Increments the current position we are at and re-writes the progress bar
155+ * Increments the current position we are at and re-writes the progress bar.
155156 *
156- * @param integer $increment The number of items to increment by
157+ * @param int $increment The number of items to increment by
157158 */
158159 public function advance (int $ increment = 1 , string $ label = '' )
159160 {
160161 $ this ->current ($ this ->current + $ increment , $ label );
161162 }
162163
163164 /**
164- * Force the progress bar to redraw every time regardless of whether it has changed or not
165+ * Force the progress bar to redraw every time regardless of whether it has changed or not.
165166 */
166167 public function forceRedraw (bool $ force = true ): self
167168 {
@@ -170,11 +171,10 @@ public function forceRedraw(bool $force = true): self
170171 return $ this ;
171172 }
172173
173-
174174 /**
175175 * Update a progress bar using an iterable.
176176 *
177- * @param iterable $items Array or any other iterable object
177+ * @param iterable $items Array or any other iterable object
178178 * @param callable $callback A handler to run on each item
179179 */
180180 public function each ($ items , callable $ callback = null )
@@ -196,9 +196,8 @@ public function each($items, callable $callback = null)
196196 }
197197 }
198198
199-
200199 /**
201- * Draw the progress bar, if necessary
200+ * Draw the progress bar, if necessary.
202201 */
203202 protected function drawProgressBar (int $ current , string $ label )
204203 {
@@ -212,7 +211,7 @@ protected function drawProgressBar(int $current, string $label)
212211 }
213212
214213 /**
215- * Build the progress bar str and return it
214+ * Build the progress bar str and return it.
216215 */
217216 protected function getProgressBar (int $ current , string $ label ): string
218217 {
@@ -226,8 +225,8 @@ protected function getProgressBar(int $current, string $label): string
226225 // Move the cursor up and clear it to the end
227226 $ lines = $ this ->hasLabelLine ? 2 : 1 ;
228227 $ bar = $ this ->cursor ->up ($ lines );
229- $ bar .= $ this ->cr () . $ this ->cursor ->eraseLine ();
230- $ bar .= $ this ->getProgressBarStr ($ current , $ label );
228+ $ bar .= $ this ->cr () . $ this ->cursor ->eraseLine ();
229+ $ bar .= $ this ->getProgressBarStr ($ current , $ label );
231230
232231 // If this line has a label then set that this progress bar has a label line
233232 if (strlen ($ label ) > 0 ) {
@@ -239,7 +238,7 @@ protected function getProgressBar(int $current, string $label): string
239238
240239 /**
241240 * Get the progress bar string, basically:
242- * =============> 50% label
241+ * =============> 50% label.
243242 */
244243 protected function getProgressBarStr (int $ current , string $ label ): string
245244 {
@@ -256,14 +255,14 @@ protected function getProgressBarStr(int $current, string $label): string
256255 $ label = $ this ->labelFormatted ('' );
257256 }
258257
259- $ bar = '< ' . $ this ->options ['color ' ] . '> ' . $ bar . ' ' . $ number . '</end> ' ;
258+ $ bar = '< ' . $ this ->options ['color ' ] . '> ' . $ bar . ' ' . $ number . '</end> ' ;
260259 $ label = '< ' . $ this ->options ['labelColor ' ] . '> ' . $ label . '</end> ' ;
261260
262261 return trim ($ bar . $ label );
263262 }
264263
265264 /**
266- * Get the string for the actual bar based on the current length
265+ * Get the string for the actual bar based on the current length.
267266 */
268267 protected function getBar (int $ length ): string
269268 {
@@ -274,7 +273,7 @@ protected function getBar(int $length): string
274273 }
275274
276275 /**
277- * Get the length of the bar string based on the width of the terminal window
276+ * Get the length of the bar string based on the width of the terminal window.
278277 */
279278 protected function getBarStrLen (): int
280279 {
@@ -287,31 +286,31 @@ protected function getBarStrLen(): int
287286 }
288287
289288 /**
290- * Format the percentage so it looks pretty
289+ * Format the percentage so it looks pretty.
291290 */
292291 protected function percentageFormatted (float $ percentage ): string
293292 {
294293 return round ($ percentage * 100 ) . '% ' ;
295294 }
296295
297296 /**
298- * Format the label so it is positioned correctly
297+ * Format the label so it is positioned correctly.
299298 */
300299 protected function labelFormatted (string $ label ): string
301300 {
302301 return "\n" . $ this ->cr () . $ this ->cursor ->eraseLine () . $ label ;
303302 }
304303
305304 /**
306- * Determine whether the progress bar has changed and we need to redrew
305+ * Determine whether the progress bar has changed and we need to redrew.
307306 */
308307 protected function shouldRedraw (string $ percentage , string $ label ): bool
309308 {
310- return ( $ this ->forceRedraw || $ percentage != $ this ->currentPercentage || $ label != $ this ->label ) ;
309+ return $ this ->forceRedraw || $ percentage != $ this ->currentPercentage || $ label != $ this ->label ;
311310 }
312311
313312 protected function cr (): string
314313 {
315- return Terminal::isWindows () ? "\r" : "" ;
314+ return Terminal::isWindows () ? "\r" : '' ;
316315 }
317316}
0 commit comments