Skip to content

Commit de53b38

Browse files
committed
refactor: differentiate between kilo/kibibytes and mega/mebibytes
1 parent fff0c87 commit de53b38

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

system/Files/File.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,18 @@ public function getSize()
7272

7373
/**
7474
* Retrieve the file size by unit.
75+
* Supports the metric units "kb" and "mb"
76+
* and the IEC units "kib" and "mib".
7577
*
7678
* @return false|int|string
7779
*/
7880
public function getSizeByUnit(string $unit = 'b')
7981
{
8082
return match (strtolower($unit)) {
81-
'kb' => number_format($this->getSize() / 1024, 3),
82-
'mb' => number_format(($this->getSize() / 1024) / 1024, 3),
83+
'kb' => number_format($this->getSize() / 1000, 3),
84+
'kib' => number_format($this->getSize() / 1024, 3),
85+
'mb' => number_format(($this->getSize() / 1000) / 1000, 3),
86+
'mib' => number_format(($this->getSize() / 1024) / 1024, 3),
8387
default => $this->getSize(),
8488
};
8589
}

tests/system/Files/FileTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,31 @@ public function testCanAccessSplFileInfoMethods(): void
9292
$this->assertSame('file', $file->getType());
9393
}
9494

95-
public function testGetSizeReturnsKB(): void
95+
public function testGetSizeReturnsKiB(): void
9696
{
9797
$file = new File(SYSTEMPATH . 'Common.php');
9898
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024, 3);
99+
$this->assertSame($size, $file->getSizeByUnit('kib'));
100+
}
101+
102+
public function testGetSizeReturnsKB(): void
103+
{
104+
$file = new File(SYSTEMPATH . 'Common.php');
105+
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1000, 3);
99106
$this->assertSame($size, $file->getSizeByUnit('kb'));
100107
}
101108

102-
public function testGetSizeReturnsMB(): void
109+
public function testGetSizeReturnsMiB(): void
103110
{
104111
$file = new File(SYSTEMPATH . 'Common.php');
105112
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024 / 1024, 3);
113+
$this->assertSame($size, $file->getSizeByUnit('mib'));
114+
}
115+
116+
public function testGetSizeReturnsMB(): void
117+
{
118+
$file = new File(SYSTEMPATH . 'Common.php');
119+
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1000 / 1000, 3);
106120
$this->assertSame($size, $file->getSizeByUnit('mb'));
107121
}
108122

0 commit comments

Comments
 (0)