@@ -195,7 +195,7 @@ public function getStartTime(): int
195
195
196
196
public function getMaxSteps (): int
197
197
{
198
- return $ this ->max ;
198
+ return $ this ->max ?? 0 ;
199
199
}
200
200
201
201
public function getProgress (): int
@@ -215,7 +215,7 @@ public function getProgressPercent(): float
215
215
216
216
public function getBarOffset (): float
217
217
{
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 );
219
219
}
220
220
221
221
public function getEstimated (): float
@@ -253,7 +253,7 @@ public function setBarCharacter(string $char): void
253
253
254
254
public function getBarCharacter (): string
255
255
{
256
- return $ this ->barChar ?? ($ this ->max ? '= ' : $ this ->emptyBarChar );
256
+ return $ this ->barChar ?? (null !== $ this ->max ? '= ' : $ this ->emptyBarChar );
257
257
}
258
258
259
259
public function setEmptyBarCharacter (string $ char ): void
@@ -315,7 +315,21 @@ public function maxSecondsBetweenRedraws(float $seconds): void
315
315
*/
316
316
public function iterate (iterable $ iterable , int $ max = null ): iterable
317
317
{
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 );
319
333
320
334
foreach ($ iterable as $ key => $ value ) {
321
335
yield $ key => $ value ;
@@ -373,11 +387,15 @@ public function setProgress(int $step): void
373
387
$ step = 0 ;
374
388
}
375
389
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 ;
379
393
$ 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
+ };
381
399
$ timeInterval = microtime (true ) - $ this ->lastWriteTime ;
382
400
383
401
// Draw regardless of other limits
@@ -398,28 +416,37 @@ public function setProgress(int $step): void
398
416
}
399
417
}
400
418
401
- public function setMaxSteps (int $ max ): void
419
+ public function setMaxSteps (? int $ max ): void
402
420
{
421
+ if (0 === $ max ) {
422
+ $ max = null ;
423
+ }
424
+
403
425
$ 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
+ }
406
433
}
407
434
408
435
/**
409
436
* Finishes the progress output.
410
437
*/
411
438
public function finish (): void
412
439
{
413
- if (! $ this ->max ) {
440
+ if (null === $ this ->max ) {
414
441
$ this ->max = $ this ->step ;
415
442
}
416
443
417
- if ($ this ->step === $ this ->max && !$ this ->overwrite ) {
444
+ if (( $ this ->step === $ this ->max || null === $ this -> max ) && !$ this ->overwrite ) {
418
445
// prevent double 100% output
419
446
return ;
420
447
}
421
448
422
- $ this ->setProgress ($ this ->max );
449
+ $ this ->setProgress ($ this ->max ?? $ this -> step );
423
450
}
424
451
425
452
/**
@@ -542,14 +569,14 @@ private static function initPlaceholderFormatters(): array
542
569
},
543
570
'elapsed ' => fn (self $ bar ) => Helper::formatTime (time () - $ bar ->getStartTime (), 2 ),
544
571
'remaining ' => function (self $ bar ) {
545
- if (! $ bar ->getMaxSteps ()) {
572
+ if (null === $ bar ->getMaxSteps ()) {
546
573
throw new LogicException ('Unable to display the remaining time if the maximum number of steps is not set. ' );
547
574
}
548
575
549
576
return Helper::formatTime ($ bar ->getRemaining (), 2 );
550
577
},
551
578
'estimated ' => function (self $ bar ) {
552
- if (! $ bar ->getMaxSteps ()) {
579
+ if (null === $ bar ->getMaxSteps ()) {
553
580
throw new LogicException ('Unable to display the estimated time if the maximum number of steps is not set. ' );
554
581
}
555
582
0 commit comments