Skip to content

Commit 9606812

Browse files
committed
Remove (string) casts from enum_value() calls
Let int-backed enums fail with TypeError at typed boundaries instead of silently converting to strings like "1". This matches Laravel's behavior where enum_value() returns the raw value and type mismatches fail naturally. Also adds enum tests for TaggedCache, RedisTaggedCache, and RateLimited.
1 parent ad15a58 commit 9606812

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+653
-255
lines changed

src/auth/src/Access/Gate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public function authorize(UnitEnum|string $ability, mixed $arguments = []): Resp
287287
public function inspect(UnitEnum|string $ability, mixed $arguments = []): Response
288288
{
289289
try {
290-
$result = $this->raw((string) enum_value($ability), $arguments);
290+
$result = $this->raw(enum_value($ability), $arguments);
291291

292292
if ($result instanceof Response) {
293293
return $result;

src/broadcasting/src/InteractsWithBroadcasting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait InteractsWithBroadcasting
2121
*/
2222
public function broadcastVia(UnitEnum|array|string|null $connection = null): static
2323
{
24-
$connection = is_null($connection) ? null : (string) enum_value($connection);
24+
$connection = is_null($connection) ? null : enum_value($connection);
2525

2626
$this->broadcastConnection = is_null($connection)
2727
? [null]

src/bus/src/PendingBatch.php

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

55
namespace Hypervel\Bus;
66

7-
use BackedEnum;
87
use Closure;
98
use Hyperf\Collection\Arr;
9+
use UnitEnum;
1010
use Hyperf\Collection\Collection;
1111
use Hyperf\Conditionable\Conditionable;
1212
use Hyperf\Coroutine\Coroutine;
@@ -212,11 +212,9 @@ public function connection(): ?string
212212
/**
213213
* Specify the queue that the batched jobs should run on.
214214
*/
215-
public function onQueue(BackedEnum|string|null $queue): static
215+
public function onQueue(UnitEnum|string|null $queue): static
216216
{
217-
$value = enum_value($queue);
218-
219-
$this->options['queue'] = is_null($value) ? null : (string) $value;
217+
$this->options['queue'] = enum_value($queue);
220218

221219
return $this;
222220
}

src/bus/src/PendingChain.php

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

55
namespace Hypervel\Bus;
66

7-
use BackedEnum;
87
use Closure;
98
use DateInterval;
109
use DateTimeInterface;
10+
use UnitEnum;
1111
use Hyperf\Conditionable\Conditionable;
1212
use Hyperf\Context\ApplicationContext;
1313
use Hypervel\Bus\Contracts\Dispatcher;
@@ -66,11 +66,9 @@ public function onConnection(?string $connection): static
6666
/**
6767
* Set the desired queue for the job.
6868
*/
69-
public function onQueue(BackedEnum|string|null $queue): static
69+
public function onQueue(UnitEnum|string|null $queue): static
7070
{
71-
$value = enum_value($queue);
72-
73-
$this->queue = is_null($value) ? null : (string) $value;
71+
$this->queue = enum_value($queue);
7472

7573
return $this;
7674
}

src/bus/src/PendingDispatch.php

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

55
namespace Hypervel\Bus;
66

7-
use BackedEnum;
87
use DateInterval;
98
use DateTimeInterface;
109
use Hyperf\Context\ApplicationContext;
10+
use UnitEnum;
1111
use Hypervel\Bus\Contracts\Dispatcher;
1212
use Hypervel\Cache\Contracts\Factory as CacheFactory;
1313
use Hypervel\Queue\Contracts\ShouldBeUnique;
@@ -30,7 +30,7 @@ public function __construct(
3030
/**
3131
* Set the desired connection for the job.
3232
*/
33-
public function onConnection(BackedEnum|string|null $connection): static
33+
public function onConnection(UnitEnum|string|null $connection): static
3434
{
3535
$this->job->onConnection($connection);
3636

@@ -40,7 +40,7 @@ public function onConnection(BackedEnum|string|null $connection): static
4040
/**
4141
* Set the desired queue for the job.
4242
*/
43-
public function onQueue(BackedEnum|string|null $queue): static
43+
public function onQueue(UnitEnum|string|null $queue): static
4444
{
4545
$this->job->onQueue($queue);
4646

@@ -50,7 +50,7 @@ public function onQueue(BackedEnum|string|null $queue): static
5050
/**
5151
* Set the desired connection for the chain.
5252
*/
53-
public function allOnConnection(BackedEnum|string|null $connection): static
53+
public function allOnConnection(UnitEnum|string|null $connection): static
5454
{
5555
$this->job->allOnConnection($connection);
5656

@@ -60,7 +60,7 @@ public function allOnConnection(BackedEnum|string|null $connection): static
6060
/**
6161
* Set the desired queue for the chain.
6262
*/
63-
public function allOnQueue(BackedEnum|string|null $queue): static
63+
public function allOnQueue(UnitEnum|string|null $queue): static
6464
{
6565
$this->job->allOnQueue($queue);
6666

src/bus/src/Queueable.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function onConnection(UnitEnum|string|null $connection): static
7171
{
7272
$value = enum_value($connection);
7373

74-
$this->connection = is_null($value) ? null : (string) $value;
74+
$this->connection = is_null($value) ? null : $value;
7575

7676
return $this;
7777
}
@@ -83,7 +83,7 @@ public function onQueue(UnitEnum|string|null $queue): static
8383
{
8484
$value = enum_value($queue);
8585

86-
$this->queue = is_null($value) ? null : (string) $value;
86+
$this->queue = is_null($value) ? null : $value;
8787

8888
return $this;
8989
}
@@ -95,7 +95,7 @@ public function allOnConnection(UnitEnum|string|null $connection): static
9595
{
9696
$value = enum_value($connection);
9797

98-
$resolvedConnection = is_null($value) ? null : (string) $value;
98+
$resolvedConnection = is_null($value) ? null : $value;
9999

100100
$this->chainConnection = $resolvedConnection;
101101
$this->connection = $resolvedConnection;
@@ -110,7 +110,7 @@ public function allOnQueue(UnitEnum|string|null $queue): static
110110
{
111111
$value = enum_value($queue);
112112

113-
$resolvedQueue = is_null($value) ? null : (string) $value;
113+
$resolvedQueue = is_null($value) ? null : $value;
114114

115115
$this->chainQueue = $resolvedQueue;
116116
$this->queue = $resolvedQueue;

src/cache/src/RateLimiter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function limiter(UnitEnum|string $name): ?Closure
5656
*/
5757
private function resolveLimiterName(UnitEnum|string $name): string
5858
{
59-
return (string) enum_value($name);
59+
return enum_value($name);
6060
}
6161

6262
/**

src/cache/src/RedisTaggedCache.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class RedisTaggedCache extends TaggedCache
3232
*/
3333
public function add(UnitEnum|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
3434
{
35-
$key = (string) enum_value($key);
35+
$key = enum_value($key);
3636

3737
$this->tags->addEntry(
3838
$this->itemKey($key),
@@ -51,7 +51,7 @@ public function put(array|UnitEnum|string $key, mixed $value, DateInterval|DateT
5151
return $this->putMany($key, $value);
5252
}
5353

54-
$key = (string) enum_value($key);
54+
$key = enum_value($key);
5555

5656
if (is_null($ttl)) {
5757
return $this->forever($key, $value);
@@ -70,7 +70,7 @@ public function put(array|UnitEnum|string $key, mixed $value, DateInterval|DateT
7070
*/
7171
public function increment(UnitEnum|string $key, int $value = 1): bool|int
7272
{
73-
$key = (string) enum_value($key);
73+
$key = enum_value($key);
7474

7575
$this->tags->addEntry($this->itemKey($key), updateWhen: 'NX');
7676

@@ -82,7 +82,7 @@ public function increment(UnitEnum|string $key, int $value = 1): bool|int
8282
*/
8383
public function decrement(UnitEnum|string $key, int $value = 1): bool|int
8484
{
85-
$key = (string) enum_value($key);
85+
$key = enum_value($key);
8686

8787
$this->tags->addEntry($this->itemKey($key), updateWhen: 'NX');
8888

@@ -94,7 +94,7 @@ public function decrement(UnitEnum|string $key, int $value = 1): bool|int
9494
*/
9595
public function forever(UnitEnum|string $key, mixed $value): bool
9696
{
97-
$key = (string) enum_value($key);
97+
$key = enum_value($key);
9898

9999
$this->tags->addEntry($this->itemKey($key));
100100

src/cache/src/Repository.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function get(array|UnitEnum|string $key, mixed $default = null): mixed
123123
return $this->many($key);
124124
}
125125

126-
$key = (string) enum_value($key);
126+
$key = enum_value($key);
127127

128128
$this->event(new RetrievingKey($this->getName(), $key));
129129

@@ -198,7 +198,7 @@ public function put(array|UnitEnum|string $key, mixed $value, DateInterval|DateT
198198
return $this->putMany($key, $value);
199199
}
200200

201-
$key = (string) enum_value($key);
201+
$key = enum_value($key);
202202

203203
if ($ttl === null) {
204204
return $this->forever($key, $value);
@@ -270,7 +270,7 @@ public function setMultiple(iterable $values, DateInterval|DateTimeInterface|int
270270
*/
271271
public function add(UnitEnum|string $key, mixed $value, DateInterval|DateTimeInterface|int|null $ttl = null): bool
272272
{
273-
$key = (string) enum_value($key);
273+
$key = enum_value($key);
274274

275275
$seconds = null;
276276

@@ -308,23 +308,23 @@ public function add(UnitEnum|string $key, mixed $value, DateInterval|DateTimeInt
308308
*/
309309
public function increment(UnitEnum|string $key, int $value = 1): bool|int
310310
{
311-
return $this->store->increment((string) enum_value($key), $value);
311+
return $this->store->increment(enum_value($key), $value);
312312
}
313313

314314
/**
315315
* Decrement the value of an item in the cache.
316316
*/
317317
public function decrement(UnitEnum|string $key, int $value = 1): bool|int
318318
{
319-
return $this->store->decrement((string) enum_value($key), $value);
319+
return $this->store->decrement(enum_value($key), $value);
320320
}
321321

322322
/**
323323
* Store an item in the cache indefinitely.
324324
*/
325325
public function forever(UnitEnum|string $key, mixed $value): bool
326326
{
327-
$key = (string) enum_value($key);
327+
$key = enum_value($key);
328328

329329
$this->event(new WritingKey($this->getName(), $key, $value));
330330

@@ -408,7 +408,7 @@ public function rememberForever(UnitEnum|string $key, Closure $callback): mixed
408408
*/
409409
public function forget(UnitEnum|string $key): bool
410410
{
411-
$key = (string) enum_value($key);
411+
$key = enum_value($key);
412412

413413
$this->event(new ForgettingKey($this->getName(), $key));
414414

src/cache/src/TaggedCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public function putMany(array $values, DateInterval|DateTimeInterface|int|null $
5151
*/
5252
public function increment(UnitEnum|string $key, int $value = 1): bool|int
5353
{
54-
return $this->store->increment($this->itemKey((string) enum_value($key)), $value);
54+
return $this->store->increment($this->itemKey(enum_value($key)), $value);
5555
}
5656

5757
/**
5858
* Decrement the value of an item in the cache.
5959
*/
6060
public function decrement(UnitEnum|string $key, int $value = 1): bool|int
6161
{
62-
return $this->store->decrement($this->itemKey((string) enum_value($key)), $value);
62+
return $this->store->decrement($this->itemKey(enum_value($key)), $value);
6363
}
6464

6565
/**

0 commit comments

Comments
 (0)