Skip to content

Commit 07d810b

Browse files
authored
refactor: use Str\parse to convert enums to string (#1589)
1 parent 873fae9 commit 07d810b

File tree

12 files changed

+37
-74
lines changed

12 files changed

+37
-74
lines changed

packages/auth/src/AccessControl/PolicyBasedAccessControl.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Tempest\Auth\AccessControl;
44

5-
use BackedEnum;
65
use Closure;
76
use Tempest\Auth\AuthConfig;
87
use Tempest\Auth\Authentication\Authenticator;
@@ -13,6 +12,7 @@
1312
use Tempest\Reflection\MethodReflector;
1413
use Tempest\Reflection\ParameterReflector;
1514
use Tempest\Support\Arr\ImmutableArray;
15+
use Tempest\Support\Str;
1616
use UnitEnum;
1717

1818
/**
@@ -82,11 +82,7 @@ private function findPoliciesForResourceAction(object|string $resource, UnitEnum
8282
? $resource::class
8383
: $resource;
8484

85-
$actionBeingEvaluated = match (true) {
86-
$action instanceof BackedEnum => $action->value,
87-
$action instanceof UnitEnum => $action->name,
88-
default => $action,
89-
};
85+
$actionBeingEvaluated = Str\parse($action);
9086

9187
return new ImmutableArray($this->authConfig->policies[$resource] ?? [])
9288
->filter(fn ($_, string $action) => $action === $actionBeingEvaluated)

packages/auth/src/AuthConfig.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use BackedEnum;
88
use Tempest\Auth\AccessControl\Policy;
99
use Tempest\Auth\Authentication\Authenticatable;
10-
use Tempest\Auth\Exceptions\PolicyMethodWasInvalid;
1110
use Tempest\Auth\Exceptions\PolicyWasInvalid;
1211
use Tempest\Reflection\MethodReflector;
1312
use Tempest\Support\Arr;
@@ -44,11 +43,7 @@ public function registerPolicy(MethodReflector $handler, Policy $policy): self
4443
}
4544

4645
foreach (Arr\wrap($policy->action) as $action) {
47-
$action = match (true) {
48-
$action instanceof BackedEnum => $action->value,
49-
$action instanceof UnitEnum => $action->name,
50-
default => $action,
51-
};
46+
$action = Str\parse($action);
5247

5348
$this->policies[$policy->resource][$action] ??= [];
5449
$this->policies[$policy->resource][$action][] = $handler;

packages/auth/src/Exceptions/OAuthWasNotConfigured.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
namespace Tempest\Auth\Exceptions;
66

77
use Exception;
8+
use Tempest\Support\Str;
89
use UnitEnum;
910

1011
final class OAuthWasNotConfigured extends Exception implements AuthenticationException
1112
{
1213
public static function configurationWasMissing(null|string|UnitEnum $tag): self
1314
{
14-
$tag = match (true) {
15-
is_string($tag) => $tag,
16-
$tag instanceof UnitEnum => $tag->name,
17-
default => null,
18-
};
15+
$tag = Str\parse($tag, default: null);
1916

2017
return new self(
2118
$tag

packages/cache/src/Testing/CacheTester.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
use Tempest\Clock\Clock;
88
use Tempest\Container\Container;
99
use Tempest\Container\GenericContainer;
10+
use Tempest\Support\Str;
1011
use UnitEnum;
1112

12-
use function Tempest\Support\Str\to_kebab_case;
13-
1413
final readonly class CacheTester
1514
{
1615
public function __construct(
@@ -23,11 +22,7 @@ public function __construct(
2322
public function fake(null|string|UnitEnum $tag = null): TestingCache
2423
{
2524
$cache = new TestingCache(
26-
tag: match (true) {
27-
is_string($tag) => to_kebab_case($tag),
28-
$tag instanceof UnitEnum => to_kebab_case($tag->name),
29-
default => 'default',
30-
},
25+
tag: Str\to_kebab_case(Str\parse($tag, default: 'default')),
3126
clock: $this->container->get(Clock::class)->toPsrClock(),
3227
);
3328

packages/database/src/Builder/QueryBuilders/HasConvenientWhereMethods.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,12 @@ protected function buildCondition(string $fieldDefinition, WhereOperator $operat
4747
throw new \InvalidArgumentException("{$operator->value} operator requires an array of values");
4848
}
4949

50-
$value = array_map(
51-
fn (mixed $value) => match (true) {
52-
$value instanceof BackedEnum => $value->value,
53-
$value instanceof UnitEnum => $value->name,
54-
$value instanceof ArrayAccess => (array) $value,
55-
default => $value,
56-
},
57-
$value,
58-
);
50+
$value = array_map(fn (mixed $value) => match (true) {
51+
$value instanceof BackedEnum => $value->value,
52+
$value instanceof UnitEnum => $value->name,
53+
$value instanceof ArrayAccess => (array) $value,
54+
default => $value,
55+
}, $value);
5956

6057
$placeholders = str_repeat('?,', times: count($value) - 1) . '?';
6158
$sql .= " {$operator->value} ({$placeholders})";

packages/event-bus/src/EventBusDiscovery.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
namespace Tempest\EventBus;
66

7-
use BackedEnum;
87
use Tempest\Discovery\Discovery;
98
use Tempest\Discovery\DiscoveryLocation;
109
use Tempest\Discovery\IsDiscovery;
1110
use Tempest\Reflection\ClassReflector;
1211
use Tempest\Reflection\TypeReflector;
13-
use UnitEnum;
12+
use Tempest\Support\Str;
1413

1514
final class EventBusDiscovery implements Discovery
1615
{
@@ -29,12 +28,7 @@ public function discover(DiscoveryLocation $location, ClassReflector $class): vo
2928
continue;
3029
}
3130

32-
$eventName = match (true) {
33-
$eventHandler->event instanceof BackedEnum => $eventHandler->event->value,
34-
$eventHandler->event instanceof UnitEnum => $eventHandler->event->name,
35-
is_string($eventHandler->event) => $eventHandler->event,
36-
default => null,
37-
};
31+
$eventName = Str\parse($eventHandler->event, default: null);
3832

3933
if ($eventName === null) {
4034
$parameters = iterator_to_array($method->getParameters());

packages/event-bus/src/GenericEventBus.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace Tempest\EventBus;
66

7-
use BackedEnum;
87
use Closure;
98
use Tempest\Container\Container;
10-
use UnitEnum;
9+
use Tempest\Support\Str;
1110

1211
final readonly class GenericEventBus implements EventBus
1312
{
@@ -33,13 +32,7 @@ public function dispatch(string|object $event): void
3332
/** @return \Tempest\EventBus\CallableEventHandler[] */
3433
private function resolveHandlers(string|object $event): array
3534
{
36-
$eventName = match (true) {
37-
$event instanceof BackedEnum => $event->value,
38-
$event instanceof UnitEnum => $event->name,
39-
is_string($event) => $event,
40-
default => $event::class,
41-
};
42-
35+
$eventName = Str\parse($event) ?: $event::class;
4336
$handlers = $this->eventBusConfig->handlers[$eventName] ?? [];
4437

4538
if (is_object($event)) {

packages/event-bus/src/Testing/EventBusTester.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace Tempest\EventBus\Testing;
44

5-
use BackedEnum;
65
use Closure;
76
use PHPUnit\Framework\Assert;
8-
use PHPUnit\Framework\ExpectationFailedException;
9-
use PHPUnit\Framework\GeneratorNotSupportedException;
107
use Tempest\Container\Container;
118
use Tempest\EventBus\EventBus;
129
use Tempest\EventBus\EventBusConfig;
13-
use UnitEnum;
10+
use Tempest\Support\Str;
1411

1512
final class EventBusTester
1613
{
@@ -110,12 +107,7 @@ private function findDispatches(string|object $event): array
110107
/** @return array<\Tempest\EventBus\CallableEventHandler> */
111108
private function findHandlersFor(string|object $event): array
112109
{
113-
$eventName = match (true) {
114-
$event instanceof BackedEnum => $event->value,
115-
$event instanceof UnitEnum => $event->name,
116-
is_string($event) => $event,
117-
default => $event::class,
118-
};
110+
$eventName = Str\parse($event) ?: $event::class;
119111

120112
return $this->fakeEventBus->eventBusConfig->handlers[$eventName] ?? [];
121113
}

packages/storage/src/Testing/StorageTester.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
use Tempest\Container\GenericContainer;
77
use Tempest\Storage\Storage;
88
use Tempest\Storage\StorageInitializer;
9-
use Tempest\Support\Arr;
9+
use Tempest\Support\Str;
1010
use UnitEnum;
1111

12-
use function Tempest\Support\Str\to_kebab_case;
13-
1412
final readonly class StorageTester
1513
{
1614
public function __construct(
@@ -22,11 +20,9 @@ public function __construct(
2220
*/
2321
public function fake(null|string|UnitEnum $tag = null, bool $persist = false): TestingStorage
2422
{
25-
$storage = new TestingStorage(match (true) {
26-
is_string($tag) => to_kebab_case($tag),
27-
$tag instanceof UnitEnum => to_kebab_case($tag->name),
28-
default => 'default',
29-
});
23+
$storage = new TestingStorage(
24+
path: Str\to_kebab_case(Str\parse($tag, default: 'default')),
25+
);
3026

3127
$this->container->singleton(Storage::class, $storage, $tag);
3228

packages/support/src/Str/functions.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
declare(strict_types=1);
44

55
namespace Tempest\Support\Str {
6+
use BackedEnum;
67
use Stringable;
78
use Tempest\Support\Arr;
9+
use UnitEnum;
810
use voku\helper\ASCII;
911

1012
use function levenshtein as php_levenshtein;
@@ -887,7 +889,7 @@ function equals(Stringable|string $string, string|Stringable $other): bool
887889
/**
888890
* Parses the given value to a string, returning the default value if it is not a string or `Stringable`.
889891
*/
890-
function parse(mixed $string, string $default = ''): string
892+
function parse(mixed $string, ?string $default = ''): ?string
891893
{
892894
if (is_string($string)) {
893895
return $string;
@@ -901,6 +903,14 @@ function parse(mixed $string, string $default = ''): string
901903
return (string) $string;
902904
}
903905

906+
if ($string instanceof BackedEnum) {
907+
return (string) $string->value;
908+
}
909+
910+
if ($string instanceof UnitEnum) {
911+
return $string->name;
912+
}
913+
904914
if (is_object($string) && method_exists($string, '__toString')) {
905915
return (string) $string;
906916
}

0 commit comments

Comments
 (0)