Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return integer|false from parse_bytes() function #9271

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,11 @@ parameters:
count: 1
path: program/lib/Roundcube/bootstrap.php

-
message: "#^Variable \\$str on left side of \\?\\? always exists and is not nullable\\.$#"
count: 1
path: program/lib/Roundcube/bootstrap.php

-
message: "#^Default value of the parameter \\#3 \\$ttl \\(int\\) of method rcube_cache_apc\\:\\:__construct\\(\\) is incompatible with type string\\.$#"
count: 1
Expand Down
33 changes: 13 additions & 20 deletions program/lib/Roundcube/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,35 +148,28 @@ function in_array_nocase($needle, $haystack)
*
* @param string $str Input string
*
* @return float Number of bytes
* @return int|false Number of bytes
*/
function parse_bytes($str)
{
if (is_numeric($str)) {
return floatval($str);
}

$bytes = 0;

if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', (string) $str, $regs)) {
if (preg_match('/^([0-9\.]+)\s*([KMGT]?)I?B?$/', trim(strtoupper($str ?? '')), $regs)) {
$bytes = floatval($regs[1]);
switch (strtolower($regs[2])) {
case 'g':
case 'gb':
$bytes *= 1073741824;
break;
case 'm':
case 'mb':
$bytes *= 1048576;
break;
case 'k':
case 'kb':
switch ($regs[2]) {
case 'T':
$bytes *= 1024;
case 'G':
$bytes *= 1024;
case 'M':
$bytes *= 1024;
case 'K':
$bytes *= 1024;
break;
}

return (int) round($bytes);
}

return floatval($bytes);
return false;
}

/**
Expand Down
31 changes: 21 additions & 10 deletions tests/Framework/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,39 @@ function test_in_array_nocase()
function test_parse_bytes()
{
$data = [
'0' => 0,
'1' => 1,
'1024' => 1024,
' 10 ' => 10,

'2k' => 2 * 1024,
'2m' => 2 * 1024 * 1024,
'2g' => 2 * 1024 * 1024 * 1024,
'2t' => 2 * 1024 * 1024 * 1024 * 1024,

'2 k' => 2 * 1024,
'2kb' => 2 * 1024,
'2kB' => 2 * 1024,
'2m' => 2 * 1048576,
'2 m' => 2 * 1048576,
'2mb' => 2 * 1048576,
'2mB' => 2 * 1048576,
'2g' => 2 * 1024 * 1048576,
'2 g' => 2 * 1024 * 1048576,
'2gb' => 2 * 1024 * 1048576,
'2gB' => 2 * 1024 * 1048576,
'2KiB' => 2 * 1024,
'2 m' => 2 * 1024 * 1024,
'2TB' => 2 * 1024 * 1024 * 1024 * 1024,

'2.5k' => (int) round(2.5 * 1024),
'0.01 MiB' => (int) round(0.01 * 1024 * 1024),

'' => false,
'-1' => false,
'1 1' => false,
'1BB' => false,
'1MM' => false,
];

foreach ($data as $value => $expected) {
$result = parse_bytes($value);
$this->{'assertEquals'}($expected, $result, "Invalid parse_bytes() result for $value");
$this->assertSame($expected, $result, "Invalid parse_bytes() result for $value");
}

$this->assertSame(0.0, parse_bytes(null));
$this->assertFalse(parse_bytes(null));
}

/**
Expand Down