PhpStorm:获取 "Methods with the same name as their class will not be constructors" 的代码检查警告

PhpStorm: get code inspection warnings for "Methods with the same name as their class will not be constructors"

好的,我终于切换到 PHP7。我的代码有点旧,会被翻新。一些问题是:

class MagicClass
    function MagicClass(){
        //etc
    }

在执行期间给出弃用警告:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; MagicClass has a deprecated constructor in

这个不错:

class MagicClass
    function __construct(){
        //etc
    }

如何获取 PhpStorm 代码检查以警告我当前代码库中的此类错误?

Preferences(在 OSX 上)或 Settings(在 Windows 和 Linux 上)对话框中选择 Languages & Frameworks -> PHP 在左侧列表中,然后选中 PHP language level 下拉框。如果您的 PhpStorm 不是很旧,那么该列表中应该有 PHP 版本 7。

PhpStorm 使用 selected PHP 版本检查您的代码是否存在语法问题。它不需要安装指定的 PHP 版本(我猜它是解释器附带的。)

在你 select 之后 PHP 版本转到 Editor->Inspections (也在 Preferences/[=11 的左侧=] window) 然后在右侧确保检查 PHP -> General -> Language Level 检查。在它的描述中说:

Checks that language features used in the source code correspond the selected language level. (i.e. traits can be used only in PHP 5.4). Desired language level is set in the project configuration (Project Settings | PHP).

更新

显然,PhpStorm 的当前版本不处理这个特定问题。我使用版本 9.0.2、10.0.3 和 11 EAP 对其进行了测试,其中 none 抱怨 PHP4 式构造函数。

语言级别检查似乎更关注新 PHP 功能的使用,而不是已弃用的功能。

PhpStorm v10 似乎不支持这种特定情况。它有 "PHP 7 Compatibility" 检查,但它不会以任何方式标记此代码(无论您选择什么 PHP 语言级别——无论是 5.6 还是 7)。

解决办法是安装"Php Inspection (EA Extended)"插件——它有这样的检查,即使你选择5.x作为PHP它也会工作] 语言水平。

请注意: 这个插件有很多检查,您可能会考虑其中的一些 wrong/useless 等..所以您可能需要检查每个此插件提供的额外检查并在需要时将其禁用。

As of PhpStorm 2017.1,有一个名为"Old style constructor"的内置检查。