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:

  1. PSR-1 and PSR-2 compliance
  2. Insecure functions
  3. Unescaped output
  4. Deprecated PHP functions
  5. PHP code syntax
  6. Naming convention
  7. PHP superglobals
  8. Empty code blocks
  9. Improper exception handling
  10. 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:

Error10Critical code issues that indicate a bug or security vulnerability.
Warning9Possible security issues that can cause bugs.
Warning8Magento-specific code issues and design violations.
Warning7General code issues.
Warning6Code style issues.
Warning5PHPDoc 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 [email protected]

Go To Top