wp-cli / php-cli-tools

A collection of tools to help with PHP command line utilities
MIT License
673 stars 118 forks source link

Off-by-one error in progress bar example code #127

Closed schlessera closed 6 years ago

schlessera commented 6 years ago

The example code contains an off-by-one error in its test_notify() method: https://github.com/wp-cli/php-cli-tools/blob/master/examples/common.php#L20

From the original report by @lf-jeremy:

The test_notify function has a default cycle of 1000000 iterations. The example constructors for \cli\Progress\bar objects pass a size of 1000000 steps. However, the for loop inside test_notify starts at 0 and goes to $cycle inclusive.

lf-jeremy commented 6 years ago

It's also worth noting that it'd be more DRY to introspect the total from the \cli\progress\Bar object than to pass it to the test_notify function as a parameter. The existing cli\Progress::total() method returns the _total property, but passes it through number_format first. Unfortunately, a getter for the raw value may be easily confused with the existing total() method, which formats the number.