Skip to content

Commit df65ffe

Browse files
committed
feature #39042 [Console] Extracting ProgressBar's format's magic strings into const (CesarScur)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Console] Extracting ProgressBar's format's magic strings into const | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | The existence of these magic string is propagating outside the project on the user land while constructing a command: ```php $bar->setFormat('very_verbose'); ``` Objective is to provide reusability and resilience to miss naming: ```php $bar->setFormat(ProgressBar::VERBOSE); ``` Commits ------- a4b2606 Extracting ProgressBar's format's magic strings into const
2 parents 7dcaf98 + a4b2606 commit df65ffe

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
*/
2727
final class ProgressBar
2828
{
29+
public const FORMAT_VERBOSE = 'verbose';
30+
public const FORMAT_VERY_VERBOSE = 'very_verbose';
31+
public const FORMAT_DEBUG = 'debug';
32+
public const FORMAT_NORMAL = 'normal';
33+
34+
private const FORMAT_VERBOSE_NOMAX = 'verbose_nomax';
35+
private const FORMAT_VERY_VERBOSE_NOMAX = 'very_verbose_nomax';
36+
private const FORMAT_DEBUG_NOMAX = 'debug_nomax';
37+
private const FORMAT_NORMAL_NOMAX = 'normal_nomax';
38+
2939
private $barWidth = 28;
3040
private $barChar;
3141
private $emptyBarChar = '-';
@@ -489,13 +499,13 @@ private function determineBestFormat(): string
489499
switch ($this->output->getVerbosity()) {
490500
// OutputInterface::VERBOSITY_QUIET: display is disabled anyway
491501
case OutputInterface::VERBOSITY_VERBOSE:
492-
return $this->max ? 'verbose' : 'verbose_nomax';
502+
return $this->max ? self::FORMAT_VERBOSE : self::FORMAT_VERBOSE_NOMAX;
493503
case OutputInterface::VERBOSITY_VERY_VERBOSE:
494-
return $this->max ? 'very_verbose' : 'very_verbose_nomax';
504+
return $this->max ? self::FORMAT_VERY_VERBOSE : self::FORMAT_VERY_VERBOSE_NOMAX;
495505
case OutputInterface::VERBOSITY_DEBUG:
496-
return $this->max ? 'debug' : 'debug_nomax';
506+
return $this->max ? self::FORMAT_DEBUG : self::FORMAT_DEBUG_NOMAX;
497507
default:
498-
return $this->max ? 'normal' : 'normal_nomax';
508+
return $this->max ? self::FORMAT_NORMAL : self::FORMAT_NORMAL_NOMAX;
499509
}
500510
}
501511

@@ -547,17 +557,17 @@ private static function initPlaceholderFormatters(): array
547557
private static function initFormats(): array
548558
{
549559
return [
550-
'normal' => ' %current%/%max% [%bar%] %percent:3s%%',
551-
'normal_nomax' => ' %current% [%bar%]',
560+
self::FORMAT_NORMAL => ' %current%/%max% [%bar%] %percent:3s%%',
561+
self::FORMAT_NORMAL_NOMAX => ' %current% [%bar%]',
552562

553-
'verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
554-
'verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
563+
self::FORMAT_VERBOSE => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%',
564+
self::FORMAT_VERBOSE_NOMAX => ' %current% [%bar%] %elapsed:6s%',
555565

556-
'very_verbose' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%',
557-
'very_verbose_nomax' => ' %current% [%bar%] %elapsed:6s%',
566+
self::FORMAT_VERY_VERBOSE => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%',
567+
self::FORMAT_VERY_VERBOSE_NOMAX => ' %current% [%bar%] %elapsed:6s%',
558568

559-
'debug' => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
560-
'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
569+
self::FORMAT_DEBUG => ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%',
570+
self::FORMAT_DEBUG_NOMAX => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
561571
];
562572
}
563573

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testFormat()
210210

211211
// max in construct, explicit format before
212212
$bar = new ProgressBar($output = $this->getOutputStream(), 10, 0);
213-
$bar->setFormat('normal');
213+
$bar->setFormat(ProgressBar::FORMAT_NORMAL);
214214
$bar->start();
215215
$bar->advance(10);
216216
$bar->finish();
@@ -220,7 +220,7 @@ public function testFormat()
220220

221221
// max in start, explicit format before
222222
$bar = new ProgressBar($output = $this->getOutputStream(), 0, 0);
223-
$bar->setFormat('normal');
223+
$bar->setFormat(ProgressBar::FORMAT_NORMAL);
224224
$bar->start(10);
225225
$bar->advance(10);
226226
$bar->finish();
@@ -828,7 +828,7 @@ public function testAnsiColorsAndEmojis()
828828
public function testSetFormat()
829829
{
830830
$bar = new ProgressBar($output = $this->getOutputStream(), 0, 0);
831-
$bar->setFormat('normal');
831+
$bar->setFormat(ProgressBar::FORMAT_NORMAL);
832832
$bar->start();
833833
rewind($output->getStream());
834834
$this->assertEquals(
@@ -837,7 +837,7 @@ public function testSetFormat()
837837
);
838838

839839
$bar = new ProgressBar($output = $this->getOutputStream(), 10, 0);
840-
$bar->setFormat('normal');
840+
$bar->setFormat(ProgressBar::FORMAT_NORMAL);
841841
$bar->start();
842842
rewind($output->getStream());
843843
$this->assertEquals(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy