Improve Magento 2 coding standards
Every Magento developer follows their own coding approach, which can lead to difficulties in interpreting and understanding each other’s codes. Non-standardized coding practices are not only less efficient but also prone to errors.
To address these issues, Magento 2 introduces coding standards that enhance the readability and quality of source codes. Adhering to these coding standards is a hallmark of Magento-certified professionals and is essential when submitting extensions to the Magento marketplace.
What are Magento Coding Standards?
Magento coding standards consist of a set of rules that validate code against the official code quality standards defined by Magento. Following these standards helps reduce the chances of performance issues, such as overloading and overwriting.
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 other general PHP and Magento-specific code issues
Running the module through the Magento coding standards scans for these issues and lists them, ensuring that the code is clean, readable, and self-explanatory.
Checking Magento 2 Coding Standards Using Code Sniffer
To check the quality of core codes in Magento modules, Magento recommends using PHP_CodeSniffer, which comes installed by default in Magento 2.
Here’s how you can check Magento coding standards using PHP Magento 2 Code Sniffer:
Step 1: Download Magento Coding Standard
You have two options to install the Magento coding standard:
Method 1: Manual Download
- Download the magento-coding-standard from GitHub.
- Extract it in the root directory of your project.
Method 2: Using Composer
Run the following Composer command to install the Magento coding standard:
composer create-project magento/magento-coding-standard --stability=dev magento-coding-standard
To verify successful installation, run the following command (it should return the list of installed Magento coding standards):
vendor/bin/phpcs -i
Step 2: Check Magento Coding Standard
Once the Magento coding standard is installed, you can 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 adjust the strictness of the code:
vendor/bin/phpcs --standard=path/to/magento-coding-standard/Magento2 path/to/your_module --severity=10
The above code tests the module with severity 10, which is the same severity used to test extensions in the Magento marketplace. Here’s a reference table for severity levels:
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. |
Code Beautification and Fixing
Some coding standard violations can be easily fixed by using the PHP Code Beautifier and Fixer (phpcbf). To apply fixes, run the following command:
vendor/bin/phpcbf --standard=Magento2 app/code/MyAwesomeExtension
That’s it! By following Magento 2 coding standards and using Code Sniffer and Code Beautifier and Fixer, you can ensure your code meets the highest quality standards. If you have any questions, feel free to reach us out at contact@webbytroops.com