PhpStorm 2019.2 Symfony控制器中未使用的元件
我正在使用PhpStorm 2019.2 在Symfony控制器中,方法高亮显示为未使用。 Xdebug,然后我得到一个错误 我读到这可能是一个bug,新版本的IDE不能正确使用注释PhpStorm 2019.2 Symfony控制器中未使用的元件,php,symfony,phpstorm,Php,Symfony,Phpstorm,我正在使用PhpStorm 2019.2 在Symfony控制器中,方法高亮显示为未使用。 Xdebug,然后我得到一个错误 我读到这可能是一个bug,新版本的IDE不能正确使用注释 C:\OpenServer\OSPanel\modules\php\PHP_7.2\php.exe -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9090 -dxdebug.remote_host=127.0.0.1 C
C:\OpenServer\OSPanel\modules\php\PHP_7.2\php.exe -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9090 -dxdebug.remote_host=127.0.0.1 C:\OpenServer\OSPanel\domains\myblog.loc\src\Controller\IndexController.php
Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Controller\Controller' not found in C:\OpenServer\OSPanel\domains\myblog.loc\src\Controller\IndexController.php on line 18
Call Stack:
0.0679 382496 1. {main}() C:\OpenServer\OSPanel\domains\myblog.loc\src\Controller\IndexController.php:0
Dump $_SERVER
$_SERVER['REMOTE_ADDR'] is undefined
$_SERVER['REQUEST_METHOD'] is undefined
Dump $_SESSION
$_SESSION['*'] is undefined
Dump $_REQUEST
Process finished with exit code 255
某控制器
class ContactController extends Controller
{
/**
* @Route("/contact", name="contact_view")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function contactView()
{
return $this->render('contact/contact.html.twig', [
]);
}
}
目前,该类及其操作在控制器中被标记为未使用,我该怎么办?未使用的代码检查将无法识别正在调用的代码,因为PhpStorm无法正确检查在Symfony中如何调用控制器。其他类可能会发生这种情况,例如,当服务容器没有拾取该类时,它可能尚未被识别为已使用。在Symfony项目中,构建缓存,例如通过
bin/console cache:clear
(清除旧缓存并预热新缓存)应该对大多数人都有帮助,但肯定不总是这样,控制器也不行
您还可以对整个项目运行一次检查,然后将控制器标记为入口点,这将自动将这些类标记为(可能)已使用的代码。执行此操作时,PhpStorm将向项目中的文件.idea/misc.xml
中添加一个条目,以确保每次都忽略该条目
如果高误报率困扰您,您可以停用该功能或将其关闭,因此它将只查看受保护/私有方法。您可以在下面找到选项:Settings/Preferences | Editor | Inspections | PHP | Unused
另请参见:您可以将控制器及其操作添加为入口点。这样,这些类和方法就不会被报告为死代码 如果您的控制器位于
src/Controller
中,并且您的操作方法具有-action
后缀,则可以在设置|编辑器|检查
中添加这两条规则作为入口点:
Class Member
/Controller/*Controller *Action
/Controller/*Controller
这是一个bug。在我们等待修复的过程中,youtrack.jetbrains.com有一个解决方案 建议您仅禁用此检查,如下面的屏幕截图所示: Dmitry Tronin于2019年7月26日19:43发表评论 安德烈·巴扎诺夫评论2019年7月26日19:44 从设置中手动禁用
参考和引用与@lg102的解决方案类似,您也可以定义自己的注释 PHPStorm 2020 在
@Route
捷径
Alt+Enter
C:\OpenServer\OSPanel\modules\php\PHP_7.2\php.exe -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9090 -dxdebug.remote_host=127.0.0.1 C:\OpenServer\OSPanel\domains\myblog.loc\src\Controller\IndexController.php
Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Controller\Controller' not found in C:\OpenServer\OSPanel\domains\myblog.loc\src\Controller\IndexController.php on line 18
Call Stack:
0.0679 382496 1. {main}() C:\OpenServer\OSPanel\domains\myblog.loc\src\Controller\IndexController.php:0
Dump $_SERVER
$_SERVER['REMOTE_ADDR'] is undefined
$_SERVER['REQUEST_METHOD'] is undefined
Dump $_SESSION
$_SESSION['*'] is undefined
Dump $_REQUEST
Process finished with exit code 255
@Route
这两种方法的作用完全相同,您只需执行一次。无需执行任何操作。忽略或禁用此检查。以下是之后要查看的主要记录单:。需要在Symfony插件中添加特殊支持,以便它报告所使用的控制器操作。除此之外,考虑禁用对控制器的检查,甚至完全禁用。您的调试尝试与此无关:使用控制器直接执行文件会产生预期结果(PHP错误),因为框架的引导逻辑被完全忽略。我的zend mvc控制器中的操作也存在此问题。如果您有一个路由“…xxx/show”。。。将函数重命名为“show”。。。不是showView。这将导致PhpStorm将其视为已使用,而不是“错误”。这是一个局部解决方法。这不是一个解决方案,因为它会关闭并更正未使用的检查。