New Inspections in PhpStorm 2017.2
Inspections are the life-blood of PhpStorm, they are the little watchers that detect when you’ve done something suspicious (or even downright wrong) and pop up a little reminder saying “you may want to check that!”. PhpStorm 2017.2 introduces some nice new inspections to catch even more future bugs before they happen.
Declaring all properties you use in your PHP code is a good practice to ensure that you are using the property visibility you need, to catch bugs from typos, to improve type-hinting in PhpStorm using the `@var` annotation, and to make your code all around more readable. Declaring properties is also slightly more memory conscious, because of the underlying way that PHP’s opcodes (the “compiled” version of the PHP text file you edit) store properties.
PhpStorm 2017.2 extends the existing inspections that detect when you use undefined fields by also checking in the constructor. Checking the constructor is particularly important given that’s traditionally where you’d initialise these fields, so being warned that properties are undeclared at this point is very useful. You can turn it off if you don’t like it, you’ll find it under Editor | Inspections | PHP | Undefined | Undefined Field | Notify of dynamic field declaration.
Inspections are really important for catching logical bugs where the code parses but things aren’t happening as you expected. The new Statement has empty body inspection is designed to detect when you’ve added a conditional or loop statement but not included any code in the body of the statement. A badly placed `;` can make this a difficult bug to catch, even in code review.
You’ll find the settings for the inspection under Editor | Inspections | PHP | Probable bugs | Statement has empty body. Here you can configure if comments count as being content or not, and if you want the inspection to fire on `foreach` and `while` loops.
We’ve also added an extra level of checking in cases where you both extend an (abstract) class and implement an interface. In some cases, these method definitions can be mutually exclusive and we’ll now tell you if a parent class and interface definitions clash. For those of us working with complex object driven systems (like Doctrine), this will be a life saver in catching bugs before they happen.
You’ll find the inspection under Editor | Inspections | PHP | General | Method declaration of super class is incompatible with implemented interface.
PHP 7 is amazing – if you haven’t upgraded yet then you really should (I’m going to keep banging that drum until everyone is running PHP 7). We’ve added inspections recently to help you detect when you haven’t added `declare(strict_types=1)` to the top of every file and intentions to automate the process of adding it (you can find more information on that in this blog post).
The flipside of that is when you do have the declaration of strict types at the top of your file but are targeting PHP 5.6 (shame on you!), then the declare statement will cause your script to fail with a warning. It’s useful to know this if, for example, you’ve coded a new feature for a version that’s targeting PHP 7, but then decide to backport the feature to your PHP 5.6 supported branch.
This feature is automatically enabled when you have a language level of PHP 5.6 (or lower) set under Languages and Frameworks | PHP.
We hope you’re enjoying PhpStorm 2017.2 and we’re looking forward to hearing what you think about the new version.
– Gary & The PhpStorm Team