简体   繁体   English

在所有文件中搜索并替换 PhpStorm 中的上下文操作

[英]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.当然,正则表达式也可以完成这项工作,但全自动过程会更舒服。

  1. Find which inspection is this.找出这是哪个检查。 For this:为了这:

    • Place caret on the highlighted part (on strpos in this case)将插入符放在突出显示的部分(在这种情况下在strpos上)
    • Invoke Intentions/Quick Fix menu ( Alt + Enter on most keymaps or via light bulb icon using a mouse)调用 Intentions/Quick Fix 菜单(在大多数键盘映射上使用Alt + Enter或使用鼠标通过灯泡图标)
    • Expand the most appropriate option ( Arrow Right )展开最合适的选项(右箭头 在此处输入图像描述
    • Select "Edit inspection profile settings" -- it will take you to that inspection so you can clearly see its name and check what is does ( although in this particular case it is not really necessary as the name is already shown on the first screenshot; but in other cases it can have different text there ). Select “编辑检查配置文件设置”——它会带你去那个检查,这样你就可以清楚地看到它的名字并检查它做了什么(尽管在这种特殊情况下它并不是真正必要的,因为名称已经显示在第一个屏幕截图上;但在其他情况下,它可以有不同的文本)。 在此处输入图像描述
  2. Run that particular inspection.运行该特定检查。

    • Use 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 键盘映射)。
    • Locate that inspection there在那里找到那个检查
    • Run it on a desired scope (eg a folder/custom scope/whole project etc)在所需的 scope 上运行它(例如文件夹/自定义范围/整个项目等) 在此处输入图像描述
  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM