How to Check Magento 2 Coding Standards (Check Quality Using Code Sniffer)
Every Magento developer has their own approach to coding. This can make it difficult for the developers to interpret and understand each other’s codes. Furthermore, non-standardized coding practices are less efficient and prone to errors.
Magento 2 coding standards improve the readability and quality of source codes. Following coding standards and best practices is a habit of Magento-certified professionals. Furthermore, it is essential to run a quality check before you submit an extension to the Magento marketplace.
In this post, find the complete step-wise method to quality-check Magento 2 coding standards of a module.
What is Magento Coding Standard?
Magento coding standard is the ruleset that validates code against the official code quality standards defined by Magento. The standardized code reduces the chances of performance issues, such as overloading, overwriting, and so on.
The PHP ruleset of Magento 2 coding standards checks for:
- PSR-1 and PSR-2 compliance
- Insecure functions
- Unescaped output
- Deprecated PHP functions
- PHP code syntax
- Naming convention
- PHP superglobals
- Empty code blocks
- Improper exception handling
- Raw SQL queries and many other general PHP and Magento-specific code issues
Running the module through the Magento coding standards scans for the above-mentioned issues and lists them. Overall, the general idea of the Magento 2 coding standard is to make the code more clean, readable, and self-explanatory.
Now, let’s learn to check the code quality of the module as per the Magento 2 coding standards.
How to Check Magento 2 Coding Standards Using Code Sniffer?
Magento recommends using the PHP_CodeSniffer for checking the quality of core codes in Magento modules. It comes installed by default in Magento 2.
In order to check Magento coding standards using PHP Magento 2 Code Sniffer, first you’ll need to clone the Magento 2 coding standards tool. Here’s how to do that.
Download Magento Coding Standard
There are two ways to install the Magento coding standard.
- Method 1: Manually download the magento-coding-standard from GitHub and extract it in the root directory.
- Method 2: Use the following command to install it using Composer:
composer create-project magento/magento-coding-standard --stability=dev magento-coding-standard
To verify successful installation, you can run the following command (it should return the list of installed Magento coding standards):
vendor/bin/phpcs -i
Check Magento Coding Standard
Once the Magento coding standard is installed, you analyze the coding standard of any extension by running the following command:
vendor/bin/phpcs --standard=path/to/magento-coding-standard/Magento2 path/to/your_module
You can also add the severity option to the command to change the strictness of the code.
For example:
vendor/bin/phpcs --standard=path/to/magento-coding-standard/Magento2 path/to/your_module --severity=10
The above code test the module with severity 10.
Extensions in Magento marketplace are tested with this severity.
Take the following table for reference:
TYPE | SEVERITY | DESCRIPTION |
---|---|---|
Error | 10 | Critical code issues that indicate a bug or security vulnerability. |
Warning | 9 | Possible security issues that can cause bugs. |
Warning | 8 | Magento-specific code issues and design violations. |
Warning | 7 | General code issues. |
Warning | 6 | Code style issues. |
Warning | 5 | PHPDoc formatting and commenting issues. |
Some of the coding standard violations can be easily fixed by using the PHP Code Beautifier and Fixer.
Simply, run the following command:
bin/phpcbf path/to/your_module --standard=path/to/magento-coding-standard/Magento2
That’s it! I hope the Magento 2 coding standards and the process to check it are now clear to you.
In case you still have any doubts regarding the above solution, feel free to comment.
Thank You! 🍀
Mahetab Saiyad
He is a Magento developer, who loves sharing technical tips and solutions. Being a coding enthusiast, he keeps exploring new concepts!
Prev
The List of Top Brands That Use Shopify In 2024
The Definitive Guide to Local SEO in 2024 [Actionable Tips & Checklist]
Next