Skip to content

Commit

Permalink
method names, visibility, return types
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasMeschke committed Nov 19, 2024
1 parent 066c03d commit 5bb2170
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 26 deletions.
19 changes: 6 additions & 13 deletions system/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ public function getSize()
* Retrieve the file size by unit, calculated in IEC standards with 1024 as base value.
*
* @phpstan-param positive-int $precision
*
* @return false|int|string
*/
public function getSizeByUnitBinary(FileSizeUnit $unit = FileSizeUnit::B, int $precision = 3)
public function getSizeByBinaryUnit(FileSizeUnit $unit = FileSizeUnit::B, int $precision = 3): int|string
{
return $this->getSizeByUnitInternal(1024, $unit, $precision);
}
Expand All @@ -86,26 +84,24 @@ public function getSizeByUnitBinary(FileSizeUnit $unit = FileSizeUnit::B, int $p
* Retrieve the file size by unit, calculated in metric standards with 1000 as base value.
*
* @phpstan-param positive-int $precision
*
* @return false|int|string
*/
public function getSizeByUnitMetric(FileSizeUnit $unit = FileSizeUnit::B, int $precision = 3)
public function getSizeByMetricUnit(FileSizeUnit $unit = FileSizeUnit::B, int $precision = 3): int|string
{
return $this->getSizeByUnitInternal(1000, $unit, $precision);
}

/**
* Retrieve the file size by unit.
*
* @deprecated 4.6.0 Use getSizeByUnitBinary() or getSizeByUnitMetric() instead
* @deprecated 4.6.0 Use getSizeByBinaryUnit() or getSizeByMetricUnit() instead
*
* @return false|int|string
*/
public function getSizeByUnit(string $unit = 'b')
{
return match (strtolower($unit)) {
'kb' => $this->getSizeByUnitBinary(FileSizeUnit::KB),
'mb' => $this->getSizeByUnitBinary(FileSizeUnit::MB),
'kb' => $this->getSizeByBinaryUnit(FileSizeUnit::KB),
'mb' => $this->getSizeByBinaryUnit(FileSizeUnit::MB),
default => $this->getSize()
};
}
Expand Down Expand Up @@ -216,10 +212,7 @@ public function getDestination(string $destination, string $delimiter = '_', int
return $destination;
}

/**
* @return false|int|string
*/
protected function getSizeByUnitInternal(int $fileSizeBase, FileSizeUnit $unit, int $precision)
private function getSizeByUnitInternal(int $fileSizeBase, FileSizeUnit $unit, int $precision): int|string
{
$exponent = $unit->value;
$divider = $fileSizeBase ** $exponent;
Expand Down
8 changes: 4 additions & 4 deletions tests/system/Files/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ public function testGetSizeBinary(FileSizeUnit $unit): void
$divider = 1024 ** $unit->value;
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / $divider, 3);
$this->assertSame($size, $file->getSizeByUnitBinary($unit));
$this->assertSame($size, $file->getSizeByBinaryUnit($unit));
}

public function testGetSizeBinaryBytes(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = filesize(SYSTEMPATH . 'Common.php');
$this->assertSame($size, $file->getSizeByUnitBinary(FileSizeUnit::B));
$this->assertSame($size, $file->getSizeByBinaryUnit(FileSizeUnit::B));
}

#[DataProvider('provideGetSizeData')]
Expand All @@ -136,14 +136,14 @@ public function testGetSizeMetric(FileSizeUnit $unit): void
$divider = 1000 ** $unit->value;
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / $divider, 3);
$this->assertSame($size, $file->getSizeByUnitMetric($unit));
$this->assertSame($size, $file->getSizeByMetricUnit($unit));
}

public function testGetSizeMetricBytes(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = filesize(SYSTEMPATH . 'Common.php');
$this->assertSame($size, $file->getSizeByUnitMetric(FileSizeUnit::B));
$this->assertSame($size, $file->getSizeByMetricUnit(FileSizeUnit::B));
}

public function testThrowsExceptionIfNotAFile(): void
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/changelogs/v4.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Deprecations
- The ``Filters::getArguments()`` method has been deprecated. No longer used.
- **File:**
- The function ``getSizeByUnit()`` of ``File`` has been deprecated.
Use either ``getSizeByUnitBinary()`` or ``getSizeByUnitMetric()`` instead.
Use either ``getSizeByBinaryUnit()`` or ``getSizeByMetricUnit()`` instead.

**********
Bugs Fixed
Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/libraries/files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ the results in kibibytes or mebibytes, respectively:

A ``RuntimeException`` will be thrown if the file does not exist or an error occurs.

getSizeByUnitBinary()
getSizeByBinaryUnit()
=====================

Returns the size of the file default in bytes. You can pass in different FileSizeUnit values as the first parameter to get
Expand All @@ -78,7 +78,7 @@ the amount of decimal places.

A ``RuntimeException`` will be thrown if the file does not exist or an error occurs.

getSizeByUnitMetric()
getSizeByMetricUnit()
=====================

Returns the size of the file default in bytes. You can pass in different FileSizeUnit values as the first parameter to get
Expand Down
6 changes: 3 additions & 3 deletions user_guide_src/source/libraries/files/017.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

use CodeIgniter\Files\FileSizeUnit;

$bytes = $file->getSizeByUnitBinary(); // 256901
$kibibytes = $file->getSizeByUnitBinary(FileSizeUnit::KB); // 250.880
$mebibytes = $file->getSizeByUnitBinary(FileSizeUnit::MB); // 0.245
$bytes = $file->getSizeByBinaryUnit(); // 256901
$kibibytes = $file->getSizeByBinaryUnit(FileSizeUnit::KB); // 250.880
$mebibytes = $file->getSizeByBinaryUnit(FileSizeUnit::MB); // 0.245
6 changes: 3 additions & 3 deletions user_guide_src/source/libraries/files/018.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

use CodeIgniter\Files\FileSizeUnit;

$bytes = $file->getSizeByUnitMetric(); // 256901
$kilobytes = $file->getSizeByUnitMetric(FileSizeUnit::KB); // 256.901
$megabytes = $file->getSizeByUnitMetric(FileSizeUnit::MB); // 0.256
$bytes = $file->getSizeByMetricUnit(); // 256901
$kilobytes = $file->getSizeByMetricUnit(FileSizeUnit::KB); // 256.901
$megabytes = $file->getSizeByMetricUnit(FileSizeUnit::MB); // 0.256

0 comments on commit 5bb2170

Please sign in to comment.