4
votes

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?

3
which version of PHPStrom you are using? - Chetan Ameta
Just use php linter php -l to list and fix it once and forever. - Alex Blex
PHPStorm 10.0.3 php -l is nice, but howto setup for phpstorm? - Kapitein Witbaard
"Which gives me an error:" What exactly gives you such "error" -- PhpStorm or PHP itself when executing? - LazyOne
fair changed the question - Kapitein Witbaard

3 Answers

3
votes

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
votes

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.

1
votes

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

enter image description here

enter image description here