Skip to content

Commit

Permalink
Added PHP 8.1 support.
Browse files Browse the repository at this point in the history
Migrated from Travis CI to GHA.
  • Loading branch information
Bilge committed Dec 10, 2021
1 parent b99205d commit 272b117
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 64 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI

on:
push:
pull_request:
schedule:
- cron: 0 6 * * *

jobs:
Test:
runs-on: ubuntu-20.04

strategy:
fail-fast: false
matrix:
php:
- 8.1
- 8.0
dependencies:
- hi
- lo

steps:
- uses: actions/checkout@v2

- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug

- name: Validate composer.json
run: composer validate

- name: Cache dependencies
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: php-${{ matrix.php }}-${{ matrix.dependencies }}-${{ hashFiles('composer.json') }}
restore-keys: php-${{ matrix.php }}-${{ matrix.dependencies }}-

- name: Install dependencies ${{ matrix.dependencies == 'lo' && '(lowest)' || '' }}
run: composer update --no-interaction --no-progress
${{ matrix.dependencies == 'lo' && '--prefer-lowest' || '' }}

- name: Run test suite
run: composer test -- --coverage-clover=build/logs/clover.xml

- name: Upload test coverage
run: |
composer global require php-coveralls/php-coveralls:^2
php-coveralls -v
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34 changes: 0 additions & 34 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
},
"require": {
"php": "^7.3|^8",
"php": "^8",
"scriptfusion/static-class": "^1"
},
"require-dev": {
Expand Down
31 changes: 11 additions & 20 deletions src/Byte/ByteFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,19 @@
*/
class ByteFormatter
{
private const DEFAULT_BASE = Base::BINARY;
private int $base = Base::BINARY;

/** @var int */
private $base = self::DEFAULT_BASE;
private string $format;

/** @var string */
private $format;
private string $sprintfFormat;

/** @var string */
private $sprintfFormat;
private int $precision = 0;

/** @var int */
private $precision = 0;
private bool $automaticPrecision = true;

/** @var bool */
private $automaticPrecision = true;
private ?int $exponent = null;

/** @var int */
private $exponent;

/** @var UnitDecorator */
private $unitDecorator;
private UnitDecorator $unitDecorator;

/**
* Initializes this instance, optionally with a specific unit decorator.
Expand All @@ -54,13 +45,13 @@ public function __construct(UnitDecorator $unitDecorator = null)
*
* @return string Formatted bytes.
*/
public function format($bytes, int $precision = null): string
public function format(int|float $bytes, int $precision = null): string
{
// Use default precision when not specified.
$precision === null && $precision = $this->getPrecision();

$log = log($bytes, $this->getBase());
$exponent = $this->hasFixedExponent() ? $this->getFixedExponent() : max(0, $log|0);
$exponent = $this->hasFixedExponent() ? $this->getFixedExponent() : max(0, (int)$log);
$value = round($this->getBase() ** ($log - $exponent), $precision);
$units = $this->getUnitDecorator()->decorate($exponent, $this->getBase(), $value);

Expand Down Expand Up @@ -89,12 +80,12 @@ private function formatValue(float $value, int $precision): string

if (isset($formattedParts[1])) {
// Strip trailing 0s in fractional part.
if (!$formattedParts[1] = chop($formattedParts[1], '0')) {
if (!$formattedParts[1] = rtrim($formattedParts[1], '0')) {
// Remove fractional part.
unset($formattedParts[1]);
}

$formatted = join('.', $formattedParts);
$formatted = implode('.', $formattedParts);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Byte/Unit/NameDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
class NameDecorator implements UnitDecorator
{
protected static $sequences = [
protected static array $sequences = [
Base::BINARY => ['kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi', 'zebi', 'yobi'],
Base::DECIMAL => ['kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'yotta'],
];
Expand Down
10 changes: 5 additions & 5 deletions src/Byte/Unit/SymbolDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class SymbolDecorator implements UnitDecorator
const SUFFIX_METRIC = 'B';
const SUFFIX_IEC = 'iB';

private $prefixes = self::PREFIXES;
private $suffix;
private $alwaysShowUnit;
private string $prefixes = self::PREFIXES;
private ?string $suffix;
private bool $alwaysShowUnit = false;

public function __construct($suffix = null)
{
Expand Down Expand Up @@ -72,9 +72,9 @@ public function setSuffix(?string $suffix): self
return $this;
}

public function alwaysShowUnit($show = true): SymbolDecorator
public function alwaysShowUnit(bool $show = true): SymbolDecorator
{
$this->alwaysShowUnit = (bool)$show;
$this->alwaysShowUnit = $show;

return $this;
}
Expand Down
3 changes: 1 addition & 2 deletions test/Integration/Byte/ByteFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

final class ByteFormatterTest extends TestCase
{
/** @var ByteFormatter */
private $formatter;
private ByteFormatter $formatter;

protected function setUp(): void
{
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/Byte/ByteFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class ByteFormatterTest extends TestCase
/**
* @var ByteFormatter
*/
private $formatter;
private ByteFormatter $formatter;

protected function setUp(): void
{
Expand Down

0 comments on commit 272b117

Please sign in to comment.