How to Check Magento 2 Coding Standards with Code Sniffer
Vinh Jacker | 3 days ago
The Most Popular Extension Builder for Magento 2
With a big catalog of 224+ extensions for your online store
Variations in coding practices among Magento developers can create challenges when interpreting and understanding each other’s code. Non-standardized approaches are not only less efficient but also increase the risk of errors.
In Magento 2, adhering to coding standards is necessary for readability, maintainability, and consistency. To help enforce these standards, you should use a code sniffer, which analyzes your code and highlights deviations. Therefore, in this article, we’ll guide you on how to check Magento 2 coding standard using code sniffer.
What is Magento Coding Standard?
The Magento coding standard is a set of rules designed for the PHP_CodeSniffer tool, specifically tailored to Magento projects. These standards help maintain consistent and high-quality code within Magento projects. Adhering to these standards ensures that your code follows best practices, making it easier to read, understand, and maintain.
Steps to Check Magento 2 Coding Standard Using Code Sniffer
1. Installation within a Magento 2 project
- Run the following command to add the Magento coding standard as a development dependency:
composer require --dev magento/magento-coding-standard``
- To enable it, add the following to your project’s composer.json:
"scripts": {
"post-install-cmd": [
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"
],
"post-update-cmd": [
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"
]
}
2. Installation for development
- Clone the GitHub repository:
git clone git@github.com:magento/magento-coding-standard.git
cd magento-coding-standard
composer install
- Alternatively, install it as a standalone application via Composer:
composer create-project magento/magento-coding-standard --stability=dev magento-coding-standard
3. Usage
Via terminal
- Analyze your code using phpcs:
vendor/bin/phpcs --standard=Magento2 app/code/MyAwesomeExtension
- Automatically fix issues with phpcbf:
vendor/bin/phpcbf --standard=Magento2 app/code/MyAwesomeExtension
Ex: For a special file
vendor/bin/phpcs app/code/Mageplaza/CustomAttribute/Setup/Patch/Data/UpgradeProductAttr.php --extensions=php,phtml
OR
Ex: For a directory
vendor/bin/phpcs app/code/Mageplaza/CustomAttribute/
--severity=10 --extensions=php,phtml
Via IDE PHPStorm
Let Setup like this in PHPStorm 2024
-
Go to the
File > Settings > PHP > Quality Tools > PHP_CodeSniffer
-
Click to the … button
After that, you get this image
-
Click validate to show the OK message.
-
Then, select Magento 2 and enter the
php, phtml-like
image![PHP configuration](https://cdn2.mageplaza.com/media/blog/check-magento-2-coding-standards/php-configuration.png)
-
The next step is to select the file or folder you want to check the code for.
-
Right-click, choose
Inspect Code
, and then selectAnalyze
.
- Errors will be displayed in the Quality Tools section, you need to check it.
4. Contribution
You can contribute by:
-
Document existing rules
-
Enhance existing rules
-
Propose new PHP CodeSniffer rules
-
Participate in discussions on new rules
-
Check the Community Dashboard for opportunities to contribute
5. Additional severity
The Magento Coding Standard classifies code sniffer violations into the following categories:
Type | Severity | Description |
---|---|---|
Error | 10 | Critical issues in the code that could mean a bug or security risk |
Warning | 9 | Possible security risks that might lead to 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 |
Note that you may encounter the serious error type that must be fixed. Otherwise, your website is at high risk of being attacked or encountering errors in the future.
Wrap Up
Checking Magento 2 coding standards using Code Sniffer is essential for maintaining a high-quality codebase. By adhering to consistent rules, you improve readability, collaboration, and long-term maintainability. Make Code Sniffer a part of your development workflow to ensure your Magento 2 projects meet the highest standards.
Feel free to explore the Magento Coding Standard repository for more details. If you have any other questions or need further assistance, feel free to ask!