如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)

时间:2021-11-02 15:07:10

I'm trying to add the strict type declaration declare(strict_types=1); to all PHP class and trait files under a specific directory.

我正在尝试添加严格类型声明declare(strict_types = 1);到特定目录下的所有PHP类和特征文件。

  1. How can I get a list of files without that declaration using Linux terminal?

    如何使用Linux终端获取没有声明的文件列表?

  2. How can I add missing declarations using PhpStorm (inspection, replace in path + regexp)?

    如何使用PhpStorm添加缺失的声明(检查,路径+ regexp中的替换)?

Example /src/Foobar.php without declaration:

示例/src/Foobar.php没有声明:

<?php
/**
 * class file
 */
namespace Foo\Bar;

use Other\Foo\Bar;

class Foobar {
  // ..
}

Example /src/Foobar.php with declaration:

带声明的示例/src/Foobar.php:

<?php
/**
 * class file
 */
declare(strict_types = 1);

namespace Foo\Bar;

use Other\Foo\Bar;

class Foobar {
  // ..
}

Comments and imports ("use") are optional within a class file.

注释和导入(“use”)在类文件中是可选的。

What I've done:

我做了什么:

  • Trying to find a PhpStorm inspection that does what I want
  • 试图找到符合我想要的PhpStorm检查

  • Trying RegularExpressions but didn't find any statement searching for a lack of something within a multi-row context
  • 尝试使用RegularExpressions但没有找到任何语句来搜索多行上下文中缺少某些内容

1 个解决方案

#1


2  

You do not need regex for this - just use built-in "quick fix" for that.

你不需要正则表达式 - 只需使用内置的“快速修复”。

  1. Select files/folder(s) where you wish to run such stuff in Project View panel
  2. 在“项目视图”面板中选择要在其中运行此类内容的文件/文件夹

  3. Code | Run Inspection by Name...
  4. 代码|按名称运行检查...

  5. Find Missing strict type declaration inspection there (e.g. type strict and select it from narrowed list) and run it
  6. 在那里找到Missing严格类型声明检查(例如,键入strict并从窄化列表中选择它)并运行它

  7. Confirm the scope (where to run this inspection on)
  8. 确认范围(在何处运行此检查)

  9. See the results -- you may now apply Quick Fix (many ways -- at least 3 are shown on my screen) -- can be done on per file basis as well as all in one go
  10. 查看结果 - 您现在可以应用快速修复(许多方式 - 我的屏幕上至少显示3个) - 可以基于每个文件以及一次性完成

Screenshots:

Finding inspection to run:

寻找运行检查:

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)

Applying the fix (NOTE: I've run this inspection on single file only that has your sample code as content):

应用修复程序(注意:我只在单个文件上运行此检查,只有您的示例代码作为内容):

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)


Alternatively -- just enable appropriate inspection (which is disabled by default) and you will be able to add such code via usual Alt + Enter (on Windows using Default keymap; or via "light bulb" icon) directly in the code:

或者 - 只需启用适当的检查(默认情况下禁用),您就可以通过常规的Alt + Enter(在Windows上使用默认键盘映射;或通过“灯泡”图标)直接在代码中添加此类代码:

Settings/Preferences | Editor | Inspections | PHP | Type Compatibility | Missing strict type declaration

设置/首选项|编辑|检查| PHP |类型兼容性|缺少严格的类型声明

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)

Quick fix menu (Alt+Enter)

快速修复菜单(Alt + Enter)

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)

#1


2  

You do not need regex for this - just use built-in "quick fix" for that.

你不需要正则表达式 - 只需使用内置的“快速修复”。

  1. Select files/folder(s) where you wish to run such stuff in Project View panel
  2. 在“项目视图”面板中选择要在其中运行此类内容的文件/文件夹

  3. Code | Run Inspection by Name...
  4. 代码|按名称运行检查...

  5. Find Missing strict type declaration inspection there (e.g. type strict and select it from narrowed list) and run it
  6. 在那里找到Missing严格类型声明检查(例如,键入strict并从窄化列表中选择它)并运行它

  7. Confirm the scope (where to run this inspection on)
  8. 确认范围(在何处运行此检查)

  9. See the results -- you may now apply Quick Fix (many ways -- at least 3 are shown on my screen) -- can be done on per file basis as well as all in one go
  10. 查看结果 - 您现在可以应用快速修复(许多方式 - 我的屏幕上至少显示3个) - 可以基于每个文件以及一次性完成

Screenshots:

Finding inspection to run:

寻找运行检查:

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)

Applying the fix (NOTE: I've run this inspection on single file only that has your sample code as content):

应用修复程序(注意:我只在单个文件上运行此检查,只有您的示例代码作为内容):

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)


Alternatively -- just enable appropriate inspection (which is disabled by default) and you will be able to add such code via usual Alt + Enter (on Windows using Default keymap; or via "light bulb" icon) directly in the code:

或者 - 只需启用适当的检查(默认情况下禁用),您就可以通过常规的Alt + Enter(在Windows上使用默认键盘映射;或通过“灯泡”图标)直接在代码中添加此类代码:

Settings/Preferences | Editor | Inspections | PHP | Type Compatibility | Missing strict type declaration

设置/首选项|编辑|检查| PHP |类型兼容性|缺少严格的类型声明

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)

Quick fix menu (Alt+Enter)

快速修复菜单(Alt + Enter)

如何在没有strict_types声明的情况下找到所有php类文件(RegExp / PhpStorm)