Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9701863

Browse files
committed
Introduce isMaxKnown variable
Up until now, it has been very hard to determin whether a progressbar has a known maximum value or not, because the property `max` is an integer. Up until now, `max == 0` has been treated like we do _not_ know the `max` value. However, `max == 0` _is_ a valid `max` value when iterating over an empty but countable array for example. We cannot simply keep `max == null` if unknown, because that would break rendering mechanics and fixing those would require interface changes. Hence, enter `isMaxKnown` - a property which keeps track of whether the `max` value is known or not.
1 parent eac82fc commit 9701863

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Symfony/Component/Console/Helper/ProgressBar.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ final class ProgressBar
5151
private int $step = 0;
5252
private int $startingStep = 0;
5353
private int $max = 0;
54+
private bool $isMaxKnown = false;
5455
private int $startTime;
5556
private int $stepWidth;
5657
private float $percent = 0.0;
@@ -289,6 +290,8 @@ public function maxSecondsBetweenRedraws(float $seconds): void
289290
*/
290291
public function iterate(iterable $iterable, int $max = null): iterable
291292
{
293+
$this->isMaxKnown = is_countable($iterable);
294+
292295
$this->start($max ?? (is_countable($iterable) ? \count($iterable) : 0));
293296

294297
foreach ($iterable as $key => $value) {
@@ -377,6 +380,7 @@ public function setMaxSteps(int $max)
377380
$this->format = null;
378381
$this->max = max(0, $max);
379382
$this->stepWidth = $this->max ? Helper::width((string) $this->max) : 4;
383+
$this->isMaxKnown = $this->isMaxKnown || 0 < $this->max;
380384
}
381385

382386
/**
@@ -519,14 +523,14 @@ private static function initPlaceholderFormatters(): array
519523
return Helper::formatTime(time() - $bar->getStartTime());
520524
},
521525
'remaining' => function (self $bar) {
522-
if (!isset($bar->max)) {
526+
if (!$bar->isMaxKnown && !$bar->getMaxSteps()) {
523527
throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.');
524528
}
525529

526530
return Helper::formatTime($bar->getRemaining());
527531
},
528532
'estimated' => function (self $bar) {
529-
if (!isset($bar->max)) {
533+
if (!$bar->isMaxKnown && !$bar->getMaxSteps()) {
530534
throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.');
531535
}
532536

0 commit comments

Comments
 (0)