Skip to content

Commit 9c38d3c

Browse files
committed
Use enum_value() for Sanctum token abilities
Replace Str::from() and Str::fromAll() with enum_value() for consistency with other enum-supporting APIs across the codebase. Also updates type hints from BackedEnum to UnitEnum to match the established pattern.
1 parent 9606812 commit 9c38d3c

27 files changed

+70
-49
lines changed

src/bus/src/PendingBatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Closure;
88
use Hyperf\Collection\Arr;
9-
use UnitEnum;
109
use Hyperf\Collection\Collection;
1110
use Hyperf\Conditionable\Conditionable;
1211
use Hyperf\Coroutine\Coroutine;
@@ -17,6 +16,7 @@
1716
use Psr\Container\ContainerInterface;
1817
use Psr\EventDispatcher\EventDispatcherInterface;
1918
use Throwable;
19+
use UnitEnum;
2020

2121
use function Hyperf\Support\value;
2222
use function Hypervel\Support\enum_value;

src/bus/src/PendingChain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use Closure;
88
use DateInterval;
99
use DateTimeInterface;
10-
use UnitEnum;
1110
use Hyperf\Conditionable\Conditionable;
1211
use Hyperf\Context\ApplicationContext;
1312
use Hypervel\Bus\Contracts\Dispatcher;
1413
use Hypervel\Queue\CallQueuedClosure;
1514
use Laravel\SerializableClosure\SerializableClosure;
15+
use UnitEnum;
1616

1717
use function Hyperf\Support\value;
1818
use function Hypervel\Support\enum_value;

src/bus/src/PendingDispatch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use DateInterval;
88
use DateTimeInterface;
99
use Hyperf\Context\ApplicationContext;
10-
use UnitEnum;
1110
use Hypervel\Bus\Contracts\Dispatcher;
1211
use Hypervel\Cache\Contracts\Factory as CacheFactory;
1312
use Hypervel\Queue\Contracts\ShouldBeUnique;
13+
use UnitEnum;
1414

1515
class PendingDispatch
1616
{

src/sanctum/src/Contracts/HasAbilities.php

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

55
namespace Hypervel\Sanctum\Contracts;
66

7-
use BackedEnum;
7+
use UnitEnum;
88

99
interface HasAbilities
1010
{
1111
/**
1212
* Determine if the token has a given ability.
1313
*/
14-
public function can(BackedEnum|string $ability): bool;
14+
public function can(UnitEnum|string $ability): bool;
1515

1616
/**
1717
* Determine if the token is missing a given ability.
1818
*/
19-
public function cant(BackedEnum|string $ability): bool;
19+
public function cant(UnitEnum|string $ability): bool;
2020
}

src/sanctum/src/Contracts/HasApiTokens.php

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

55
namespace Hypervel\Sanctum\Contracts;
66

7-
use BackedEnum;
87
use DateTimeInterface;
98
use Hyperf\Database\Model\Relations\MorphMany;
9+
use UnitEnum;
1010

1111
interface HasApiTokens
1212
{
@@ -18,17 +18,17 @@ public function tokens(): MorphMany;
1818
/**
1919
* Determine if the current API token has a given ability.
2020
*/
21-
public function tokenCan(BackedEnum|string $ability): bool;
21+
public function tokenCan(UnitEnum|string $ability): bool;
2222

2323
/**
2424
* Determine if the current API token is missing a given ability.
2525
*/
26-
public function tokenCant(BackedEnum|string $ability): bool;
26+
public function tokenCant(UnitEnum|string $ability): bool;
2727

2828
/**
2929
* Create a new personal access token for the user.
3030
*
31-
* @param array<BackedEnum|string> $abilities
31+
* @param array<string|UnitEnum> $abilities
3232
*/
3333
public function createToken(string $name, array $abilities = ['*'], ?DateTimeInterface $expiresAt = null): \Hypervel\Sanctum\NewAccessToken;
3434

src/sanctum/src/HasApiTokens.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace Hypervel\Sanctum;
66

7-
use BackedEnum;
87
use DateTimeInterface;
98
use Hyperf\Database\Model\Relations\MorphMany;
109
use Hypervel\Sanctum\Contracts\HasAbilities;
11-
use Hypervel\Support\Str;
10+
use UnitEnum;
11+
12+
use function Hypervel\Support\enum_value;
1213

1314
/**
1415
* @template TToken of \Hypervel\Sanctum\Contracts\HasAbilities = \Hypervel\Sanctum\PersonalAccessToken
@@ -35,27 +36,27 @@ public function tokens(): MorphMany
3536
/**
3637
* Determine if the current API token has a given ability.
3738
*/
38-
public function tokenCan(BackedEnum|string $ability): bool
39+
public function tokenCan(UnitEnum|string $ability): bool
3940
{
4041
return $this->accessToken && $this->accessToken->can($ability);
4142
}
4243

4344
/**
4445
* Determine if the current API token does not have a given ability.
4546
*/
46-
public function tokenCant(BackedEnum|string $ability): bool
47+
public function tokenCant(UnitEnum|string $ability): bool
4748
{
4849
return ! $this->tokenCan($ability);
4950
}
5051

5152
/**
5253
* Create a new personal access token for the user.
5354
*
54-
* @param array<BackedEnum|string> $abilities
55+
* @param array<string|UnitEnum> $abilities
5556
*/
5657
public function createToken(string $name, array $abilities = ['*'], ?DateTimeInterface $expiresAt = null): NewAccessToken
5758
{
58-
$abilities = Str::fromAll($abilities);
59+
$abilities = array_map(enum_value(...), $abilities);
5960

6061
$plainTextToken = $this->generateTokenString();
6162

src/sanctum/src/PersonalAccessToken.php

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

55
namespace Hypervel\Sanctum;
66

7-
use BackedEnum;
87
use Hyperf\Database\Model\Events\Deleting;
98
use Hyperf\Database\Model\Events\Updating;
109
use Hyperf\Database\Model\Relations\MorphTo;
@@ -14,7 +13,9 @@
1413
use Hypervel\Context\ApplicationContext;
1514
use Hypervel\Database\Eloquent\Model;
1615
use Hypervel\Sanctum\Contracts\HasAbilities;
17-
use Hypervel\Support\Str;
16+
use UnitEnum;
17+
18+
use function Hypervel\Support\enum_value;
1819

1920
/**
2021
* @property int|string $id
@@ -154,9 +155,9 @@ public static function findTokenable(PersonalAccessToken $accessToken): ?Authent
154155
/**
155156
* Determine if the token has a given ability.
156157
*/
157-
public function can(BackedEnum|string $ability): bool
158+
public function can(UnitEnum|string $ability): bool
158159
{
159-
$ability = Str::from($ability);
160+
$ability = enum_value($ability);
160161

161162
return in_array('*', $this->abilities)
162163
|| array_key_exists($ability, array_flip($this->abilities));
@@ -165,7 +166,7 @@ public function can(BackedEnum|string $ability): bool
165166
/**
166167
* Determine if the token is missing a given ability.
167168
*/
168-
public function cant(BackedEnum|string $ability): bool
169+
public function cant(UnitEnum|string $ability): bool
169170
{
170171
return ! $this->can($ability);
171172
}

src/sanctum/src/TransientToken.php

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

55
namespace Hypervel\Sanctum;
66

7-
use BackedEnum;
87
use Hypervel\Sanctum\Contracts\HasAbilities;
8+
use UnitEnum;
99

1010
class TransientToken implements HasAbilities
1111
{
1212
/**
1313
* Determine if the token has a given ability.
1414
*/
15-
public function can(BackedEnum|string $ability): bool
15+
public function can(UnitEnum|string $ability): bool
1616
{
1717
return true;
1818
}
1919

2020
/**
2121
* Determine if the token is missing a given ability.
2222
*/
23-
public function cant(BackedEnum|string $ability): bool
23+
public function cant(UnitEnum|string $ability): bool
2424
{
2525
return false;
2626
}

tests/Broadcasting/InteractsWithBroadcastingTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Hypervel\Broadcasting\InteractsWithBroadcasting;
1111
use Mockery as m;
1212
use PHPUnit\Framework\TestCase;
13+
use TypeError;
1314

1415
enum InteractsWithBroadcastingTestConnectionStringEnum: string
1516
{
@@ -105,7 +106,7 @@ public function testBroadcastWithIntBackedEnumThrowsTypeErrorAtBroadcastTime():
105106
$manager = m::mock(BroadcastingFactory::class);
106107

107108
// TypeError is thrown when BroadcastManager::connection() receives int instead of ?string
108-
$this->expectException(\TypeError::class);
109+
$this->expectException(TypeError::class);
109110
$broadcastEvent->handle($manager);
110111
}
111112
}

tests/Broadcasting/PendingBroadcastTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Mockery as m;
1313
use PHPUnit\Framework\TestCase;
1414
use Psr\EventDispatcher\EventDispatcherInterface;
15+
use TypeError;
1516

1617
enum PendingBroadcastTestConnectionStringEnum: string
1718
{
@@ -78,7 +79,7 @@ public function testViaWithIntBackedEnumThrowsTypeErrorAtBroadcastTime(): void
7879
$manager = m::mock(BroadcastingFactory::class);
7980

8081
// TypeError is thrown when BroadcastManager::connection() receives int instead of ?string
81-
$this->expectException(\TypeError::class);
82+
$this->expectException(TypeError::class);
8283
$broadcastEvent->handle($manager);
8384
}
8485

0 commit comments

Comments
 (0)