Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

Commit 7c26456

Browse files
committed
Extract number formatter util
NumberFormatter which strips tailing zeros Updated CL Make the behavior optional Update schema Update docs Disable trailing zeros by default in PHPBench
1 parent e0b8546 commit 7c26456

8 files changed

Lines changed: 118 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ master
66

77
Improvements
88

9-
- Show more concise error messages including script exit code
9+
- Show more concise error messages including script exit code #969
10+
- Configuration (`expression.strip_tailing_zeros`) to strip meaningless zeros after the decimal place #958
1011

1112
1.2.3
1213
------

‎docs/configuration.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,17 @@ Types: ``["string"]``
544544

545545
Allowed values: ``["binary","decimal"]``
546546

547+
.. _configuration_expression_strip_tailing_zeros:
548+
549+
expression.strip_tailing_zeros
550+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
551+
552+
Do not display meaningless zeros after the decimal place
553+
554+
Default: ``false``
555+
556+
Types: ``["bool"]``
557+
547558
Storage
548559
-------
549560

‎lib/Expression/NodePrinter/DisplayAsPrinter.php‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpBench\Expression\NodePrinter;
1515
use PhpBench\Expression\Printer;
1616
use PhpBench\Util\MemoryUnit;
17+
use PhpBench\Util\NumberFormat;
1718
use PhpBench\Util\TimeUnit;
1819

1920
class DisplayAsPrinter implements NodePrinter
@@ -34,10 +35,16 @@ class DisplayAsPrinter implements NodePrinter
3435
*/
3536
private $memoryAsBinary;
3637

37-
public function __construct(TimeUnit $timeUnit, bool $memoryAsBinary)
38+
/**
39+
* @var bool
40+
*/
41+
private $stripTailingZeros;
42+
43+
public function __construct(TimeUnit $timeUnit, bool $memoryAsBinary, bool $stripTailingZeros = false)
3844
{
3945
$this->timeUnit = $timeUnit;
4046
$this->memoryAsBinary = $memoryAsBinary;
47+
$this->stripTailingZeros = $stripTailingZeros;
4148
}
4249

4350
public function print(Printer $printer, Node $node): ?string
@@ -115,13 +122,14 @@ public static function supportedUnitNames(): array
115122

116123
private function timeUnit(float $value, ?string $unit, ?int $precision, string $prettyUnit, ?string $mode): string
117124
{
118-
return sprintf('%s%s', number_format(
125+
return sprintf('%s%s', NumberFormat::format(
119126
$this->timeUnit->toDestUnit(
120127
$value,
121128
$this->timeUnit->resolveDestUnit($unit, $value),
122129
$mode
123130
),
124-
$precision ?: $this->timeUnit->getPrecision()
131+
$precision ?: $this->timeUnit->getPrecision(),
132+
$this->stripTailingZeros,
125133
), $prettyUnit);
126134
}
127135

@@ -132,7 +140,11 @@ private function memoryUnit(float $value, string $unit, ?int $precision, string
132140
{
133141
return sprintf(
134142
'%s%s',
135-
number_format(MemoryUnit::convertTo($value, MemoryUnit::BYTES, $unit), $unit === MemoryUnit::BYTES ? 0 : ($precision ?: 3)),
143+
NumberFormat::format(
144+
MemoryUnit::convertTo($value, MemoryUnit::BYTES, $unit),
145+
$unit === MemoryUnit::BYTES ? 0 : ($precision ?: 3),
146+
$this->stripTailingZeros
147+
),
136148
$prettyUnit
137149
);
138150
}

‎lib/Extension/ExpressionExtension.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class ExpressionExtension implements ExtensionInterface
130130
public const PARAM_SYNTAX_HIGHLIGHTING = 'expression.syntax_highlighting';
131131
public const PARAM_THEME = 'expression.theme';
132132
public const PARAM_MEMORY_UNIT_PREFIX = 'expression.memory_unit_prefix';
133+
public const PARAM_STRIP_TAILING_ZEROS = 'expression.strip_tailing_zeros';
133134

134135
public const SERVICE_PLAIN_PRINTER = 'expression.printer.plain';
135136
public const SERVICE_BARE_PRINTER = 'expression.printer.bare';
@@ -296,7 +297,8 @@ public function load(Container $container): void
296297
new ValueWithUnitPrinter(),
297298
new DisplayAsPrinter(
298299
$container->get(TimeUnit::class),
299-
$container->getParameter(self::PARAM_MEMORY_UNIT_PREFIX) === self::PREFIX_BINARY
300+
$container->getParameter(self::PARAM_MEMORY_UNIT_PREFIX) === self::PREFIX_BINARY,
301+
$container->getParameter(self::PARAM_STRIP_TAILING_ZEROS)
300302
),
301303
new ParameterPrinter(),
302304
new StringPrinter(),
@@ -390,14 +392,18 @@ public function configure(OptionsResolver $resolver): void
390392
self::PARAM_SYNTAX_HIGHLIGHTING => true,
391393
self::PARAM_THEME => self::THEME_SOLARIZED,
392394
self::PARAM_MEMORY_UNIT_PREFIX => self::PREFIX_DECIMAL,
395+
self::PARAM_STRIP_TAILING_ZEROS => false,
393396
]);
394397
$resolver->setAllowedTypes(self::PARAM_SYNTAX_HIGHLIGHTING, 'bool');
395398
$resolver->setAllowedTypes(self::PARAM_THEME, 'string');
396399
$resolver->setAllowedTypes(self::PARAM_MEMORY_UNIT_PREFIX, ['string']);
400+
$resolver->setAllowedTypes(self::PARAM_STRIP_TAILING_ZEROS, ['bool']);
397401
$resolver->setAllowedValues(self::PARAM_MEMORY_UNIT_PREFIX, [self::PREFIX_BINARY, self::PREFIX_DECIMAL]);
402+
398403
SymfonyOptionsResolverCompat::setInfos($resolver, [
399404
self::PARAM_SYNTAX_HIGHLIGHTING => 'Enable syntax highlighting',
400405
self::PARAM_THEME => 'Select a theme to use',
406+
self::PARAM_STRIP_TAILING_ZEROS => 'Do not display meaningless zeros after the decimal place',
401407
self::PARAM_MEMORY_UNIT_PREFIX => sprintf(
402408
'By default use ``%s`` (1kb = 1000 bytes) or ``%s`` (1KiB = 1024 bytes) when displaying memory',
403409
self::PREFIX_DECIMAL,

‎lib/Util/NumberFormat.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace PhpBench\Util;
4+
5+
use function number_format;
6+
7+
class NumberFormat
8+
{
9+
/**
10+
* Similar to the built-in number_format but meaningless zeros after the
11+
* decimal place are trimmed.
12+
*/
13+
public static function format(float $number, int $decimals, bool $stripTailingZeros): string
14+
{
15+
$formated = number_format($number, $decimals);
16+
17+
if ($stripTailingZeros && false !== strpos($formated, '.')) {
18+
$formated = rtrim($formated, '0');
19+
20+
if (substr($formated, -1) === '.') {
21+
return (string)$number;
22+
}
23+
}
24+
25+
return $formated;
26+
}
27+
}

‎phpbench.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"runner.retry_threshold": 5,
1616
"runner.revs": 10,
1717
"runner.assert": "mode(variant.time.avg) as ms <= mode(baseline.time.avg) as ms +/- 5%",
18+
"expression.strip_tailing_zeros": true,
1819
"report.generators": {
1920
"examples": {
2021
"generator": "composite",

‎phpbench.schema.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@
317317
"decimal"
318318
]
319319
},
320+
"expression.strip_tailing_zeros": {
321+
"description": "Do not display meaningless zeros after the decimal place",
322+
"type": [
323+
"boolean"
324+
]
325+
},
320326
"storage.driver": {
321327
"description": "Storage driver to use",
322328
"type": [
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace PhpBench\Tests\Unit\Util;
4+
5+
use Generator;
6+
use PHPUnit\Framework\TestCase;
7+
use PhpBench\Util\NumberFormat;
8+
9+
class NumberFormatTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider provideFormat
13+
*/
14+
public function testFormat(float $number, int $decimals, bool $stripTrailingZeros, string $expected): void
15+
{
16+
self::assertEquals($expected, NumberFormat::format($number, $decimals, $stripTrailingZeros));
17+
}
18+
19+
/**
20+
* @return Generator<mixed>
21+
*/
22+
public function provideFormat(): Generator
23+
{
24+
yield [0, 0, true, '0'];
25+
26+
yield [0, 1, true, '0'];
27+
28+
yield [0, 2, true, '0'];
29+
30+
yield [10.12, 2, true, '10.12'];
31+
32+
yield [10.12, 3, true, '10.12'];
33+
34+
yield [10.000, 3, true, '10'];
35+
36+
yield [0, 0, false, '0'];
37+
38+
yield [0, 1, false, '0.0'];
39+
40+
yield [0, 2, false, '0.00'];
41+
42+
yield [10.12, 2, false, '10.12'];
43+
44+
yield [10.12, 3, false, '10.120'];
45+
46+
yield [10.000, 3, false, '10.000'];
47+
}
48+
}

0 commit comments

Comments
 (0)