Skip to content

Commit da7e40d

Browse files
committed
Fixes
1 parent 7dba502 commit da7e40d

3 files changed

Lines changed: 16 additions & 17 deletions

File tree

‎src/Dashboards/Realpath/RealpathTrait.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
trait RealpathTrait {
1616
private function panels(): string {
17-
$total_memory = Format::iniSizeToBytes(ini_get('realpath_cache_size'));
17+
$total_memory = Format::iniSizeToBytes((string) ini_get('realpath_cache_size'));
1818
$memory_used = realpath_cache_size();
19-
$memory_usage = round(($memory_used / $total_memory) * 100, 2);
19+
$memory_usage = $total_memory > 0 ? round(($memory_used / $total_memory) * 100, 2) : 0;
2020

2121
$panels = [
2222
[
@@ -42,7 +42,7 @@ private function panels(): string {
4242
* @return array<int, array<string, string|int>>
4343
*/
4444
private function getAllKeys(): array {
45-
static $keys = [];
45+
$keys = [];
4646
$search = Http::get('s', '');
4747

4848
$this->template->addGlobal('search_value', $search);

‎src/Format.php‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,20 @@ public static function bytes(int $bytes, int $decimals = 2): string {
2929
* @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
3030
*/
3131
public static function iniSizeToBytes(string $ini_size): int {
32-
$size = (int) substr($ini_size, 0, -1);
3332
$unit = strtoupper(substr($ini_size, -1));
3433

35-
switch ($unit) {
36-
case 'K':
37-
$size *= 1024;
38-
break;
39-
case 'M':
40-
$size *= 1024 * 1024;
41-
break;
42-
case 'G':
43-
$size *= 1024 * 1024 * 1024;
44-
break;
45-
default:
46-
break;
34+
// Values without a shorthand suffix are plain bytes.
35+
if (!in_array($unit, ['K', 'M', 'G'], true)) {
36+
return (int) $ini_size;
4737
}
4838

49-
return $size;
39+
$size = (int) substr($ini_size, 0, -1);
40+
41+
return match ($unit) {
42+
'K' => $size * 1024,
43+
'M' => $size * 1024 * 1024,
44+
'G' => $size * 1024 * 1024 * 1024,
45+
};
5046
}
5147

5248
public static function seconds(int $time): string {

‎tests/FormatTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public function testIniSizeToBytes(): void {
2020
$this->assertSame(10_240, Format::iniSizeToBytes('10K'));
2121
$this->assertSame(10_485_760, Format::iniSizeToBytes('10M'));
2222
$this->assertSame(10_737_418_240, Format::iniSizeToBytes('10G'));
23+
$this->assertSame(10_240, Format::iniSizeToBytes('10k'));
24+
$this->assertSame(16_777_216, Format::iniSizeToBytes('16777216'));
25+
$this->assertSame(0, Format::iniSizeToBytes('0'));
2326
}
2427

2528
public function testSeconds(): void {

0 commit comments

Comments
 (0)