From 88675b4f0d799f34133cbb5d1c37a2ceb26e40e8 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Fri, 22 Nov 2024 15:05:17 +0100 Subject: [PATCH 1/5] feat: add Tests for PHP 8.4, fix deprecations for PHP 8.4 --- .github/workflows/build.yml | 8 ++++---- src/CasesCollection.php | 4 ++-- src/Concerns/CollectsCases.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 316c572..777738d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [8.1, 8.2, 8.3] + php: [8.1, 8.2, 8.3, 8.4] dependency-version: [prefer-stable] os: [ubuntu-latest] @@ -19,7 +19,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -42,7 +42,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: fetch-depth: 0 @@ -71,7 +71,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/src/CasesCollection.php b/src/CasesCollection.php index 6d8e17c..c74686c 100644 --- a/src/CasesCollection.php +++ b/src/CasesCollection.php @@ -115,7 +115,7 @@ public function toArray(): array * @param (callable(TValue, array-key): bool)|null $callback * @return ?TValue */ - public function first(callable $callback = null): mixed + public function first(callable|null $callback = null): mixed { $callback ??= fn() => true; @@ -159,7 +159,7 @@ public function values(): array * @param (callable(TValue): array-key)|string|null $key * @return array */ - public function pluck(callable|string $value, callable|string $key = null): array + public function pluck(callable|string $value, callable|string|null $key = null): array { $result = []; diff --git a/src/Concerns/CollectsCases.php b/src/Concerns/CollectsCases.php index 2be87dd..8e1e9aa 100644 --- a/src/Concerns/CollectsCases.php +++ b/src/Concerns/CollectsCases.php @@ -32,7 +32,7 @@ public static function count(): int * * @param (callable(self, array-key): bool)|null $callback */ - public static function first(callable $callback = null): ?self + public static function first(callable|null $callback = null): ?self { return self::collect()->first($callback); } @@ -66,7 +66,7 @@ public static function values(): array * @param (callable(self): array-key)|string|null $key * @return array */ - public static function pluck(callable|string $value, callable|string $key = null): array + public static function pluck(callable|string $value, callable|string|null $key = null): array { return self::collect()->pluck($value, $key); } From fa8046c8bb776ab43c152e821a1337bab3fa086a Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Fri, 22 Nov 2024 17:22:14 +0100 Subject: [PATCH 2/5] Update nullables --- src/CasesCollection.php | 4 ++-- src/Concerns/CollectsCases.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CasesCollection.php b/src/CasesCollection.php index c74686c..a7a0e28 100644 --- a/src/CasesCollection.php +++ b/src/CasesCollection.php @@ -112,10 +112,10 @@ public function toArray(): array /** * Retrieve the first case. * - * @param (callable(TValue, array-key): bool)|null $callback + * @param ?callable(TValue, array-key): bool $callback * @return ?TValue */ - public function first(callable|null $callback = null): mixed + public function first(?callable $callback = null): mixed { $callback ??= fn() => true; diff --git a/src/Concerns/CollectsCases.php b/src/Concerns/CollectsCases.php index 8e1e9aa..9944313 100644 --- a/src/Concerns/CollectsCases.php +++ b/src/Concerns/CollectsCases.php @@ -30,9 +30,9 @@ public static function count(): int /** * Retrieve the first case. * - * @param (callable(self, array-key): bool)|null $callback + * @param ?callable(self, array-key): bool $callback */ - public static function first(callable|null $callback = null): ?self + public static function first(?callable $callback = null): ?self { return self::collect()->first($callback); } From e34ade94c603d59dc5527c2f6c0ded9f62ed4ac1 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Fri, 22 Nov 2024 17:26:34 +0100 Subject: [PATCH 3/5] Improve error message --- src/Concerns/SelfAware.php | 2 +- tests/BackedEnumTest.php | 4 ++-- tests/PureEnumTest.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Concerns/SelfAware.php b/src/Concerns/SelfAware.php index 2527f22..9953132 100644 --- a/src/Concerns/SelfAware.php +++ b/src/Concerns/SelfAware.php @@ -147,7 +147,7 @@ public function resolveMetaAttribute(string $meta): mixed } } - throw new ValueError(sprintf('"%s" is not a valid meta for enum "%s"', $meta, self::class)); + throw new ValueError(sprintf('The case %s::%s has no "%s" meta set', self::class, $this->name, $meta)); } /** diff --git a/tests/BackedEnumTest.php b/tests/BackedEnumTest.php index e880fe8..67e5b17 100644 --- a/tests/BackedEnumTest.php +++ b/tests/BackedEnumTest.php @@ -394,7 +394,7 @@ }); it('handles the call to an inaccessible case method', fn() => BackedEnum::one->unknownMethod()) - ->throws(Error::class, '"unknownMethod" is not a valid meta for enum "Cerbero\Enum\BackedEnum"'); + ->throws(Error::class, 'The case Cerbero\Enum\BackedEnum::one has no "unknownMethod" meta set'); it('runs custom logic when calling an inaccessible case method', function() { Enums::onCall(function(object $case, string $name, array $arguments) { @@ -450,7 +450,7 @@ ->toBe('red'); it('throws a value error when attempting to retrieve an invalid item', fn() => BackedEnum::one->resolveItem('invalid')) - ->throws(ValueError::class, '"invalid" is not a valid meta for enum "Cerbero\Enum\BackedEnum"'); + ->throws(ValueError::class, 'The case Cerbero\Enum\BackedEnum::one has no "invalid" meta set'); it('retrieves the value of a backed case or the name of a pure case', function() { expect(BackedEnum::one->value())->toBe(1); diff --git a/tests/PureEnumTest.php b/tests/PureEnumTest.php index f6b9d9c..0ecd1da 100644 --- a/tests/PureEnumTest.php +++ b/tests/PureEnumTest.php @@ -403,7 +403,7 @@ }); it('handles the call to an inaccessible case method', fn() => PureEnum::one->unknownMethod()) - ->throws(Error::class, '"unknownMethod" is not a valid meta for enum "Cerbero\Enum\PureEnum"'); + ->throws(Error::class, 'The case Cerbero\Enum\PureEnum::one has no "unknownMethod" meta set'); it('runs custom logic when calling an inaccessible case method', function() { Enums::onCall(function(object $case, string $name, array $arguments) { @@ -458,7 +458,7 @@ ->toBe('red'); it('throws a value error when attempting to retrieve an invalid item', fn() => PureEnum::one->resolveItem('invalid')) - ->throws(ValueError::class, '"invalid" is not a valid meta for enum "Cerbero\Enum\PureEnum"'); + ->throws(ValueError::class, 'The case Cerbero\Enum\PureEnum::one has no "invalid" meta set'); it('retrieves the value of a backed case or the name of a pure case', function() { expect(PureEnum::one->value())->toBe('one'); From 7ac6d3877e80df39286f6d1ee9b349d9408e521e Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Fri, 22 Nov 2024 17:26:51 +0100 Subject: [PATCH 4/5] Update readme --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 013a777..6369f32 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,12 @@ enum BackedEnum: int The above enum defines 3 meta for each case: `color`, `shape` and `isOdd`. The `#[Meta]` attributes are ideal to declare static information, whilst public non-static methods are ideal to declare dynamic information. +To access a case meta, we can simply call the method having the same name of the wanted meta: + +```php +BackedEnum::Two->color(); // green +``` + `#[Meta]` attributes can also be attached to the enum itself to provide default values when a case does not declare its own meta values: ```php From 49d65f4d59b818185009b0c646f4e94643eb4129 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Fri, 22 Nov 2024 17:43:38 +0100 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92fdbee..3dcb19d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,15 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi - Nothing +## 2.2.1 - 2024-11-22 + +### Added +- Full support for PHP 8.4 + +### Changed +- Improved error message for invalid meta + + ## 2.2.0 - 2024-11-19 ### Added