Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Check Magento 2 Coding Standards with Code Sniffer

Vinh Jacker | 06-28-2024

How to Check Magento 2 Coding Standards with Code Sniffer How to Check Magento 2 Coding Standards with Code Sniffer

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 [email protected]: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

```

Usage via terminal example

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

IDE PHPStorm

  • 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 select Analyze.

Spectify inspection scopst

  • Errors will be displayed in the Quality Tools section, you need to check it.

Error in quality tool section

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:

</thead>
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!

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support & Maintenance Services

    mageplaza services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.