@@ -52,17 +52,14 @@ class ProgressBar
52
52
*
53
53
* @param OutputInterface $output An OutputInterface instance
54
54
* @param int $max Maximum steps (0 if unknown)
55
+ *
56
+ * @throws \InvalidArgumentException
55
57
*/
56
58
public function __construct (OutputInterface $ output , $ max = 0 )
57
59
{
58
- if (!is_integer ($ max ) || $ max < 0 ) {
59
- throw new \InvalidArgumentException ('Max steps should be a positive integer, 0 or null. Got "%s". ' , $ max );
60
- }
61
-
62
60
// Disabling output when it does not support ANSI codes as it would result in a broken display anyway.
63
61
$ this ->output = $ output ->isDecorated () ? $ output : new NullOutput ();
64
- $ this ->max = (int ) $ max ;
65
- $ this ->stepWidth = $ this ->max > 0 ? Helper::strlen ($ this ->max ) : 4 ;
62
+ $ this ->setMaxSteps ($ max );
66
63
67
64
if (!self ::$ formatters ) {
68
65
self ::$ formatters = self ::initPlaceholderFormatters ();
@@ -167,6 +164,25 @@ public function getStartTime()
167
164
return $ this ->startTime ;
168
165
}
169
166
167
+ /**
168
+ * Sets the progress bar maximal steps.
169
+ *
170
+ * @param int The progress bar max steps
171
+ *
172
+ * @throws \InvalidArgumentException
173
+ */
174
+ public function setMaxSteps ($ max )
175
+ {
176
+ $ max = (int ) $ max ;
177
+
178
+ if ($ max < 0 ) {
179
+ throw new \InvalidArgumentException ('Max steps should be a positive integer, 0 or null. Got "%s". ' , $ max );
180
+ }
181
+
182
+ $ this ->max = $ max ;
183
+ $ this ->stepWidth = $ this ->max > 0 ? Helper::strlen ($ this ->max ) : 4 ;
184
+ }
185
+
170
186
/**
171
187
* Gets the progress bar maximal steps.
172
188
*
@@ -338,8 +354,7 @@ public function setRedrawFrequency($freq)
338
354
public function start ($ max = 0 )
339
355
{
340
356
if (0 !== $ max ) {
341
- $ this ->max = $ max ;
342
- $ this ->stepWidth = $ this ->max > 0 ? Helper::strlen ($ this ->max ) : 4 ;
357
+ $ this ->setMaxSteps ($ max );
343
358
}
344
359
345
360
if (!$ this ->max ) {
@@ -377,7 +392,7 @@ public function setCurrent($step)
377
392
}
378
393
379
394
if ($ this ->max > 0 && $ step > $ this ->max ) {
380
- throw new \ LogicException ( ' You can \' t advance the progress bar past the max value. ' ) ;
395
+ $ this -> max = $ step ;
381
396
}
382
397
383
398
$ prevPeriod = intval ($ this ->step / $ this ->redrawFreq );
0 commit comments