From f3b1603492ff657c502657ed9b098c4b0f095a65 Mon Sep 17 00:00:00 2001
From: Davide Bellettini <325358+dbellettini@users.noreply.github.com>
Date: Sun, 10 Aug 2025 13:22:26 +0200
Subject: [PATCH 1/6] Make exceptions extend meaningful parents
---
src/ByteUnits/ConversionException.php | 4 +---
src/ByteUnits/NegativeBytesException.php | 4 +---
src/ByteUnits/ParseException.php | 4 +---
3 files changed, 3 insertions(+), 9 deletions(-)
diff --git a/src/ByteUnits/ConversionException.php b/src/ByteUnits/ConversionException.php
index 06bac64..b4dfa3c 100644
--- a/src/ByteUnits/ConversionException.php
+++ b/src/ByteUnits/ConversionException.php
@@ -2,8 +2,6 @@
namespace ByteUnits;
-use Exception;
-
-class ConversionException extends Exception
+class ConversionException extends \InvalidArgumentException
{
}
diff --git a/src/ByteUnits/NegativeBytesException.php b/src/ByteUnits/NegativeBytesException.php
index 6599b95..37622a3 100644
--- a/src/ByteUnits/NegativeBytesException.php
+++ b/src/ByteUnits/NegativeBytesException.php
@@ -2,8 +2,6 @@
namespace ByteUnits;
-use Exception;
-
-class NegativeBytesException extends Exception
+class NegativeBytesException extends \DomainException
{
}
diff --git a/src/ByteUnits/ParseException.php b/src/ByteUnits/ParseException.php
index dea9c6c..3e23a28 100644
--- a/src/ByteUnits/ParseException.php
+++ b/src/ByteUnits/ParseException.php
@@ -2,8 +2,6 @@
namespace ByteUnits;
-use Exception;
-
-class ParseException extends Exception
+class ParseException extends \InvalidArgumentException
{
}
From 0e7ddd94164002da3caf40663d1ed8cf5f105638 Mon Sep 17 00:00:00 2001
From: Davide Bellettini <325358+dbellettini@users.noreply.github.com>
Date: Sun, 10 Aug 2025 13:53:58 +0200
Subject: [PATCH 2/6] Modernize for PHP 8.3
---
.env | 2 +
.github/workflows/ci.yml | 48 ++++++++++
.gitignore | 3 +-
Dockerfile | 43 +++++++++
Makefile | 47 ++++++++++
README.md | 3 +-
compose.dev.yaml | 9 ++
compose.yaml | 9 ++
composer.json | 46 ++++++---
phpunit.dist.xml | 25 +++++
phpunit.xml | 7 --
src/ByteUnits/Binary.php | 12 +--
src/ByteUnits/Metric.php | 14 +--
src/ByteUnits/Parser.php | 12 ++-
src/ByteUnits/System.php | 93 +++++++++----------
src/ByteUnits/functions.php | 24 +++--
tests/ByteUnits/ArithmeticTest.php | 39 +++++---
tests/ByteUnits/BinarySystemTest.php | 8 +-
tests/ByteUnits/BoxingTest.php | 7 +-
tests/ByteUnits/CompareTest.php | 4 +-
.../ConversionBetweenSystemsTest.php | 4 +-
tests/ByteUnits/FormatInBinarySystemTest.php | 4 +-
tests/ByteUnits/FormatInMetricSystemTest.php | 4 +-
tests/ByteUnits/MetricSystemTest.php | 8 +-
tests/ByteUnits/ParseTest.php | 24 +++--
25 files changed, 363 insertions(+), 136 deletions(-)
create mode 100644 .env
create mode 100644 .github/workflows/ci.yml
create mode 100644 Dockerfile
create mode 100644 Makefile
create mode 100644 compose.dev.yaml
create mode 100644 compose.yaml
create mode 100644 phpunit.dist.xml
delete mode 100644 phpunit.xml
diff --git a/.env b/.env
new file mode 100644
index 0000000..f9f2bc6
--- /dev/null
+++ b/.env
@@ -0,0 +1,2 @@
+COMPOSE_FILE=compose.yaml:compose.dev.yaml
+PHP_VERSION=8.3
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 0000000..2aaf4d6
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,48 @@
+name: CI Pipeline
+
+on:
+ push:
+ branches: [ "master" ]
+ pull_request:
+ branches: [ "master" ]
+
+permissions:
+ contents: read
+
+jobs:
+ strategy:
+ matrix:
+ php_version:
+ - 8.3
+ - 8.4
+ build:
+ runs-on: ubuntu-latest
+
+ env:
+ COMPOSE_FILE: compose.yaml
+ PHP_VERSION=${{ matrix.php_version }}
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Validate composer.json and composer.lock
+ run: composer validate --strict
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v3
+
+ - name: Build and push Docker image
+ uses: docker/build-push-action@v5
+ with:
+ context: .
+ target: ci
+ push: false
+ load: true
+ tags: recruiterphp/byte-units-php:php-${{ matrix.php_version }}-latest
+ build-args: |
+ PHP_VERSION=${{ matrix.php_version }}
+ cache-from: type=gha
+ cache-to: type=gha,mode=max
+
+ - name: Run test suite
+ run: make test
diff --git a/.gitignore b/.gitignore
index 303abbe..2540d6c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
-.*
+.*.cache
+.idea/
composer.phar
vendor/
composer.lock
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..ccdebfe
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,43 @@
+ARG PHP_VERSION=8.4
+
+FROM php:${PHP_VERSION}-cli AS base
+
+# Install system dependencies
+RUN apt-get update && apt-get install -y \
+ git \
+ unzip \
+ libssl-dev \
+ libcurl4-openssl-dev \
+ pkg-config \
+ && rm -rf /var/lib/apt/lists/*
+
+RUN docker-php-ext-install -j$(nproc) \
+ bcmath
+
+# Copy Composer from official image
+COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
+
+# Set working directory
+WORKDIR /app
+
+# Set environment variable for Composer
+ENV COMPOSER_ALLOW_SUPERUSER=1
+
+CMD ["tail", "-f", "/dev/null"]
+
+FROM base AS dev
+
+# Install XDebug extension
+RUN pecl install xdebug \
+ && docker-php-ext-enable xdebug
+
+FROM base AS ci
+
+# Copy composer files
+COPY composer.json composer.lock* ./
+
+# Install dependencies including dev dependencies for testing
+RUN composer install --optimize-autoloader
+
+# Copy application code
+COPY . .
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..b23f6db
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,47 @@
+.PHONY: build up down test test-coverage phpstan rector fix-cs install shell logs clean
+
+# Build the Docker image
+build:
+ docker compose build
+
+# Start the services
+up:
+ docker compose up -d
+
+# Stop the services
+down:
+ docker compose down
+
+# Install dependencies
+install:
+ docker compose run --rm php composer install
+
+# Run all tests
+test: up
+ docker compose exec php vendor/bin/phpunit
+
+# Run unit tests with coverage
+test-coverage: up
+ docker compose exec -e XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html var/coverage/
+
+phpstan: up
+ docker compose exec php vendor/bin/phpstan
+
+rector: up
+ docker compose exec php vendor/bin/rector
+
+fix-cs: up
+ docker compose exec php vendor/bin/php-cs-fixer fix -v
+
+# Open a shell in the PHP container
+shell:
+ docker compose exec php bash
+
+# View logs
+logs:
+ docker compose logs -f php
+
+# Clean up containers and volumes
+clean:
+ docker compose down -v
+ docker compose rm -f
diff --git a/README.md b/README.md
index f1494e6..75c06be 100644
--- a/README.md
+++ b/README.md
@@ -72,11 +72,12 @@ echo ByteUnits\bytes(1322000)->numberOfBytes(); // outputs 1322000
### Compare
There are a few methods that could be used to compare bytes in various units and systems
+
```php
isLessThan(ByteUnits\Binary::kilobytes(1)); // it's true
-ByteUnits\Metric::kilobytes(1)->isEqualTo(ByteUnits\Binary::bytes(1000)); // it's true
+ByteUnits\Metric::kilobytes(1)->equals(ByteUnits\Binary::bytes(1000)); // it's true
ByteUnits\Metric::kilobytes(1.3)->isGreaterThan(ByteUnits\Binary::kilobytes(1)); // it's true
```
diff --git a/compose.dev.yaml b/compose.dev.yaml
new file mode 100644
index 0000000..a2a82a6
--- /dev/null
+++ b/compose.dev.yaml
@@ -0,0 +1,9 @@
+services:
+ php:
+ build:
+ context: .
+ args:
+ - PHP_VERSION=${PHP_VERSION}
+ target: dev
+ volumes:
+ - .:/app
diff --git a/compose.yaml b/compose.yaml
new file mode 100644
index 0000000..9338c3f
--- /dev/null
+++ b/compose.yaml
@@ -0,0 +1,9 @@
+services:
+ php:
+ build:
+ context: .
+ args:
+ - PHP_VERSION=${PHP_VERSION}
+ tags:
+ - latest
+ working_dir: /app
diff --git a/composer.json b/composer.json
index 3138b54..864ad44 100644
--- a/composer.json
+++ b/composer.json
@@ -1,15 +1,36 @@
{
"name": "gabrielelana/byte-units",
"description": "Library to parse, format and convert byte units",
+ "license": "MIT",
"type": "library",
"version": "0.5.0",
- "keywords": ["byte", "units", "format", "parse", "convert", "size"],
- "homepage": "https://github.com/gabrielelana/byte-units",
- "license": "MIT",
- "authors": [{
- "name": "Gabriele Lana",
- "email": "gabriele.lana@gmail.com"
- }],
+ "keywords": [
+ "byte",
+ "units",
+ "format",
+ "parse",
+ "convert",
+ "size"
+ ],
+ "authors": [
+ {
+ "name": "Gabriele Lana",
+ "email": "gabriele.lana@gmail.com"
+ },
+ {
+ "name": "Contributors",
+ "homepage": "https://github.com/recruiterphp/byte-units/graphs/contributors"
+ }
+ ],
+ "homepage": "https://github.com/recruiterphp/byte-units",
+ "require": {
+ "php": "^8.3",
+ "ext-bcmath": "*"
+ },
+ "require-dev": {
+ "ergebnis/composer-normalize": "^2.47",
+ "phpunit/phpunit": "^12.3"
+ },
"autoload": {
"psr-4": {
"ByteUnits\\": "src/ByteUnits"
@@ -18,11 +39,12 @@
"src/ByteUnits/functions.php"
]
},
- "require": {
- "php": ">=5.4.0",
- "ext-bcmath": "*"
+ "config": {
+ "allow-plugins": {
+ "ergebnis/composer-normalize": true
+ }
},
- "require-dev": {
- "phpunit/phpunit": ">=4.0,<6.0"
+ "replaces": {
+ "gabrielelana/byte-units": "self.version"
}
}
diff --git a/phpunit.dist.xml b/phpunit.dist.xml
new file mode 100644
index 0000000..c2048ac
--- /dev/null
+++ b/phpunit.dist.xml
@@ -0,0 +1,25 @@
+
+
+
+
+ tests
+
+
+
+
+
+ src
+
+
+
diff --git a/phpunit.xml b/phpunit.xml
deleted file mode 100644
index ab4a71a..0000000
--- a/phpunit.xml
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
- tests/
-
-
-
diff --git a/src/ByteUnits/Binary.php b/src/ByteUnits/Binary.php
index df949c7..5aae0d6 100644
--- a/src/ByteUnits/Binary.php
+++ b/src/ByteUnits/Binary.php
@@ -31,7 +31,7 @@ public static function megabytes($numberOf)
* @param int $numberOf
* @return Binary
*/
- public static function gigabytes($numberOf)
+ public static function gigabytes(int $numberOf): Binary
{
return new self(self::scale()->scaleFromUnit($numberOf, 'GiB'));
}
@@ -40,7 +40,7 @@ public static function gigabytes($numberOf)
* @param int $numberOf
* @return Binary
*/
- public static function terabytes($numberOf)
+ public static function terabytes($numberOf): Binary
{
return new self(self::scale()->scaleFromUnit($numberOf, 'TiB'));
}
@@ -49,7 +49,7 @@ public static function terabytes($numberOf)
* @param int $numberOf
* @return Binary
*/
- public static function petabytes($numberOf)
+ public static function petabytes($numberOf): Binary
{
return new self(self::scale()->scaleFromUnit($numberOf, 'PiB'));
}
@@ -58,7 +58,7 @@ public static function petabytes($numberOf)
* @param int $numberOf
* @return Binary
*/
- public static function exabytes($numberOf)
+ public static function exabytes($numberOf): Binary
{
return new self(self::scale()->scaleFromUnit($numberOf, 'EiB'));
}
@@ -67,7 +67,7 @@ public static function exabytes($numberOf)
* @param int $numberOf
* @return Binary
*/
- public function __construct($numberOfBytes, $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION)
+ public function __construct(int $numberOfBytes, int $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION)
{
parent::__construct($numberOfBytes, new Formatter(self::scale(), $formatWithPrecision));
}
@@ -76,7 +76,7 @@ public function __construct($numberOfBytes, $formatWithPrecision = self::DEFAULT
/**
* @return PowerScale
*/
- public static function scale()
+ public static function scale(): PowerScale
{
return self::$scale = self::$scale ?: new PowerScale(self::$base, self::$suffixes, self::COMPUTE_WITH_PRECISION);
}
diff --git a/src/ByteUnits/Metric.php b/src/ByteUnits/Metric.php
index 9cb9894..50f0692 100644
--- a/src/ByteUnits/Metric.php
+++ b/src/ByteUnits/Metric.php
@@ -4,8 +4,8 @@
class Metric extends System
{
- private static $base = 1000;
- private static $suffixes = ['YB'=>8, 'ZB'=>7, 'EB'=>6, 'PB'=>5, 'TB'=>4, 'GB'=>3, 'MB'=>2, 'kB'=>1, 'B'=>0];
+ private static int $base = 1000;
+ private static array $suffixes = ['YB'=>8, 'ZB'=>7, 'EB'=>6, 'PB'=>5, 'TB'=>4, 'GB'=>3, 'MB'=>2, 'kB'=>1, 'B'=>0];
private static $scale;
private static $parser;
@@ -68,18 +68,12 @@ public static function exabytes($numberOf)
return new self(self::scale()->scaleFromUnit($numberOf, 'EB'));
}
- /**
- * @return PowerScale
- */
- public static function scale()
+ public static function scale(): PowerScale
{
return self::$scale = self::$scale ?: new PowerScale(self::$base, self::$suffixes, self::COMPUTE_WITH_PRECISION);
}
- /**
- * @return Parser
- */
- public static function parser()
+ public static function parser(): Parser
{
return self::$parser = self::$parser ?: new Parser(self::scale(), __CLASS__);
}
diff --git a/src/ByteUnits/Parser.php b/src/ByteUnits/Parser.php
index 1c0bb34..1b7f484 100644
--- a/src/ByteUnits/Parser.php
+++ b/src/ByteUnits/Parser.php
@@ -7,9 +7,14 @@
class Parser
{
private $scale;
- private $system;
+ private readonly ReflectionClass $system;
- public function __construct($scale, $system)
+ /**
+ * @param $scale
+ * @param class-string $system
+ * @throws \ReflectionException
+ */
+ public function __construct($scale, string $system)
{
$this->scale = $scale;
$this->system = new ReflectionClass($system);
@@ -19,8 +24,9 @@ public function __construct($scale, $system)
* @param string $quantityWithUnit
* @return System
* @throws ParseException
+ * @throws \ReflectionException
*/
- public function parse($quantityWithUnit)
+ public function parse(string $quantityWithUnit): System
{
if (preg_match('/(?P[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\W*(?P.*)/', $quantityWithUnit, $matches)) {
$quantity = $matches['quantity'];
diff --git a/src/ByteUnits/System.php b/src/ByteUnits/System.php
index dd9aa93..52955ca 100644
--- a/src/ByteUnits/System.php
+++ b/src/ByteUnits/System.php
@@ -4,42 +4,37 @@
abstract class System
{
- const DEFAULT_FORMAT_PRECISION = 2;
- const COMPUTE_WITH_PRECISION = 10;
+ protected const int DEFAULT_FORMAT_PRECISION = 2;
+ protected const int COMPUTE_WITH_PRECISION = 10;
protected $formatter;
- protected $numberOfBytes;
+ protected string|int $numberOfBytes;
- /**
- * @param int|string $numberOf
- * @param int $formatWithPrecision
- * @return System
- */
- public static function bytes($numberOf, $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION)
+ public static function bytes(int|string $numberOf, int $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION): static
{
return new static($numberOf, $formatWithPrecision);
}
- /**
- * @param string $bytesAsString
- * @return System
- */
- public static function parse($bytesAsString)
+ public static function parse(string $bytesAsString): static
{
return static::parser()->parse($bytesAsString);
}
+ /**
+ * @throws NegativeBytesException
+ */
public function __construct($numberOfBytes, $formatter)
{
- $this->formatter = $formatter;
$this->numberOfBytes = $this->ensureIsNotNegative($this->normalize($numberOfBytes));
+ $this->formatter = $formatter;
}
/**
- * @param System $another
- * @return System
+ * @throws ConversionException
+ * @throws NegativeBytesException
+ * @throws \Exception
*/
- public function add($another)
+ public function add(mixed $another): static
{
return new static(
bcadd($this->numberOfBytes, box($another)->numberOfBytes, self::COMPUTE_WITH_PRECISION),
@@ -48,10 +43,11 @@ public function add($another)
}
/**
- * @param System $another
- * @return System
+ * @throws ConversionException
+ * @throws NegativeBytesException
+ * @throws \Exception
*/
- public function remove($another)
+ public function remove(mixed $another): static
{
return new static(
bcsub($this->numberOfBytes, box($another)->numberOfBytes, self::COMPUTE_WITH_PRECISION),
@@ -60,10 +56,22 @@ public function remove($another)
}
/**
- * @param System $another
- * @return bool
+ * @throws ConversionException
+ * @throws \Exception
*/
- public function isEqualTo($another)
+ public function equals(System $another): bool
+ {
+ if ($this::class !== $another::class) {
+ return false;
+ }
+
+ return self::compare($this, $another) === 0;
+ }
+
+ /**
+ * @throws \Exception
+ */
+ public function isEqualTo(mixed $another): bool
{
return self::compare($this, box($another)) === 0;
}
@@ -71,8 +79,9 @@ public function isEqualTo($another)
/**
* @param System $another
* @return bool
+ * @throws ConversionException
*/
- public function isGreaterThanOrEqualTo($another)
+ public function isGreaterThanOrEqualTo(mixed $another): bool
{
return self::compare($this, box($another)) >= 0;
}
@@ -81,7 +90,7 @@ public function isGreaterThanOrEqualTo($another)
* @param System $another
* @return bool
*/
- public function isGreaterThan($another)
+ public function isGreaterThan(mixed $another)
{
return self::compare($this, box($another)) > 0;
}
@@ -96,20 +105,14 @@ public function isLessThanOrEqualTo($another)
}
/**
- * @param System $another
- * @return bool
+ * @throws \Exception
*/
- public function isLessThan($another)
+ public function isLessThan(mixed $another): bool
{
return self::compare($this, box($another)) < 0;
}
- /**
- * @param System $left
- * @param System $right
- * @return int
- */
- public static function compare($left, $right)
+ public static function compare(System $left, System $right): int
{
return bccomp(
$left->numberOfBytes,
@@ -128,10 +131,7 @@ public function format($howToFormat = null, $separator = '')
return $this->formatter->format($this->numberOfBytes, $howToFormat, $separator);
}
- /**
- * @return System
- */
- public function asBinary()
+ public function asBinary(): Binary
{
return Binary::bytes($this->numberOfBytes);
}
@@ -144,11 +144,7 @@ public function asMetric()
return Metric::bytes($this->numberOfBytes);
}
- /**
- * @param string $numberOfBytes
- * @return int
- */
- private function normalize($numberOfBytes)
+ private function normalize(string $numberOfBytes): string
{
$numberOfBytes = (string) $numberOfBytes;
if (preg_match('/^(?P\d+(?:\.\d+))E\+(?P\d+)$/', $numberOfBytes, $matches)) {
@@ -161,11 +157,9 @@ private function normalize($numberOfBytes)
}
/**
- * @param int|string $numberOfBytes
- * @return int|string
* @throws NegativeBytesException
*/
- private function ensureIsNotNegative($numberOfBytes)
+ private function ensureIsNotNegative(int|string $numberOfBytes): int|string
{
if (bccomp($numberOfBytes, 0) < 0) {
throw new NegativeBytesException();
@@ -173,10 +167,7 @@ private function ensureIsNotNegative($numberOfBytes)
return $numberOfBytes;
}
- /**
- * @return int|string
- */
- public function numberOfBytes()
+ public function numberOfBytes(): int|string
{
return $this->numberOfBytes;
}
diff --git a/src/ByteUnits/functions.php b/src/ByteUnits/functions.php
index d876d49..e3a873f 100644
--- a/src/ByteUnits/functions.php
+++ b/src/ByteUnits/functions.php
@@ -4,7 +4,11 @@
use Exception;
-function box($something)
+/**
+ * @throws ConversionException
+ * @throws Exception
+ */
+function box(mixed $something): System
{
if (is_integer($something)) {
return bytes($something);
@@ -12,30 +16,32 @@ function box($something)
if (is_string($something)) {
return parse($something);
}
- if (is_object($something) && ($something instanceof System)) {
+ if ($something instanceof System) {
return $something;
}
throw new ConversionException();
}
-function bytes($numberOf)
+/**
+ * @throws NegativeBytesException
+ */
+function bytes($numberOf): Metric
{
return new Metric($numberOf);
}
/**
- * @param $bytesAsString
- * @return System
- * @throws Exception
+ * @throws ParseException
+ * @throws \ReflectionException
*/
-function parse($bytesAsString)
+function parse(string $bytesAsString): System
{
- $lastParseException = null;
$parsers = [Metric::parser(), Binary::parser()];
+ /** @var Parser $parser */
foreach ($parsers as $parser) {
try {
return $parser->parse($bytesAsString);
- } catch (\Exception $e) {
+ } catch (\ReflectionException|ParseException $e) {
$lastParseException = $e;
}
}
diff --git a/tests/ByteUnits/ArithmeticTest.php b/tests/ByteUnits/ArithmeticTest.php
index 2334493..f845d6f 100644
--- a/tests/ByteUnits/ArithmeticTest.php
+++ b/tests/ByteUnits/ArithmeticTest.php
@@ -2,40 +2,51 @@
namespace ByteUnits;
-class ArithmeticTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class ArithmeticTest extends TestCase
{
public function testAddInSameUnitSystem()
{
- $this->assertEquals(Metric::bytes(10), Metric::bytes(5)->add(Metric::bytes(5)));
- $this->assertEquals(Binary::bytes(10), Binary::bytes(5)->add(Binary::bytes(5)));
+ $this->assertObjectEquals(Metric::bytes(10), Metric::bytes(5)->add(Metric::bytes(5)));
+ $this->assertObjectEquals(Binary::bytes(10), Binary::bytes(5)->add(Binary::bytes(5)));
}
+ /**
+ * @throws ConversionException
+ * @throws NegativeBytesException
+ * @throws \Exception
+ */
public function testRemoveInSameUnitSystem()
{
- $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove(Metric::bytes(2)));
- $this->assertEquals(Binary::bytes(3), Binary::bytes(5)->remove(Binary::bytes(2)));
+ $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove(Metric::bytes(2)));
+ $this->assertObjectEquals(Binary::bytes(3), Binary::bytes(5)->remove(Binary::bytes(2)));
}
+ /**
+ * @throws \Exception
+ */
public function testAutoboxing()
{
- $this->assertEquals(Metric::bytes(10), Metric::bytes(5)->add(5));
- $this->assertEquals(Metric::bytes(10), Metric::bytes(5)->add('5B'));
- $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove(2));
- $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove('2B'));
+ $this->assertObjectEquals(Metric::bytes(10), Metric::bytes(5)->add(5));
+ $this->assertObjectEquals(Metric::bytes(10), Metric::bytes(5)->add('5B'));
+ $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove(2));
+ $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove('2B'));
}
- /**
- * @expectedException ByteUnits\NegativeBytesException
- */
public function testCannotRemoveMoreBytesThanYouHave()
{
+ $this->expectException(NegativeBytesException::class);
Metric::bytes(5)->remove(Metric::bytes(10));
}
+ /**
+ * @throws \Exception
+ */
public function testPreserveSystemUnitOfReceiver()
{
- $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
- $this->assertNotEquals(Binary::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
+ $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
+ $this->assertObjectNotEquals(Binary::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
}
public function testPreserveFormatPrecisionOfReceiver()
diff --git a/tests/ByteUnits/BinarySystemTest.php b/tests/ByteUnits/BinarySystemTest.php
index 2c4ef64..95ce9b8 100644
--- a/tests/ByteUnits/BinarySystemTest.php
+++ b/tests/ByteUnits/BinarySystemTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class BinarySystemTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class BinarySystemTest extends TestCase
{
public function testKilobytesConstructor()
{
@@ -34,11 +36,9 @@ public function testExabytesConstructor()
$this->assertEquals(Binary::bytes(1152921504606846976), Binary::exabytes(1));
}
- /**
- * @expectedException ByteUnits\NegativeBytesException
- */
public function testCannotBeNegative()
{
+ $this->expectException(NegativeBytesException::class);
Binary::bytes(-1);
}
}
diff --git a/tests/ByteUnits/BoxingTest.php b/tests/ByteUnits/BoxingTest.php
index 87b6070..5bdcf0d 100644
--- a/tests/ByteUnits/BoxingTest.php
+++ b/tests/ByteUnits/BoxingTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class BoxingTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class BoxingTest extends TestCase
{
public function testBoxAnInteger()
{
@@ -23,10 +25,11 @@ public function testBoxAByteUnit()
}
/**
- * @expectedException ByteUnits\ConversionException
+ * @throws \Exception
*/
public function testBoxAnObjectThatIsNotAByteUnit()
{
+ $this->expectException(ConversionException::class);
box(new \StdClass());
}
}
diff --git a/tests/ByteUnits/CompareTest.php b/tests/ByteUnits/CompareTest.php
index fb3a13e..0512116 100644
--- a/tests/ByteUnits/CompareTest.php
+++ b/tests/ByteUnits/CompareTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class CompareTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class CompareTest extends TestCase
{
public function testCompareWithSameUnitSystem()
{
diff --git a/tests/ByteUnits/ConversionBetweenSystemsTest.php b/tests/ByteUnits/ConversionBetweenSystemsTest.php
index b6aaea0..d2ac6ef 100644
--- a/tests/ByteUnits/ConversionBetweenSystemsTest.php
+++ b/tests/ByteUnits/ConversionBetweenSystemsTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class ConversionBetweenSystemsTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class ConversionBetweenSystemsTest extends TestCase
{
public function testBytesAreInMetricStystem()
{
diff --git a/tests/ByteUnits/FormatInBinarySystemTest.php b/tests/ByteUnits/FormatInBinarySystemTest.php
index 18147d0..a674430 100644
--- a/tests/ByteUnits/FormatInBinarySystemTest.php
+++ b/tests/ByteUnits/FormatInBinarySystemTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class FormatInBinarySystemTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class FormatInBinarySystemTest extends TestCase
{
public function testBytesNamedConstructor()
{
diff --git a/tests/ByteUnits/FormatInMetricSystemTest.php b/tests/ByteUnits/FormatInMetricSystemTest.php
index 83aede7..29d3676 100644
--- a/tests/ByteUnits/FormatInMetricSystemTest.php
+++ b/tests/ByteUnits/FormatInMetricSystemTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class FormatInMetricSystemTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class FormatInMetricSystemTest extends TestCase
{
public function testBytesNamedConstructor()
{
diff --git a/tests/ByteUnits/MetricSystemTest.php b/tests/ByteUnits/MetricSystemTest.php
index 36677a4..eb7e0ae 100644
--- a/tests/ByteUnits/MetricSystemTest.php
+++ b/tests/ByteUnits/MetricSystemTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class MetricSystemTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class MetricSystemTest extends TestCase
{
public function testKilobytesConstructor()
{
@@ -34,11 +36,9 @@ public function testExabytesConstructor()
$this->assertEquals(Metric::bytes(1000000000000000000), Metric::exabytes(1));
}
- /**
- * @expectedException ByteUnits\NegativeBytesException
- */
public function testCannotBeNegative()
{
+ $this->expectException(NegativeBytesException::class);
Metric::bytes(-1);
}
}
diff --git a/tests/ByteUnits/ParseTest.php b/tests/ByteUnits/ParseTest.php
index 3deb8cc..37c57f1 100644
--- a/tests/ByteUnits/ParseTest.php
+++ b/tests/ByteUnits/ParseTest.php
@@ -2,7 +2,9 @@
namespace ByteUnits;
-class ParseTest extends \PHPUnit_Framework_TestCase
+use PHPUnit\Framework\TestCase;
+
+class ParseTest extends TestCase
{
public function testParseInMetricSystem()
{
@@ -10,17 +12,26 @@ public function testParseInMetricSystem()
$this->assertEquals(Metric::bytes(1250000), parse('1.25MB'));
}
+ /**
+ * @throws \ReflectionException
+ */
public function testParseInBinarySystem()
{
$this->assertEquals(Binary::bytes(1024), parse('1.00KiB'));
$this->assertEquals(Binary::bytes(1310720), parse('1.25MiB'));
}
+ /**
+ * @throws \ReflectionException
+ */
public function testParseWithoutUnit()
{
$this->assertEquals(Metric::bytes(1000), parse('1000'));
}
+ /**
+ * @throws \ReflectionException
+ */
public function testParseWithSeparator()
{
$this->assertEquals(Metric::bytes(1000), parse('1.00 kB'));
@@ -29,26 +40,23 @@ public function testParseWithSeparator()
}
/**
- * @expectedException ByteUnits\ParseException
+ * @throws \ReflectionException
*/
public function testInvalidByteFormat()
{
+ $this->expectException(ParseException::class);
parse('Not a valid byte format');
}
- /**
- * @expectedException ByteUnits\ParseException
- */
public function testInvalidByteFormatForBinarySystem()
{
+ $this->expectException(ParseException::class);
Binary::parse('1.00kB');
}
- /**
- * @expectedException ByteUnits\ParseException
- */
public function testInvalidByteFormatForMetricSystem()
{
+ $this->expectException(ParseException::class);
Metric::parse('1.00KiB');
}
}
From dea2aa58dc81cbf77248d78672ed7bef73af6ec4 Mon Sep 17 00:00:00 2001
From: Davide Bellettini <325358+dbellettini@users.noreply.github.com>
Date: Sun, 10 Aug 2025 13:58:27 +0200
Subject: [PATCH 3/6] Fix CI Actions
---
.github/workflows/ci.yml | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 2aaf4d6..7a11f56 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -10,17 +10,16 @@ permissions:
contents: read
jobs:
- strategy:
- matrix:
- php_version:
- - 8.3
- - 8.4
build:
+ strategy:
+ matrix:
+ php_version:
+ - 8.3
+ - 8.4
runs-on: ubuntu-latest
-
env:
COMPOSE_FILE: compose.yaml
- PHP_VERSION=${{ matrix.php_version }}
+ PHP_VERSION: ${{ matrix.php_version }}
steps:
- uses: actions/checkout@v4
From b910d2c0563dd53287951c1d19b75b4a22c174c9 Mon Sep 17 00:00:00 2001
From: Davide Bellettini <325358+dbellettini@users.noreply.github.com>
Date: Sun, 10 Aug 2025 14:00:15 +0200
Subject: [PATCH 4/6] Fix errors in composer validation
---
composer.json | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/composer.json b/composer.json
index 864ad44..b946356 100644
--- a/composer.json
+++ b/composer.json
@@ -1,9 +1,8 @@
{
- "name": "gabrielelana/byte-units",
+ "name": "recruiterphp/byte-units",
"description": "Library to parse, format and convert byte units",
"license": "MIT",
"type": "library",
- "version": "0.5.0",
"keywords": [
"byte",
"units",
@@ -44,7 +43,7 @@
"ergebnis/composer-normalize": true
}
},
- "replaces": {
+ "replace": {
"gabrielelana/byte-units": "self.version"
}
}
From 638a77e9aa35379edec184b1c282a8da0f347fc6 Mon Sep 17 00:00:00 2001
From: Davide Bellettini <325358+dbellettini@users.noreply.github.com>
Date: Sun, 10 Aug 2025 14:02:29 +0200
Subject: [PATCH 5/6] Normalize composer.json
---
composer.json | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/composer.json b/composer.json
index b946356..1f9e111 100644
--- a/composer.json
+++ b/composer.json
@@ -30,6 +30,9 @@
"ergebnis/composer-normalize": "^2.47",
"phpunit/phpunit": "^12.3"
},
+ "replace": {
+ "gabrielelana/byte-units": "self.version"
+ },
"autoload": {
"psr-4": {
"ByteUnits\\": "src/ByteUnits"
@@ -42,8 +45,5 @@
"allow-plugins": {
"ergebnis/composer-normalize": true
}
- },
- "replace": {
- "gabrielelana/byte-units": "self.version"
}
}
From 6b1cc92391a3ab8f52a59b4d7c4fe07cc5a4da0a Mon Sep 17 00:00:00 2001
From: Davide Bellettini <325358+dbellettini@users.noreply.github.com>
Date: Sun, 10 Aug 2025 14:15:03 +0200
Subject: [PATCH 6/6] Restore code from master with minimal changes
---
src/ByteUnits/Binary.php | 12 +--
src/ByteUnits/ConversionException.php | 4 +-
src/ByteUnits/Metric.php | 14 +++-
src/ByteUnits/NegativeBytesException.php | 4 +-
src/ByteUnits/ParseException.php | 4 +-
src/ByteUnits/Parser.php | 12 +--
src/ByteUnits/System.php | 93 +++++++++++++-----------
src/ByteUnits/functions.php | 24 +++---
tests/ByteUnits/ArithmeticTest.php | 31 +++-----
tests/ByteUnits/BoxingTest.php | 3 -
tests/ByteUnits/ParseTest.php | 12 ---
11 files changed, 98 insertions(+), 115 deletions(-)
diff --git a/src/ByteUnits/Binary.php b/src/ByteUnits/Binary.php
index 5aae0d6..df949c7 100644
--- a/src/ByteUnits/Binary.php
+++ b/src/ByteUnits/Binary.php
@@ -31,7 +31,7 @@ public static function megabytes($numberOf)
* @param int $numberOf
* @return Binary
*/
- public static function gigabytes(int $numberOf): Binary
+ public static function gigabytes($numberOf)
{
return new self(self::scale()->scaleFromUnit($numberOf, 'GiB'));
}
@@ -40,7 +40,7 @@ public static function gigabytes(int $numberOf): Binary
* @param int $numberOf
* @return Binary
*/
- public static function terabytes($numberOf): Binary
+ public static function terabytes($numberOf)
{
return new self(self::scale()->scaleFromUnit($numberOf, 'TiB'));
}
@@ -49,7 +49,7 @@ public static function terabytes($numberOf): Binary
* @param int $numberOf
* @return Binary
*/
- public static function petabytes($numberOf): Binary
+ public static function petabytes($numberOf)
{
return new self(self::scale()->scaleFromUnit($numberOf, 'PiB'));
}
@@ -58,7 +58,7 @@ public static function petabytes($numberOf): Binary
* @param int $numberOf
* @return Binary
*/
- public static function exabytes($numberOf): Binary
+ public static function exabytes($numberOf)
{
return new self(self::scale()->scaleFromUnit($numberOf, 'EiB'));
}
@@ -67,7 +67,7 @@ public static function exabytes($numberOf): Binary
* @param int $numberOf
* @return Binary
*/
- public function __construct(int $numberOfBytes, int $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION)
+ public function __construct($numberOfBytes, $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION)
{
parent::__construct($numberOfBytes, new Formatter(self::scale(), $formatWithPrecision));
}
@@ -76,7 +76,7 @@ public function __construct(int $numberOfBytes, int $formatWithPrecision = self:
/**
* @return PowerScale
*/
- public static function scale(): PowerScale
+ public static function scale()
{
return self::$scale = self::$scale ?: new PowerScale(self::$base, self::$suffixes, self::COMPUTE_WITH_PRECISION);
}
diff --git a/src/ByteUnits/ConversionException.php b/src/ByteUnits/ConversionException.php
index b4dfa3c..06bac64 100644
--- a/src/ByteUnits/ConversionException.php
+++ b/src/ByteUnits/ConversionException.php
@@ -2,6 +2,8 @@
namespace ByteUnits;
-class ConversionException extends \InvalidArgumentException
+use Exception;
+
+class ConversionException extends Exception
{
}
diff --git a/src/ByteUnits/Metric.php b/src/ByteUnits/Metric.php
index 50f0692..9cb9894 100644
--- a/src/ByteUnits/Metric.php
+++ b/src/ByteUnits/Metric.php
@@ -4,8 +4,8 @@
class Metric extends System
{
- private static int $base = 1000;
- private static array $suffixes = ['YB'=>8, 'ZB'=>7, 'EB'=>6, 'PB'=>5, 'TB'=>4, 'GB'=>3, 'MB'=>2, 'kB'=>1, 'B'=>0];
+ private static $base = 1000;
+ private static $suffixes = ['YB'=>8, 'ZB'=>7, 'EB'=>6, 'PB'=>5, 'TB'=>4, 'GB'=>3, 'MB'=>2, 'kB'=>1, 'B'=>0];
private static $scale;
private static $parser;
@@ -68,12 +68,18 @@ public static function exabytes($numberOf)
return new self(self::scale()->scaleFromUnit($numberOf, 'EB'));
}
- public static function scale(): PowerScale
+ /**
+ * @return PowerScale
+ */
+ public static function scale()
{
return self::$scale = self::$scale ?: new PowerScale(self::$base, self::$suffixes, self::COMPUTE_WITH_PRECISION);
}
- public static function parser(): Parser
+ /**
+ * @return Parser
+ */
+ public static function parser()
{
return self::$parser = self::$parser ?: new Parser(self::scale(), __CLASS__);
}
diff --git a/src/ByteUnits/NegativeBytesException.php b/src/ByteUnits/NegativeBytesException.php
index 37622a3..6599b95 100644
--- a/src/ByteUnits/NegativeBytesException.php
+++ b/src/ByteUnits/NegativeBytesException.php
@@ -2,6 +2,8 @@
namespace ByteUnits;
-class NegativeBytesException extends \DomainException
+use Exception;
+
+class NegativeBytesException extends Exception
{
}
diff --git a/src/ByteUnits/ParseException.php b/src/ByteUnits/ParseException.php
index 3e23a28..dea9c6c 100644
--- a/src/ByteUnits/ParseException.php
+++ b/src/ByteUnits/ParseException.php
@@ -2,6 +2,8 @@
namespace ByteUnits;
-class ParseException extends \InvalidArgumentException
+use Exception;
+
+class ParseException extends Exception
{
}
diff --git a/src/ByteUnits/Parser.php b/src/ByteUnits/Parser.php
index 1b7f484..1c0bb34 100644
--- a/src/ByteUnits/Parser.php
+++ b/src/ByteUnits/Parser.php
@@ -7,14 +7,9 @@
class Parser
{
private $scale;
- private readonly ReflectionClass $system;
+ private $system;
- /**
- * @param $scale
- * @param class-string $system
- * @throws \ReflectionException
- */
- public function __construct($scale, string $system)
+ public function __construct($scale, $system)
{
$this->scale = $scale;
$this->system = new ReflectionClass($system);
@@ -24,9 +19,8 @@ public function __construct($scale, string $system)
* @param string $quantityWithUnit
* @return System
* @throws ParseException
- * @throws \ReflectionException
*/
- public function parse(string $quantityWithUnit): System
+ public function parse($quantityWithUnit)
{
if (preg_match('/(?P[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\W*(?P.*)/', $quantityWithUnit, $matches)) {
$quantity = $matches['quantity'];
diff --git a/src/ByteUnits/System.php b/src/ByteUnits/System.php
index 52955ca..dd9aa93 100644
--- a/src/ByteUnits/System.php
+++ b/src/ByteUnits/System.php
@@ -4,37 +4,42 @@
abstract class System
{
- protected const int DEFAULT_FORMAT_PRECISION = 2;
- protected const int COMPUTE_WITH_PRECISION = 10;
+ const DEFAULT_FORMAT_PRECISION = 2;
+ const COMPUTE_WITH_PRECISION = 10;
protected $formatter;
- protected string|int $numberOfBytes;
+ protected $numberOfBytes;
- public static function bytes(int|string $numberOf, int $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION): static
+ /**
+ * @param int|string $numberOf
+ * @param int $formatWithPrecision
+ * @return System
+ */
+ public static function bytes($numberOf, $formatWithPrecision = self::DEFAULT_FORMAT_PRECISION)
{
return new static($numberOf, $formatWithPrecision);
}
- public static function parse(string $bytesAsString): static
+ /**
+ * @param string $bytesAsString
+ * @return System
+ */
+ public static function parse($bytesAsString)
{
return static::parser()->parse($bytesAsString);
}
- /**
- * @throws NegativeBytesException
- */
public function __construct($numberOfBytes, $formatter)
{
- $this->numberOfBytes = $this->ensureIsNotNegative($this->normalize($numberOfBytes));
$this->formatter = $formatter;
+ $this->numberOfBytes = $this->ensureIsNotNegative($this->normalize($numberOfBytes));
}
/**
- * @throws ConversionException
- * @throws NegativeBytesException
- * @throws \Exception
+ * @param System $another
+ * @return System
*/
- public function add(mixed $another): static
+ public function add($another)
{
return new static(
bcadd($this->numberOfBytes, box($another)->numberOfBytes, self::COMPUTE_WITH_PRECISION),
@@ -43,11 +48,10 @@ public function add(mixed $another): static
}
/**
- * @throws ConversionException
- * @throws NegativeBytesException
- * @throws \Exception
+ * @param System $another
+ * @return System
*/
- public function remove(mixed $another): static
+ public function remove($another)
{
return new static(
bcsub($this->numberOfBytes, box($another)->numberOfBytes, self::COMPUTE_WITH_PRECISION),
@@ -56,22 +60,10 @@ public function remove(mixed $another): static
}
/**
- * @throws ConversionException
- * @throws \Exception
- */
- public function equals(System $another): bool
- {
- if ($this::class !== $another::class) {
- return false;
- }
-
- return self::compare($this, $another) === 0;
- }
-
- /**
- * @throws \Exception
+ * @param System $another
+ * @return bool
*/
- public function isEqualTo(mixed $another): bool
+ public function isEqualTo($another)
{
return self::compare($this, box($another)) === 0;
}
@@ -79,9 +71,8 @@ public function isEqualTo(mixed $another): bool
/**
* @param System $another
* @return bool
- * @throws ConversionException
*/
- public function isGreaterThanOrEqualTo(mixed $another): bool
+ public function isGreaterThanOrEqualTo($another)
{
return self::compare($this, box($another)) >= 0;
}
@@ -90,7 +81,7 @@ public function isGreaterThanOrEqualTo(mixed $another): bool
* @param System $another
* @return bool
*/
- public function isGreaterThan(mixed $another)
+ public function isGreaterThan($another)
{
return self::compare($this, box($another)) > 0;
}
@@ -105,14 +96,20 @@ public function isLessThanOrEqualTo($another)
}
/**
- * @throws \Exception
+ * @param System $another
+ * @return bool
*/
- public function isLessThan(mixed $another): bool
+ public function isLessThan($another)
{
return self::compare($this, box($another)) < 0;
}
- public static function compare(System $left, System $right): int
+ /**
+ * @param System $left
+ * @param System $right
+ * @return int
+ */
+ public static function compare($left, $right)
{
return bccomp(
$left->numberOfBytes,
@@ -131,7 +128,10 @@ public function format($howToFormat = null, $separator = '')
return $this->formatter->format($this->numberOfBytes, $howToFormat, $separator);
}
- public function asBinary(): Binary
+ /**
+ * @return System
+ */
+ public function asBinary()
{
return Binary::bytes($this->numberOfBytes);
}
@@ -144,7 +144,11 @@ public function asMetric()
return Metric::bytes($this->numberOfBytes);
}
- private function normalize(string $numberOfBytes): string
+ /**
+ * @param string $numberOfBytes
+ * @return int
+ */
+ private function normalize($numberOfBytes)
{
$numberOfBytes = (string) $numberOfBytes;
if (preg_match('/^(?P\d+(?:\.\d+))E\+(?P\d+)$/', $numberOfBytes, $matches)) {
@@ -157,9 +161,11 @@ private function normalize(string $numberOfBytes): string
}
/**
+ * @param int|string $numberOfBytes
+ * @return int|string
* @throws NegativeBytesException
*/
- private function ensureIsNotNegative(int|string $numberOfBytes): int|string
+ private function ensureIsNotNegative($numberOfBytes)
{
if (bccomp($numberOfBytes, 0) < 0) {
throw new NegativeBytesException();
@@ -167,7 +173,10 @@ private function ensureIsNotNegative(int|string $numberOfBytes): int|string
return $numberOfBytes;
}
- public function numberOfBytes(): int|string
+ /**
+ * @return int|string
+ */
+ public function numberOfBytes()
{
return $this->numberOfBytes;
}
diff --git a/src/ByteUnits/functions.php b/src/ByteUnits/functions.php
index e3a873f..d876d49 100644
--- a/src/ByteUnits/functions.php
+++ b/src/ByteUnits/functions.php
@@ -4,11 +4,7 @@
use Exception;
-/**
- * @throws ConversionException
- * @throws Exception
- */
-function box(mixed $something): System
+function box($something)
{
if (is_integer($something)) {
return bytes($something);
@@ -16,32 +12,30 @@ function box(mixed $something): System
if (is_string($something)) {
return parse($something);
}
- if ($something instanceof System) {
+ if (is_object($something) && ($something instanceof System)) {
return $something;
}
throw new ConversionException();
}
-/**
- * @throws NegativeBytesException
- */
-function bytes($numberOf): Metric
+function bytes($numberOf)
{
return new Metric($numberOf);
}
/**
- * @throws ParseException
- * @throws \ReflectionException
+ * @param $bytesAsString
+ * @return System
+ * @throws Exception
*/
-function parse(string $bytesAsString): System
+function parse($bytesAsString)
{
+ $lastParseException = null;
$parsers = [Metric::parser(), Binary::parser()];
- /** @var Parser $parser */
foreach ($parsers as $parser) {
try {
return $parser->parse($bytesAsString);
- } catch (\ReflectionException|ParseException $e) {
+ } catch (\Exception $e) {
$lastParseException = $e;
}
}
diff --git a/tests/ByteUnits/ArithmeticTest.php b/tests/ByteUnits/ArithmeticTest.php
index f845d6f..20876b2 100644
--- a/tests/ByteUnits/ArithmeticTest.php
+++ b/tests/ByteUnits/ArithmeticTest.php
@@ -8,30 +8,22 @@ class ArithmeticTest extends TestCase
{
public function testAddInSameUnitSystem()
{
- $this->assertObjectEquals(Metric::bytes(10), Metric::bytes(5)->add(Metric::bytes(5)));
- $this->assertObjectEquals(Binary::bytes(10), Binary::bytes(5)->add(Binary::bytes(5)));
+ $this->assertEquals(Metric::bytes(10), Metric::bytes(5)->add(Metric::bytes(5)));
+ $this->assertEquals(Binary::bytes(10), Binary::bytes(5)->add(Binary::bytes(5)));
}
- /**
- * @throws ConversionException
- * @throws NegativeBytesException
- * @throws \Exception
- */
public function testRemoveInSameUnitSystem()
{
- $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove(Metric::bytes(2)));
- $this->assertObjectEquals(Binary::bytes(3), Binary::bytes(5)->remove(Binary::bytes(2)));
+ $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove(Metric::bytes(2)));
+ $this->assertEquals(Binary::bytes(3), Binary::bytes(5)->remove(Binary::bytes(2)));
}
- /**
- * @throws \Exception
- */
public function testAutoboxing()
{
- $this->assertObjectEquals(Metric::bytes(10), Metric::bytes(5)->add(5));
- $this->assertObjectEquals(Metric::bytes(10), Metric::bytes(5)->add('5B'));
- $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove(2));
- $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove('2B'));
+ $this->assertEquals(Metric::bytes(10), Metric::bytes(5)->add(5));
+ $this->assertEquals(Metric::bytes(10), Metric::bytes(5)->add('5B'));
+ $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove(2));
+ $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove('2B'));
}
public function testCannotRemoveMoreBytesThanYouHave()
@@ -40,13 +32,10 @@ public function testCannotRemoveMoreBytesThanYouHave()
Metric::bytes(5)->remove(Metric::bytes(10));
}
- /**
- * @throws \Exception
- */
public function testPreserveSystemUnitOfReceiver()
{
- $this->assertObjectEquals(Metric::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
- $this->assertObjectNotEquals(Binary::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
+ $this->assertEquals(Metric::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
+ $this->assertNotEquals(Binary::bytes(3), Metric::bytes(5)->remove(Binary::bytes(2)));
}
public function testPreserveFormatPrecisionOfReceiver()
diff --git a/tests/ByteUnits/BoxingTest.php b/tests/ByteUnits/BoxingTest.php
index 5bdcf0d..6350a7d 100644
--- a/tests/ByteUnits/BoxingTest.php
+++ b/tests/ByteUnits/BoxingTest.php
@@ -24,9 +24,6 @@ public function testBoxAByteUnit()
$this->assertEquals($byteUnitInBinarySystem, box($byteUnitInBinarySystem));
}
- /**
- * @throws \Exception
- */
public function testBoxAnObjectThatIsNotAByteUnit()
{
$this->expectException(ConversionException::class);
diff --git a/tests/ByteUnits/ParseTest.php b/tests/ByteUnits/ParseTest.php
index 37c57f1..91cf7ec 100644
--- a/tests/ByteUnits/ParseTest.php
+++ b/tests/ByteUnits/ParseTest.php
@@ -12,26 +12,17 @@ public function testParseInMetricSystem()
$this->assertEquals(Metric::bytes(1250000), parse('1.25MB'));
}
- /**
- * @throws \ReflectionException
- */
public function testParseInBinarySystem()
{
$this->assertEquals(Binary::bytes(1024), parse('1.00KiB'));
$this->assertEquals(Binary::bytes(1310720), parse('1.25MiB'));
}
- /**
- * @throws \ReflectionException
- */
public function testParseWithoutUnit()
{
$this->assertEquals(Metric::bytes(1000), parse('1000'));
}
- /**
- * @throws \ReflectionException
- */
public function testParseWithSeparator()
{
$this->assertEquals(Metric::bytes(1000), parse('1.00 kB'));
@@ -39,9 +30,6 @@ public function testParseWithSeparator()
$this->assertEquals(Metric::bytes(1000), parse('1.00~~~kB'));
}
- /**
- * @throws \ReflectionException
- */
public function testInvalidByteFormat()
{
$this->expectException(ParseException::class);