xfxqzw
Last Updated: February 25, 2016
·
1.801K
· aimfeld

Quality control with PhpStorm code inspection

One of the coolest features of the PhpStorm IDE is code inspection. Code inspection checks your code in the background and highlights dozens of different errors and warnings. Before committing, PhpStorm will warn you about problems in your code. The code editor shows the code inspection status (red, yellow, green) on the top right corner and indicate the position and message of errors and warnings:

Picture

The inspection scope (which files to scan) and the inspection settings are highly configurable for various languages (PHP, Javascript, CSS, SASS, LESS, SQL, XML, ...). These settings can be shared in your developer team as XML files to ensure a consistent quality standard. Our code quality has improved a lot due to code inspection and has certainly prevented several bugs from getting into our code base.

Picture

PhpStorm is really smart when it comes to deducing variable types from PHPDoc comments such as @param or @return. To get the full quality control benefits of code inspection, it is sometimes necessary to type-hint variables with an @var statement:

class Module
{
    public function onBootstrap(MvcEvent $mvcEvent)
    {       
        $sm = $mvcEvent->getApplication()->getServiceManager();

        // Initialize the ZendDiCompiler
        /** @var ZendDiCompiler $zendDiCompiler */
        $zendDiCompiler = $sm->get('ZendDiCompiler');
        $zendDiCompiler->init($mvcEvent);
    }
}

The /** @var ZendDiCompiler $zendDiCompiler */ statement lets PhpStorm know the type of $zendDiCompiler. Therefore, the init()method is recognized which gives us hyper-linking to the method source, code-assist, and code inspection. In case the arguments for the init() method change and the method call is not updated, a full code inspection will point out the problem. This is an example of a full code inspection result, where problems are grouped by severity and language:

Picture

PhpStorm code inspection encourages the consistent use of PhpDocumentor type hints since we get commit warnings for methods calls on unrecognized objects. Besides preventing bugs, this also leads to better code-hyperlinking and code-assist.

1 Response
Add your response

11706

I completely agree; PhpStorm helped me to better follow coding guidelines as well as helping me to spot bugs and typos. Would highly recommend PhpStorm to any web developer.

over 1 year ago ·