Have you ever wondered how easy it is to check the quality of your own code, your colleagues' code, or a third-party module you want to use on a Magento project?

The Magento development team has created the Magento Extension Quality Program Coding Standard (Magento EQP), which allows you to check the code compliance with the standard, as well as identify the following shortcomings:

- SQL queries execution in the loop;
- use of dangerous functions;
- use of super-global variables;
- excessive code complexity;
- Unjustified collections loads.

Installation

You can install the Magento EQP via Composer, running this CLI command:

git clone https://github.com/magento/magento-coding-standard.git
cd magento-coding-standard
composer install

In case you don't have the composer, you can download it here.

Checking

To check the code (which is, for example, in the /path/to/your/extension folder), go to the Magento EQP folder:

cd /path/to/magento-coding-standard

and run the following command:

vendor/bin/phpcs /path/to/your/extension --standard=Magento2 > log.txt

The check result  will be stored to the log.txt file and will look like this:

FILE: /path/to/your/extension/Model/Object.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 10 WARNINGS AFFECTING 10 LINES
----------------------------------------------------------------------
  46 | WARNING | Use of protected class members is discouraged.
  51 | WARNING | Use of protected class members is discouraged.
  56 | WARNING | Use of protected class members is discouraged.
  61 | WARNING | Use of protected class members is discouraged.
  66 | WARNING | Use of protected class members is discouraged.
  71 | WARNING | Use of protected class members is discouraged.
 109 | WARNING | Use of protected class members is discouraged.
 159 | WARNING | Direct object instantiation (object of
     |         | \Magento\Framework\DataObject) is discouraged in
     |         | Magento 2.
 222 | WARNING | The use of function is_null() is discouraged; use
     |         | strict comparison "=== null" instead.
 256 | WARNING | Direct throw of Exception is discouraged. Use
     |         | \Magento\Framework\Exception\LocalizedException
     |         | instead.
----------------------------------------------------------------------

First of all, pay attention to whether there is any information about ERRORS that need to be corrected, especially if you want your code to pass the technical test on Magento Marketplace. Try to correct WARNINGS as well.

Automatic Error Correction

If you receive an extremely large number of warnings after the quality check, Magento EQP provides a tool that will automatically correct the deficiencies for you, if possible.

To start the auto-correction process, run this command:

vendor/bin/phpcbf /path/to/your/extension --standard=Magento2