P粉8055354342023-09-04 21:30:05
對於靜態分析來說,守衛的工作方式似乎有點過於複雜,因此我進行了重構,將底層自訂程式碼從守衛中移出,直接移入中間件,這樣就成功了,現在可以正確檢測到異常.
class Authenticate { public function handle(Request $request, Closure $next, string|null $guard = null): mixed { try { $security = new Security(); $user = $security->middleware($request); $signedIn = !empty($user->id); // ... } catch (NoUserIdProvidedException) { // ... } // ... } }
安全類別是自訂邏輯,重要的是具有@throws的文件區塊足夠接近,可以被IDE找到
class Security{ /** * @param Request $request * @return bool|object|null * @throws InvalidDomainUser * @throws NoDomainUserException * @throws NoTokenOnRecordException * @throws NoTokenProvidedException * @throws NoUserException * @throws NoUserIdProvidedException */ public function middleware(Request $request): object|bool|null { // .... } }