Magento 2 Coding Standard

Coding standards are a result of conventions designed to produce high-quality code. Adopting certain standards yields better code quality, reduces the time taken to develop, and minimizes maintenance cost. Following coding standards requires knowing the standards in question and meticulously applying it to every aspect of the code that we write.

Use Magento’s coding standards when you contribute to Magento’s codebase or create extensions:

  • The code demarcation standard: decoupling HTML, CSS and JS from PHP classes. By doing so, the backend related development stays unaffected by frontend development (and vice versa). This means that we can make business logic changes without fearing a broken frontend. Details
  • The PHP coding standard: refers to a set of rules that covers the following:
      • PSR-1 and PSR-2 compliance. PSR-1: Basic Coding Standard touches on PHP filenames, class names, namespaces, class constant, properties, and methods. PSR-2: Coding Style Guide extends the PSR-1 by touching on the actual inners of a class, such as spaces, braces, method and properties visibility, control structures, and so on.
      • The use of insecure functions
      • Unescaped output
      • The use of deprecated PHP functions
      • PHP code syntact
      • Naming convention
      • The use of PHP superglobals
      • Empty code blocks
      • Improper exception handling
      • Raw SQL queries and many other general PHP and Magento-specific code issues

    To ensure that the code meets the standard, developers are recommended to use PHP_CodeSniffer. This tool provides the mechanism of checking code compliance with specific coding standard.

  • The JavaScript coding standard: based on the Google JavaScript Style Guide. This coding standard touches on the JavaScript language and coding style rules. It is a lot like PSR-1 and PSR-2 for PHP. To ensure the code adheres to Magento’s conding standard, use Magento’s ESLint Rules.
  • The jQuery widget coding standard: mandatory for Magento core developers and recommended for third-party developers. It goes without saying how important jQuery UI widgets are in Magento. The standard describes several things, such as widget naming, instantiation, extension, DOM event bubbling, and so on.
  • The DocBlock standard: defines the requirement and conventions for addition of inline code documentation. The goal of this standard is to unify usage of code DocBlocks for all files, not specific to a particular language. However, a DocBlock standard for that particular language may override it.
  • JavaScript DocBlock standard: relates to the JavaScript code files and their inline documentation. It is a subset of Google JavaScript Style Guide and JSDoc, which can be found at Following this standard is optional for 3rd party Magento developers, but will help to create consistent, clean, and easy to read inline documentation.
  • The LESS coding standard: defines the formatting and coding style when working with LESS and CSS files.

You can read more about the actual details of each standard at

Series Navigation<< Magento 2 Request Flow

Leave a Reply