[英]Search and replace in all files for context action in PhpStorm
After upgrading a project to PHP8, I'd like to refactor my code to use the new features, eg.将项目升级到 PHP8 后,我想重构我的代码以使用新功能,例如。 replacing
更换
if (strpos($link, 'somestring') === false) {
with和
if (!str_contains($link, 'somestring')) {
PhpStorm offers a context menu for this action which is great. PhpStorm 为这个操作提供了一个上下文菜单,这很棒。 It also means that PhpStorm can detect these constructs.
这也意味着 PhpStorm 可以检测到这些结构。
But: Is there a possibility to replace all occurrences automatically, maybe in combination with the "Replace in files" menu?但是:是否有可能自动替换所有出现的地方,也许与“替换文件”菜单结合使用? Of course a regex would also do the job, but a full automatic process would be more comfortable.
当然,正则表达式也可以完成这项工作,但全自动过程会更舒服。
Find which inspection is this.找出这是哪个检查。 For this:
为了这:
strpos
in this case)strpos
上) Run that particular inspection.运行该特定检查。
Code | Analyze Code | Run Inspection by Name...
Code | Analyze Code | Run Inspection by Name...
Code | Analyze Code | Run Inspection by Name...
Code | Analyze Code | Run Inspection by Name...
( Ctrl + Alt + Shift + I here on Windows keymap). Code | Analyze Code | Run Inspection by Name...
( Ctrl + Alt + Shift + I此处为 Windows 键盘映射)。 The "Problems" toolwindow will be activated and will list all places where this inspection is triggered. “问题”工具窗口将被激活,并将列出触发此检查的所有位置。 You can do the batch fix here (on a file scope, not all occurrences at once though -- not all quick fixes have "fix all found occurrences at once" option).
您可以在此处进行批量修复(在文件 scope 上,但并非所有事件都一次出现——并非所有快速修复都有“一次修复所有找到的事件”选项)。
PS You can skip some steps (1.4, 2.1 & 2.2) by simply selecting Run inspection on...
on the first screenshot if you know for sure that it is the right inspection. PS您可以跳过某些步骤(1.4、2.1 和 2.2),只需在第一个屏幕截图上选择
Run inspection on...
如果您确定这是正确的检查。
Another approach / option -- use Rector that can do quite a few of such upgrades in automated way.另一种方法/选项——使用Rector可以自动执行相当多的此类升级。 It can be integrated with PhpStorm as of 2022.2 version (and you can always run it from a command line).
从 2022.2 版本开始,它可以与 PhpStorm 集成(您始终可以从命令行运行它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.