简体   繁体   English

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

[英]How can I find all php class files without strict_types declaration (RegExp / PhpStorm)

I'm trying to add the strict type declaration declare(strict_types=1); 我正在尝试添加严格类型声明declare(strict_types=1); to all PHP class and trait files under a specific directory. 到特定目录下的所有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但没有找到任何语句在多行上下文中搜索缺少某些内容

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. Code | Run Inspection by Name...
  3. Find Missing strict type declaration inspection there (eg type strict and select it from narrowed list) and run it 在那里找到Missing strict type declaration检查(例如,键入strict并从缩小列表中选择它)并运行它
  4. Confirm the scope (where to run this inspection on) 确认范围(在何处运行此检查)
  5. 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 查看结果 - 您现在可以应用快速修复(许多方式 - 我的屏幕上至少显示3个) - 可以基于每个文件以及一次性完成

Screenshots: 截图:

Finding inspection to run: 寻找运行检查:

在此输入图像描述

Applying the fix (NOTE: I've run this inspection on single file only that has your sample code as content): 应用此修复程序(注意:我只对包含示例代码作为内容的单个文件运行此检查):

在此输入图像描述


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 | PHP | Type Compatibility | 类型兼容性| Missing strict type declaration 缺少严格的类型声明

在此输入图像描述

Quick fix menu ( Alt+Enter ) 快速修复菜单( Alt+Enter

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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