diff --git a/src/AuthCommand.php b/src/AuthCommand.php index ba74845..1ee6f6f 100644 --- a/src/AuthCommand.php +++ b/src/AuthCommand.php @@ -30,7 +30,7 @@ public function handle() $this->gen('SIMPLE_JWT_SECRET'); } - public function gen($key, string $value = null) + public function gen($key, ?string $value = null) { if (empty(env($key))) { file_put_contents(BASE_PATH . '/.env', sprintf(PHP_EOL . '%s=%s', $key, $value ?? str_random(16)), FILE_APPEND); diff --git a/src/Exception/UnauthorizedException.php b/src/Exception/UnauthorizedException.php index f097677..8bcb168 100644 --- a/src/Exception/UnauthorizedException.php +++ b/src/Exception/UnauthorizedException.php @@ -20,7 +20,7 @@ class UnauthorizedException extends AuthException protected int $statusCode = 401; - public function __construct(string $message, AuthGuard $guard = null, Throwable $previous = null) + public function __construct(string $message, ?AuthGuard $guard = null, ?Throwable $previous = null) { parent::__construct($message, 401, $previous); $this->guard = $guard; diff --git a/src/Guard/JwtGuard.php b/src/Guard/JwtGuard.php index 7bcbaa3..df6f2a8 100644 --- a/src/Guard/JwtGuard.php +++ b/src/Guard/JwtGuard.php @@ -90,7 +90,7 @@ public function user(?string $token = null): ?Authenticatable if ($result instanceof UnauthorizedException) { throw $result; } - return $result ?: null; + return $result ?? null; } try { @@ -98,7 +98,7 @@ public function user(?string $token = null): ?Authenticatable $jwt = $this->getJwtManager()->parse($token); $uid = $jwt->getPayload()['uid'] ?? null; $user = $uid ? $this->userProvider->retrieveByCredentials($uid) : null; - Context::set($key, $user ?: 0); + Context::set($key, $user ?? 0); return $user; } @@ -139,7 +139,7 @@ public function guest(?string $token = null): bool */ public function refresh(?string $token = null): ?string { - $token = $token ?: $this->parseToken(); + $token = $token ?? $this->parseToken(); if ($token) { Context::set($this->resultKey($token), null); @@ -158,7 +158,7 @@ public function refresh(?string $token = null): ?string return null; } - public function logout($token = null) + public function logout(?string $token = null) { if ($token = $token ?? $this->parseToken()) { Context::set($this->resultKey($token), null); @@ -170,7 +170,7 @@ public function logout($token = null) return false; } - public function getPayload($token = null): ?array + public function getPayload(?string $token = null): ?array { if ($token = $token ?? $this->parseToken()) { return $this->getJwtManager()->justParse($token)->getPayload(); @@ -183,7 +183,7 @@ public function getJwtManager(): JWTManager return $this->jwtManager; } - public function id($token = null) + public function id(?string $token = null) { if ($token = $token ?? $this->parseToken()) { return $this->getJwtManager()->parse($token)->getPayload()['uid']; diff --git a/src/Guard/SessionGuard.php b/src/Guard/SessionGuard.php index bdb65a5..5e6747f 100644 --- a/src/Guard/SessionGuard.php +++ b/src/Guard/SessionGuard.php @@ -52,7 +52,7 @@ public function user(): ?Authenticatable if ($result instanceof \Throwable) { throw $result; } - return $result ?: null; + return $result ?? null; } try { diff --git a/src/Guard/SsoGuard.php b/src/Guard/SsoGuard.php index 09187d9..5471553 100644 --- a/src/Guard/SsoGuard.php +++ b/src/Guard/SsoGuard.php @@ -46,9 +46,9 @@ public function getClients(): array return $this->config['clients'] ?? ['unknown']; } - public function login(Authenticatable $user, array $payload = [], string $client = null) + public function login(Authenticatable $user, array $payload = [], ?string $client = null) { - $client = $client ?: $this->getClients()[0]; // 需要至少配置一个客户端 + $client = $client ?? $this->getClients()[0]; // 需要至少配置一个客户端 $token = parent::login($user, $payload); $redisKey = str_replace('{uid}', (string) $user->getId(), $this->config['redis_key'] ?? 'u:token:{uid}'); @@ -64,12 +64,12 @@ public function login(Authenticatable $user, array $payload = [], string $client return $token; } - public function refresh(?string $token = null, string $client = null): ?string + public function refresh(?string $token = null, ?string $client = null): ?string { $token = parent::refresh($token); if ($token) { - $client = $client ?: $this->getClients()[0]; // 需要至少配置一个客户端 + $client = $client ?? $this->getClients()[0]; // 需要至少配置一个客户端 $redisKey = str_replace('{uid}', (string) $this->id($token), $this->config['redis_key'] ?? 'u:token:{uid}'); $this->redis->hSet($redisKey, $client, $token); }