Project Wide PHP 7 Strict Types in PhpStorm 2016.3

PHP 7 is here, and thankfully gaining a lot of traction. There’s never been a better time to convert your project to PHP 7 strict types, and you’ll find so many bugs you never knew existed in the process. Adding the strict types declaration to all of those files can be painful, and you may forget some places. PhpStorm 2016.3 has got you covered.

PhpStorm 2016.3 has introduced a new inspection, the Strict types declaration setting that you can find under Type compatibility in the Inspections pane of the Preferences window.

2016-3-strict-typesOnce you enable this inspection, any files missing the strict type declaration will get a warning on the first line at the opening tag.

2016-3-strict-types-2

As with any of the inspections, you can also run across the whole (or parts) of your project by using the Run Inspection by Name option in the Code menu, then selecting the scope you wish to run the inspection on.

2016-3-strict-types-3

You can see here we have 68 files that are missing the declaration. Thankfully, PhpStorm 2016.3 also ships with an intention that can fix the problem, and add the statement to the top of the file. You can either do this in batch across all found files by clicking the Add strict types declaration button, or on a case by case basis in each file by firing the Show Intentions selector (default keybinding of Alt+Enter) and selecting Add strict type declaration.

Adding strict type hinting to your PHP projects just got a lot easier. Give it a go and let us know what you think.

– Gary and the PhpStorm Team

About Gary Hockin

Gary Hockin has been creating code to power web applications for 15 years, the last three of them at an extraordinary level. He is handsome, talented, funny and articulate, and also extremely modest. Gary is a valued contributor to Zend Framework 2 and is a member of the community review team. He's married and has two wonderful children, and when he's not coding, writing about code, speaking about code or reading about code, he can usually be found in the pub playing pool.
This entry was posted in Cool Feature. Bookmark the permalink.
  • http://funivan.com/ Funivan

    One of the best feature of the phpstorm is ability to fix problems in multiple files with the single click.

  • Alessandro Martins

    Unfortunately, PHP strict mode doesn’t allow null values, hope they fix them in the next version

    • Brent

      7.1 is allowing null types. PHP Internals has stated it will meet general availability on December 1.

      • Alessandro Martins

        I know, but had to rewrite all my models again due that

  • Andrzej Daniłowski

    Constants are treated always as mixed|null nevertheless they are defined (for example) as strings – this do not cause an error in PHP (strict_types enabled) but PHPStorm reports strict type violation.

    • Maxim Kolmakov

      Please contact our support team with a code sample that causes the issue (Help->New Support Request).