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

纵饮孤独 提交于 2019-12-10 16:31:29

问题


Okay I've finally switched to PHP7. My code is a bit old and will be refurbished. Some of the problems are:

class MagicClass
    function MagicClass(){
        //etc
    }

Which gives an deprecation warning during execution:

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

This is good:

class MagicClass
    function __construct(){
        //etc
    }

How can I get PhpStorm code inspection to warn me for such errors in my current codebase?


回答1:


It does not look like PhpStorm v10 supports this specific case. It has "PHP 7 Compatibility" inspection but it does not flag this code in any way (regardless what PHP Language Level you choose -- be it 5.6 or 7).

The solution is to install "Php Inspection (EA Extended)" plugin -- it has such inspection and it will work even if you choose 5.x as PHP Language level.

Please note: this plugin has a lot of inspections, some of which you may consider wrong/useless etc .. so you may have a need to go trough each additional inspection provided by this plugin and disable it if needed.




回答2:


In the Preferences (on OSX) or Settings (on Windows & Linux) dialog box choose Languages & Frameworks -> PHP on the left side list then check the PHP language level drop down box. If your PhpStorm is not very old then you should have PHP version 7 in that list.

PhpStorm uses the selected PHP version to check your code for syntax issues. It doesn't need to have the specified PHP version installed (I guess it comes with the interpreters.)

After you select the PHP version go to Editor->Inspections (also in the left side of the Preferences/Settings window) then in the right side make sure that the PHP -> General -> Language Level inspection is checked. In its description it says:

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).

Update

Apparently, the current versions of PhpStorm doesn't handle this specific issue. I tested it with version 9.0.2, 10.0.3 and 11 EAP and none of them complains about PHP4-style constructors.

It seems the Language Level Inspection focuses on the usage of new PHP features more than on the deprecated ones.




回答3:


As of PhpStorm 2017.1, there is a built-in inspection called "Old style constructor".



来源:https://stackoverflow.com/questions/35222415/phpstorm-get-code-inspection-warnings-for-methods-with-the-same-name-as-their

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!