Skip to content

[Console] Impossible to extend ProgressBar #20427

Closed
@a-ast

Description

@a-ast

Helpers are not final classes, so it is assumed we can extend them.
If, for any chance, you needed to extend ProgressBar, it would fail.

This test

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;

class ProgressBarExtensibilityTest extends PHPUnit_Framework_TestCase
{
    public function testStart()
    {
        $progressBar = new ExtendedProgressBar(new DummyOutput());
        $progressBar->start(10);
    }
}

class ExtendedProgressBar extends ProgressBar
{

}

fails with the message

PHP Fatal error: Call to private method Symfony\Component\Console\Helper\ProgressBar::getStepWidth() from context 'ProgressBar'
in vendor/symfony/symfony/src/Symfony/Component/Console/Helper/ProgressBar.php on line 573

It happens because the private method getStepWidth is called in a closure:

'current' => function (ProgressBar $bar) {
      return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT);
},

It works only inside ProgressBar but ExtendedProgressBar has no access to private methods of the parent class.

My proposal is to change a visibility of ProgressBar::getStepWidth to protected.
All other ProgressBar methods that are used in the same context (in initPlaceholderFormatters) are public.

(I'd work on PR if there are no objections or other ideas.)

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