From 3e0c7370e7f49cfa8d1a34b7b6cbfdff6fb0c3cb Mon Sep 17 00:00:00 2001 From: BrettonYe <867057410@qq.com> Date: Sun, 30 Jul 2023 01:22:45 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9Clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Helpers/ClientApiResponse.php | 6 +++--- app/Helpers/ClientConfig.php | 2 +- app/Helpers/WebApiResponse.php | 6 +++--- app/Http/Controllers/Admin/LogsController.php | 2 +- .../Controllers/Api/Client/ClientController.php | 2 +- app/Models/Article.php | 2 +- app/Models/User.php | 2 +- app/Services/OrderService.php | 2 +- app/Services/ProxyService.php | 14 +++++++------- app/Services/TelegramService.php | 2 +- app/Services/UserService.php | 2 +- app/Utils/DDNS/CloudFlare.php | 2 +- app/Utils/Helpers.php | 12 ++++++------ app/helpers.php | 4 ++-- 14 files changed, 30 insertions(+), 30 deletions(-) diff --git a/app/Helpers/ClientApiResponse.php b/app/Helpers/ClientApiResponse.php index 4e1fa1c6..d4125cd3 100644 --- a/app/Helpers/ClientApiResponse.php +++ b/app/Helpers/ClientApiResponse.php @@ -21,12 +21,12 @@ trait ClientApiResponse self::$client = $client; } - public function succeed(?array $data = null, ?array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse + public function succeed(array $data = null, array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse { return $this->jsonResponse(1, $codeResponse, $data, $addition); } - private function jsonResponse(int $status, array $codeResponse, array|string|null $data = null, array|null $addition = null): JsonResponse + private function jsonResponse(int $status, array $codeResponse, array|string $data = null, array $addition = null): JsonResponse { [$code, $message] = $codeResponse; $code = $code > 1000 ? (int) ($code / 1000) : $code; @@ -47,7 +47,7 @@ trait ClientApiResponse return response()->json($result, $code, ['content-type' => 'application/json']); } - public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, array|string|null $data = null): JsonResponse + public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, array|string $data = null): JsonResponse { return $this->jsonResponse(0, $codeResponse, is_array($data) ? $data[0] : $data); } diff --git a/app/Helpers/ClientConfig.php b/app/Helpers/ClientConfig.php index 2ffbc5a8..0299ce97 100644 --- a/app/Helpers/ClientConfig.php +++ b/app/Helpers/ClientConfig.php @@ -99,7 +99,7 @@ trait ClientConfig return $encode ? base64_encode($uri) : $uri; } - private function clash(?string $client = null): string + private function clash(string $client = null): string { $user = $this->getUser(); $webName = sysConfig('website_name'); diff --git a/app/Helpers/WebApiResponse.php b/app/Helpers/WebApiResponse.php index 8a14d968..5ed9ad9d 100644 --- a/app/Helpers/WebApiResponse.php +++ b/app/Helpers/WebApiResponse.php @@ -6,12 +6,12 @@ use Illuminate\Http\JsonResponse; trait WebApiResponse { - public function succeed(?array $data = null, ?array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse // 成功 + public function succeed(array $data = null, array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse // 成功 { return $this->jsonResponse('success', $codeResponse, $data, $addition); } - private function jsonResponse(string $status, array $codeResponse, ?array $data = null, ?array $addition = null): JsonResponse // 返回数据 + private function jsonResponse(string $status, array $codeResponse, array $data = null, array $addition = null): JsonResponse // 返回数据 { [$code, $message] = $codeResponse; if ($status === 'success') { @@ -42,7 +42,7 @@ trait WebApiResponse return $etag; } - public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, ?array $data = null): JsonResponse // 失败 + public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, array $data = null): JsonResponse // 失败 { return $this->jsonResponse('fail', $codeResponse, $data); } diff --git a/app/Http/Controllers/Admin/LogsController.php b/app/Http/Controllers/Admin/LogsController.php index b6dc40d2..30c63dc3 100644 --- a/app/Http/Controllers/Admin/LogsController.php +++ b/app/Http/Controllers/Admin/LogsController.php @@ -142,7 +142,7 @@ class LogsController extends Controller } // 在线IP监控(实时) - public function onlineIPMonitor(Request $request, ?int $id = null) + public function onlineIPMonitor(Request $request, int $id = null) { $query = NodeOnlineIp::with(['node:id,name', 'user:id,username'])->where('created_at', '>=', strtotime('-2 minutes')); diff --git a/app/Http/Controllers/Api/Client/ClientController.php b/app/Http/Controllers/Api/Client/ClientController.php index 32ba1bb3..2f7861c0 100644 --- a/app/Http/Controllers/Api/Client/ClientController.php +++ b/app/Http/Controllers/Api/Client/ClientController.php @@ -247,7 +247,7 @@ class ClientController extends Controller return $this->succeed(null, ['config' => $config]); } - private function clientConfig(?string $key = null) + private function clientConfig(string $key = null) { if (! config('client')) { Artisan::call('config:cache'); diff --git a/app/Models/Article.php b/app/Models/Article.php index 7364a90c..0d7b61c6 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -25,7 +25,7 @@ class Article extends Model return $query->whereType($type); } - public function scopeLang(Builder $query, ?string $language = null): Builder + public function scopeLang(Builder $query, string $language = null): Builder { return $query->whereLanguage($language ?? app()->getLocale()); } diff --git a/app/Models/User.php b/app/Models/User.php index 677269b9..05b764e9 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -223,7 +223,7 @@ class User extends Authenticatable return $query->where('status', '<>', -1)->whereEnable(0); } - public function nodes(?int $userLevel = null, ?int $userGroupId = null): Node|Builder|BelongsToMany + public function nodes(int $userLevel = null, int $userGroupId = null): Node|Builder|BelongsToMany { if ($userGroupId === null && $this->user_group_id) { // 使用默认的用户分组 $query = $this->userGroup->nodes(); diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index f24d54e4..2803e0b0 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -100,7 +100,7 @@ class OrderService return false; } - public function resetTimeAndData(?string $expired_at = null): array + public function resetTimeAndData(string $expired_at = null): array { // 计算下次重置与账号过期时间 if (! $expired_at) { // 账号有效期 $expired_at = $this->getFinallyExpiredTime(); diff --git a/app/Services/ProxyService.php b/app/Services/ProxyService.php index 1985a9e2..70c689f3 100644 --- a/app/Services/ProxyService.php +++ b/app/Services/ProxyService.php @@ -17,17 +17,17 @@ class ProxyService private static array $servers; - public function __construct(User|null $user = null) + public function __construct(User $user = null) { $this->setUser($user ?? auth()->user()); } - public function setUser(User|null $user): void + public function setUser(User $user = null): void { self::$user = $user; } - public function getUser(): User + public function getUser(): ?User { return self::$user; } @@ -37,7 +37,7 @@ class ProxyService return self::$servers; } - public function getProxyText(string $target, ?int $type = null): string + public function getProxyText(string $target, int $type = null): string { $servers = $this->getNodeList($type); if (empty($servers)) { @@ -58,7 +58,7 @@ class ProxyService return $this->clientConfig($target); } - public function getNodeList(?int $type = null, bool $isConfig = true): array + public function getNodeList(int $type = null, bool $isConfig = true): array { $query = self::$user->nodes()->whereIn('is_display', [2, 3]); // 获取这个账号可用节点 @@ -162,7 +162,7 @@ class ProxyService return match ($type) { 1 => 'vmess://'.base64url_encode(json_encode(['v' => '2', 'ps' => $text, 'add' => $url, 'port' => 0, 'id' => 0, 'aid' => 0, 'net' => 'tcp', 'type' => 'none', 'host' => $url, 'path' => '/', 'tls' => 'tls'], JSON_PRETTY_PRINT)), 2 => 'trojan://0@0.0.0.0:0?peer=0.0.0.0#'.rawurlencode($text), - default => 'ssr://'.base64url_encode('0.0.0.0:0:origin:none:plain:'.base64url_encode('0000').'/?obfsparam=&protoparam=&remarks='.base64url_encode($text).'&group='.base64url_encode(sysConfig('website_name')).'&udpport=0&uot=0'), + default => 'ssr://'.base64url_encode('0.0.0.0:0:origin:none:plain:MDAwMA/?obfsparam=&protoparam=&remarks='.base64url_encode($text).'&group='.base64url_encode(sysConfig('website_name')).'&udpport=0&uot=0'), }.PHP_EOL; } @@ -171,7 +171,7 @@ class ProxyService self::$servers = $servers; } - public function getProxyCode(string $target, ?int $type = null): ?string + public function getProxyCode(string $target, int $type = null): ?string {// 客户端用代理信息 $servers = $this->getNodeList($type); if (empty($servers)) { diff --git a/app/Services/TelegramService.php b/app/Services/TelegramService.php index 04df503c..14505783 100644 --- a/app/Services/TelegramService.php +++ b/app/Services/TelegramService.php @@ -8,7 +8,7 @@ class TelegramService { private static string $api; - public function __construct(?string $token = null) + public function __construct(string $token = null) { self::$api = 'https://api.telegram.org/bot'.($token ?? sysConfig('telegram_token')).'/'; } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 0876b833..82ad929e 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -9,7 +9,7 @@ class UserService { private static User $user; - public function __construct(?User $user = null) + public function __construct(User $user = null) { self::$user = $user ?? auth()->user(); } diff --git a/app/Utils/DDNS/CloudFlare.php b/app/Utils/DDNS/CloudFlare.php index d46152bf..16ff8ac6 100644 --- a/app/Utils/DDNS/CloudFlare.php +++ b/app/Utils/DDNS/CloudFlare.php @@ -38,7 +38,7 @@ class CloudFlare implements DNS exit(400); } - private function send(string $action, array $parameters = [], ?string $identifier = null): array + private function send(string $action, array $parameters = [], string $identifier = null): array { $client = Http::timeout(10)->retry(3, 1000)->withHeaders($this->auth)->baseUrl($this->apiHost)->asJson(); diff --git a/app/Utils/Helpers.php b/app/Utils/Helpers.php index edf8544c..e9771b27 100644 --- a/app/Utils/Helpers.php +++ b/app/Utils/Helpers.php @@ -53,7 +53,7 @@ class Helpers * @param int|null $inviter_id 邀请人 * @param string|null $nickname 昵称 */ - public static function addUser(string $username, string $password, int $transfer_enable, ?string $date = null, ?int $inviter_id = null, ?string $nickname = null): User + public static function addUser(string $username, string $password, int $transfer_enable, string $date = null, int $inviter_id = null, string $nickname = null): User { return User::create([ 'nickname' => $nickname ?? $username, @@ -133,7 +133,7 @@ class Helpers * @param string|null $msgId 对公查询ID * @param string $address 收信方 */ - public static function addNotificationLog(string $title, string $content, int $type, int $status = 1, ?string $error = null, ?string $msgId = null, string $address = 'admin'): int + public static function addNotificationLog(string $title, string $content, int $type, int $status = 1, string $error = null, string $msgId = null, string $address = 'admin'): int { $log = new NotificationLog(); $log->type = $type; @@ -156,7 +156,7 @@ class Helpers * @param int|null $goodsId 商品ID * @param int|null $orderId 订单ID */ - public static function addCouponLog(string $description, int $couponId, ?int $goodsId = null, ?int $orderId = null): bool + public static function addCouponLog(string $description, int $couponId, int $goodsId = null, int $orderId = null): bool { $log = new CouponLog(); $log->coupon_id = $couponId; @@ -177,7 +177,7 @@ class Helpers * @param int $amount 发生金额 * @param string|null $description 描述 */ - public static function addUserCreditLog(int $userId, ?int $orderId, int $before, int $after, int $amount, ?string $description = null): bool + public static function addUserCreditLog(int $userId, ?int $orderId, int $before, int $after, int $amount, string $description = null): bool { $log = new UserCreditLog(); $log->user_id = $userId; @@ -200,7 +200,7 @@ class Helpers * @param string|null $description 描述 * @param int|null $orderId 订单ID */ - public static function addUserTrafficModifyLog(int $userId, int $before, int $after, ?string $description = null, ?int $orderId = null): bool + public static function addUserTrafficModifyLog(int $userId, int $before, int $after, string $description = null, int $orderId = null): bool { $log = new UserDataModifyLog(); $log->user_id = $userId; @@ -222,7 +222,7 @@ class Helpers * @param int $status 状态 * @param string|null $error 报错 */ - public static function addMarketing(string $receiver, int $type, string $title, string $content, int $status = 1, ?string $error = null): bool + public static function addMarketing(string $receiver, int $type, string $title, string $content, int $status = 1, string $error = null): bool { $marketing = new Marketing(); $marketing->type = $type; diff --git a/app/helpers.php b/app/helpers.php index 78fc82bc..5fcf1b9b 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -27,7 +27,7 @@ if (! function_exists('base64url_decode')) { // 根据流量值自动转换单位输出 if (! function_exists('formatBytes')) { - function formatBytes(int $bytes, ?string $base = null, int $precision = 2): string + function formatBytes(int $bytes, string $base = null, int $precision = 2): string { $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; $bytes = max($bytes, 0); @@ -70,7 +70,7 @@ if (! function_exists('formatTime')) { // 获取系统设置 if (! function_exists('sysConfig')) { - function sysConfig(?string $key = null, ?string $default = null): array|string|null + function sysConfig(string $key = null, string $default = null): array|string|null { return $key ? config("settings.$key", $default) : config('settings'); }