Static Analysis with PHPStan in Magento 2

In this article, I will show how you can perform Static Analysis with PHPStan in Magento 2

What is PHPStan?

PHPStan is a static analysis tool for PHP code. It helps developers find errors in their codebase early during development by analyzing the code without actually executing it. PHPStan checks for type errors, undefined variables, unused code, and other potential issues, providing insights into improving code quality and reliability.

There are other code analyzer tools available, the popular and widely used is PHP_CodeSniffer but it can only check one file at a time.

PHPStan is useful for verifying issues such as:

  • If the Return statement is missing in the methods though every method should have a return statement.
  • We access variable or constant which has not been defined.
  • When we access an undefined property
  • Accessing to constant on an unknown class.
  • When the number of characters exceeds defined in PHP standard.
  • When the nesting level of a loop or statement is too deep.
  • When DockBlock and return statement mismatch

Static Analysis with PHPStan in Magento 2

Did you know that Magento actually comes with PHPStan pre-installed? You can confirm this by checking the composer.json file in the root of your Magento instance.

Static Analysis with PHPStan in Magento 2

To confirm which version of PHPStan you are running, execute:

composer show phpstan/phpstan

To use the PHPStan in Magento we should always update it using composer. Use the below command to update the version.

composer require --dev phpstan/phpstan

The easiest way to update a specific package is to simply install it again using the require command.
Once you update the PHPStan, you need to create a PHPStan configuration file named phpstan.neon at root directory of your project which looks like the below:

    level: 7
        - php
        - phtml
        - Api
        - Block
        - Controller
        - Helper
        - Model
        - Plugin
        - Repository
        - Setup
        - Ui
        - Validator
        - view
    - VendorName/ModuleName/phpstan-magento/extension.neon

This file is optional to create, this file is a configuration file used by PHPStan. It allows developers to customize and control the behavior of PHPStan during the static analysis process
The above configuration file comes with Magento 2 preinstalled package.

Let’s understand the usage of the commands mentioned in the file.

  • The level parameter determines the level of Strictness of the analysis, ranging from 0 (least strict) to 9 (most strict).
  • The paths parameter tells PHPStan which directories or files PHPStan should analyze
  • The includes parameter is where you have placed the phpstan configuration file, and it’s load custom rules or extensions to extend PHPStan’s capabilities

Now you can run PHPStan using the vendor/bin/phpstan analyze command

vendor/bin/phpstan analyze app/code/VendorName/ModuleName

In the above command, we’re analyzing the app/code/VendorName/ModuleName directory. We can also analyze certain files with the command below:

vendor/bin/phpstan analyze app/code/VendorName/ModuleName/Controller/Index/Index.php

PHPStan will output any errors or warnings it finds in your code. You can then use this information to fix potential issues and improve the quality of your code.

I hope this blog clearly explains How to perform Static Analysis with PHPStan in Magento 2. If I missed anything or you need additional information, please feel free to leave a comment on this blog. I will respond with a proper solution.

You may also like this:
How to create a child theme in Hyva Magento 2?

Rate this post