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

Skip to content

[ProgressBar] Empty iterable throws Exception on "maximum number of steps is not set" #47244

Closed
@freimair

Description

@freimair

Symfony version(s) affected

latest

Description

I stumbled upon an empty array that has been used with a progress bar. And my execution stopped. Turns out the ProgressBar checks if its max steps are set when "remaining" and "estimated" values are used in formatting. Nothing wrong so far.

However, in case there is an empty array supplied to the progress bar, code internals interpret a max value of 0 as the max value not being set:

                if (!$bar->getMaxSteps()) {
                    throw new LogicException('Unable to display the ... if the maximum number of steps is not set.');
                }

(https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Console/Helper/ProgressBar.php#L522 and https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Console/Helper/ProgressBar.php#L529)

That to me seems not entirely accurate. Because in this very case, the max value IS set and it IS 0. Yet, it is treated like it is NOT set.

Expected:
I would expect the progress bar not to fail if an empty Iterable is presented to it.

How to reproduce

$progressBar = new ProgressBar(new ConsoleOutput(OutputInterface::VERBOSITY_DEBUG);
$progressBar->setFormat('debug');

// this very array sources somewhere in a bigger project and it just happens to be empty some times
$incoming = array();

foreach($progressBar->iterate($incoming) as $current) {
   var_dump($current);
}

Possible Solution

Changing the line mentioned above:

                if (!$bar->getMaxSteps()) {
                    throw new LogicException('Unable to display the ... if the maximum number of steps is not set.');
                }

(https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Console/Helper/ProgressBar.php#L522 and https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Console/Helper/ProgressBar.php#L529)

to

                if (!isset($bar->max)) {
                    throw new LogicException('Unable to display the ... if the maximum number of steps is not set.');
                }

seem to be semantically more correct. And it makes the ProgressBar behave like I expected it to do. If you guys are ok with this approach, I can provider a PR.

Additional Context

No response

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions