Skip to content

Commit 272b117

Browse files
committed
Added PHP 8.1 support.
Migrated from Travis CI to GHA.
1 parent b99205d commit 272b117

File tree

8 files changed

+75
-64
lines changed

8 files changed

+75
-64
lines changed

.github/workflows/CI.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: 0 6 * * *
8+
9+
jobs:
10+
Test:
11+
runs-on: ubuntu-20.04
12+
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
php:
17+
- 8.1
18+
- 8.0
19+
dependencies:
20+
- hi
21+
- lo
22+
23+
steps:
24+
- uses: actions/checkout@v2
25+
26+
- name: Setup PHP ${{ matrix.php }}
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php }}
30+
coverage: xdebug
31+
32+
- name: Validate composer.json
33+
run: composer validate
34+
35+
- name: Cache dependencies
36+
id: composer-cache
37+
uses: actions/cache@v2
38+
with:
39+
path: vendor
40+
key: php-${{ matrix.php }}-${{ matrix.dependencies }}-${{ hashFiles('composer.json') }}
41+
restore-keys: php-${{ matrix.php }}-${{ matrix.dependencies }}-
42+
43+
- name: Install dependencies ${{ matrix.dependencies == 'lo' && '(lowest)' || '' }}
44+
run: composer update --no-interaction --no-progress
45+
${{ matrix.dependencies == 'lo' && '--prefer-lowest' || '' }}
46+
47+
- name: Run test suite
48+
run: composer test -- --coverage-clover=build/logs/clover.xml
49+
50+
- name: Upload test coverage
51+
run: |
52+
composer global require php-coveralls/php-coveralls:^2
53+
php-coveralls -v
54+
env:
55+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.travis.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
},
1616
"require": {
17-
"php": "^7.3|^8",
17+
"php": "^8",
1818
"scriptfusion/static-class": "^1"
1919
},
2020
"require-dev": {

src/Byte/ByteFormatter.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,19 @@
99
*/
1010
class ByteFormatter
1111
{
12-
private const DEFAULT_BASE = Base::BINARY;
12+
private int $base = Base::BINARY;
1313

14-
/** @var int */
15-
private $base = self::DEFAULT_BASE;
14+
private string $format;
1615

17-
/** @var string */
18-
private $format;
16+
private string $sprintfFormat;
1917

20-
/** @var string */
21-
private $sprintfFormat;
18+
private int $precision = 0;
2219

23-
/** @var int */
24-
private $precision = 0;
20+
private bool $automaticPrecision = true;
2521

26-
/** @var bool */
27-
private $automaticPrecision = true;
22+
private ?int $exponent = null;
2823

29-
/** @var int */
30-
private $exponent;
31-
32-
/** @var UnitDecorator */
33-
private $unitDecorator;
24+
private UnitDecorator $unitDecorator;
3425

3526
/**
3627
* Initializes this instance, optionally with a specific unit decorator.
@@ -54,13 +45,13 @@ public function __construct(UnitDecorator $unitDecorator = null)
5445
*
5546
* @return string Formatted bytes.
5647
*/
57-
public function format($bytes, int $precision = null): string
48+
public function format(int|float $bytes, int $precision = null): string
5849
{
5950
// Use default precision when not specified.
6051
$precision === null && $precision = $this->getPrecision();
6152

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

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

9081
if (isset($formattedParts[1])) {
9182
// Strip trailing 0s in fractional part.
92-
if (!$formattedParts[1] = chop($formattedParts[1], '0')) {
83+
if (!$formattedParts[1] = rtrim($formattedParts[1], '0')) {
9384
// Remove fractional part.
9485
unset($formattedParts[1]);
9586
}
9687

97-
$formatted = join('.', $formattedParts);
88+
$formatted = implode('.', $formattedParts);
9889
}
9990
}
10091

src/Byte/Unit/NameDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
class NameDecorator implements UnitDecorator
1010
{
11-
protected static $sequences = [
11+
protected static array $sequences = [
1212
Base::BINARY => ['kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi', 'zebi', 'yobi'],
1313
Base::DECIMAL => ['kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'yotta'],
1414
];

src/Byte/Unit/SymbolDecorator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class SymbolDecorator implements UnitDecorator
1313
const SUFFIX_METRIC = 'B';
1414
const SUFFIX_IEC = 'iB';
1515

16-
private $prefixes = self::PREFIXES;
17-
private $suffix;
18-
private $alwaysShowUnit;
16+
private string $prefixes = self::PREFIXES;
17+
private ?string $suffix;
18+
private bool $alwaysShowUnit = false;
1919

2020
public function __construct($suffix = null)
2121
{
@@ -72,9 +72,9 @@ public function setSuffix(?string $suffix): self
7272
return $this;
7373
}
7474

75-
public function alwaysShowUnit($show = true): SymbolDecorator
75+
public function alwaysShowUnit(bool $show = true): SymbolDecorator
7676
{
77-
$this->alwaysShowUnit = (bool)$show;
77+
$this->alwaysShowUnit = $show;
7878

7979
return $this;
8080
}

test/Integration/Byte/ByteFormatterTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
final class ByteFormatterTest extends TestCase
1111
{
12-
/** @var ByteFormatter */
13-
private $formatter;
12+
private ByteFormatter $formatter;
1413

1514
protected function setUp(): void
1615
{

test/Unit/Byte/ByteFormatterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class ByteFormatterTest extends TestCase
99
/**
1010
* @var ByteFormatter
1111
*/
12-
private $formatter;
12+
private ByteFormatter $formatter;
1313

1414
protected function setUp(): void
1515
{

0 commit comments

Comments
 (0)