如何标记call_user_func消除PhpStorm异常未抛出警告(Lumen)
解决PhpStorm对Lumen Auth中间件的异常误报问题
以下几种方案可在不关闭检查的前提下消除误报:
1. 给中间件内的调用添加内联@throws注释
在触发Auth检查的代码行上方,手动标注会抛出的异常类,让PhpStorm明确此处存在异常抛出:
foreach ($guards as $guard) { /** @throws \Illuminate\Auth\AuthenticationException */ if ($this->auth->guard($guard)->check()) { return $this->auth->shouldUse($guard); } }
2. 用辅助方法包裹Auth调用并添加注释
将Auth检查逻辑抽成单独的辅助方法,给该方法添加@throws文档注释,PhpStorm能识别到方法抛出的异常:
/** * 执行Auth校验 * @param string|null $guard * @throws \Illuminate\Auth\AuthenticationException * @return bool */ protected function performAuthCheck($guard) { return $this->auth->guard($guard)->check(); } // 在authenticate方法中调用 foreach ($guards as $guard) { if ($this->performAuthCheck($guard)) { return $this->auth->shouldUse($guard); } }
3. 使用PhpStorm元数据扩展类型提示
创建项目根目录的.phpstorm.meta.php文件,通过元数据告知IDE RequestGuard的check方法会抛出指定异常,无需修改框架源码:
<?php namespace PHPSTORM_META { override(\Illuminate\Auth\RequestGuard::check(), map(['' => '@throws \Illuminate\Auth\AuthenticationException'])); }
4. 给AuthServiceProvider中的闭包添加注释
在定义Auth驱动的闭包内部,添加@throws注释明确异常类型:
$this->app['auth']->viaRequest('api', function ($request) { /** @throws \Illuminate\Auth\AuthenticationException */ return $this->app->make(Security::class)->middleware($request); });
内容的提问来源于stack exchange,提问作者CodeMonkey
相关产品推荐
相关产品推荐

