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 { // .... } }