Skip to content

Commit

Permalink
Merge branch 'release/2.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Nov 22, 2024
2 parents 3d19256 + 49d65f4 commit 8ca4377
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 15 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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]

name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -42,7 +42,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/CasesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 $callback = null): mixed
public function first(?callable $callback = null): mixed
{
$callback ??= fn() => true;

Expand Down Expand Up @@ -159,7 +159,7 @@ public function values(): array
* @param (callable(TValue): array-key)|string|null $key
* @return array<array-key, TPluckValue>
*/
public function pluck(callable|string $value, callable|string $key = null): array
public function pluck(callable|string $value, callable|string|null $key = null): array
{
$result = [];

Expand Down
6 changes: 3 additions & 3 deletions src/Concerns/CollectsCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 $callback = null): ?self
public static function first(?callable $callback = null): ?self
{
return self::collect()->first($callback);
}
Expand Down Expand Up @@ -66,7 +66,7 @@ public static function values(): array
* @param (callable(self): array-key)|string|null $key
* @return array<array-key, TPluckValue>
*/
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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/SelfAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/BackedEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/PureEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 8ca4377

Please sign in to comment.