@@ -195,7 +195,7 @@ public function getStartTime(): int
195195
196196 public function getMaxSteps (): int
197197 {
198- return $ this ->max ;
198+ return $ this ->max ?? 0 ;
199199 }
200200
201201 public function getProgress (): int
@@ -215,7 +215,7 @@ public function getProgressPercent(): float
215215
216216 public function getBarOffset (): float
217217 {
218- return floor ($ this ->max ? $ this ->percent * $ this ->barWidth : (null === $ this ->redrawFreq ? (int ) (min (5 , $ this ->barWidth / 15 ) * $ this ->writeCount ) : $ this ->step ) % $ this ->barWidth );
218+ return floor (null !== $ this ->max ? $ this ->percent * $ this ->barWidth : (null === $ this ->redrawFreq ? (int ) (min (5 , $ this ->barWidth / 15 ) * $ this ->writeCount ) : $ this ->step ) % $ this ->barWidth );
219219 }
220220
221221 public function getEstimated (): float
@@ -253,7 +253,7 @@ public function setBarCharacter(string $char): void
253253
254254 public function getBarCharacter (): string
255255 {
256- return $ this ->barChar ?? ($ this ->max ? '= ' : $ this ->emptyBarChar );
256+ return $ this ->barChar ?? (null !== $ this ->max ? '= ' : $ this ->emptyBarChar );
257257 }
258258
259259 public function setEmptyBarCharacter (string $ char ): void
@@ -315,7 +315,21 @@ public function maxSecondsBetweenRedraws(float $seconds): void
315315 */
316316 public function iterate (iterable $ iterable , int $ max = null ): iterable
317317 {
318- $ this ->start ($ max ?? (is_countable ($ iterable ) ? \count ($ iterable ) : 0 ));
318+ if (0 === $ max ) {
319+ $ max = null ;
320+ }
321+
322+ $ max ??= is_countable ($ iterable ) ? \count ($ iterable ) : null ;
323+
324+ if (0 === $ max ) {
325+ $ this ->max = 0 ;
326+ $ this ->stepWidth = 2 ;
327+ $ this ->finish ();
328+
329+ return ;
330+ }
331+
332+ $ this ->start ($ max );
319333
320334 foreach ($ iterable as $ key => $ value ) {
321335 yield $ key => $ value ;
@@ -373,11 +387,15 @@ public function setProgress(int $step): void
373387 $ step = 0 ;
374388 }
375389
376- $ redrawFreq = $ this ->redrawFreq ?? (($ this ->max ?: 10 ) / 10 );
377- $ prevPeriod = (int ) ($ this ->step / $ redrawFreq );
378- $ currPeriod = (int ) ($ step / $ redrawFreq );
390+ $ redrawFreq = $ this ->redrawFreq ?? (($ this ->max ?? 10 ) / 10 );
391+ $ prevPeriod = $ redrawFreq ? (int ) ($ this ->step / $ redrawFreq ) : 0 ;
392+ $ currPeriod = $ redrawFreq ? (int ) ($ step / $ redrawFreq ) : 0 ;
379393 $ this ->step = $ step ;
380- $ this ->percent = $ this ->max ? (float ) $ this ->step / $ this ->max : 0 ;
394+ $ this ->percent = match ($ this ->max ) {
395+ null => 0 ,
396+ 0 => 1 ,
397+ default => (float ) $ this ->step / $ this ->max ,
398+ };
381399 $ timeInterval = microtime (true ) - $ this ->lastWriteTime ;
382400
383401 // Draw regardless of other limits
@@ -398,28 +416,37 @@ public function setProgress(int $step): void
398416 }
399417 }
400418
401- public function setMaxSteps (int $ max ): void
419+ public function setMaxSteps (? int $ max ): void
402420 {
421+ if (0 === $ max ) {
422+ $ max = null ;
423+ }
424+
403425 $ this ->format = null ;
404- $ this ->max = max (0 , $ max );
405- $ this ->stepWidth = $ this ->max ? Helper::width ((string ) $ this ->max ) : 4 ;
426+ if (null === $ max ) {
427+ $ this ->max = null ;
428+ $ this ->stepWidth = 4 ;
429+ } else {
430+ $ this ->max = max (0 , $ max );
431+ $ this ->stepWidth = Helper::width ((string ) $ this ->max );
432+ }
406433 }
407434
408435 /**
409436 * Finishes the progress output.
410437 */
411438 public function finish (): void
412439 {
413- if (! $ this ->max ) {
440+ if (null === $ this ->max ) {
414441 $ this ->max = $ this ->step ;
415442 }
416443
417- if ($ this ->step === $ this ->max && !$ this ->overwrite ) {
444+ if (( $ this ->step === $ this ->max || null === $ this -> max ) && !$ this ->overwrite ) {
418445 // prevent double 100% output
419446 return ;
420447 }
421448
422- $ this ->setProgress ($ this ->max );
449+ $ this ->setProgress ($ this ->max ?? $ this -> step );
423450 }
424451
425452 /**
@@ -542,14 +569,14 @@ private static function initPlaceholderFormatters(): array
542569 },
543570 'elapsed ' => fn (self $ bar ) => Helper::formatTime (time () - $ bar ->getStartTime (), 2 ),
544571 'remaining ' => function (self $ bar ) {
545- if (! $ bar ->getMaxSteps ()) {
572+ if (null === $ bar ->getMaxSteps ()) {
546573 throw new LogicException ('Unable to display the remaining time if the maximum number of steps is not set. ' );
547574 }
548575
549576 return Helper::formatTime ($ bar ->getRemaining (), 2 );
550577 },
551578 'estimated ' => function (self $ bar ) {
552- if (! $ bar ->getMaxSteps ()) {
579+ if (null === $ bar ->getMaxSteps ()) {
553580 throw new LogicException ('Unable to display the estimated time if the maximum number of steps is not set. ' );
554581 }
555582
0 commit comments