Back to Top

PHPStan in Magento 2

Updated 15 May 2023

Hello Friends!!

In today’s blog, I will explain about PHPStan.

PHPStan is a static analysis tool for PHP that can help you catch potential bugs and errors in your code before they even happen.

PHPStan is useful to verify issues such as –

● Return statement is missing in the methods However every method should return something.
● When we access an undefined constant.
● When we access an undefined property.
● If an array has N duplicate keys.
● When we call an undefined method.
● When we use possibly undefined variables.
● When we use unknown magic methods and properties on classes with __call and __get.
● Those anonymous function which has unused use.
● Accessing to constant on an unknown class.

Searching for an experienced
Magento 2 Company ?
Find out More

Magento actually comes with PHPStan pre-installed. You can confirm it by checking out the composer.json file in the root of your Magento instance like the below image.

phpstan in composer file

To confirm which version of PHPStan you are running, execute the below command.

composer show phpstan/phpstan

To use PHPStan with Magento 2, we should always update it via Composer.

Use the below command to update the PHPStan –

composer require --dev phpstan/phpstan

Once PHPStan is updated on your instance,

Create a PHPStan configuration file called phpstan.neon in the root folder of your project which looks like the below file –

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

In the above configuration file, we’re including the phpstan.neon file that comes with the Magento 2 base package, and defining some additional parameters.

The level parameter determines the strictness of the analysis, with higher numbers indicating more strictness.

The paths parameter tells PHPStan where to find the code that it should analyze.

The includes parameter is where you have placed the phpstan configuration file.

It can be in the vendor folder too if you have installed your module via composer.

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. You can also analyze individual files with the below command-

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

If PHPStan runs successfully It will look like the below image.

phpstan run successfully will look like this

In the above image, Webkul is my VendorName and DetectMobile is my ModuleName.

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

That’s all about PHPStan, I Hope this will be helpful.

If you have any questions please comment below, and we will try to respond to you.

Thanks for visiting the Webkul blog! 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home