Magento recommends the consistent use of a code sniffer to enhance the readability of your code and help ensure that it meets Magento code standards. A code sniffer is a tool that automates the process of identifying code irregularities.
It allows automatically check your code against some of the common Magento and PHP coding issues, like:
- Raw SQL queries;
- SQL queries inside a loop;
- Direct class instantiation;
- Unnecessary collection loading;
- Excessive code complexity;
- Use of dangerous functions;
- Use of PHP superglobals;
- Code style issues
Install all dependencies via Composer:
composer create-project --repository=https://repo.magento.com magento/marketplace-eqp magento-coding-standardda
You’re required to authenticate; see Get your authentication keys for details.
The first step is defining what Code Sniffer installation will be used. In PhpStorm preferences go to:
Languages & Frameworks / PHP / Code Sniffer
To reduce the number of issues the most recommended way is to use the installation that comes with the Composer installation. For the Local configuration
open settings (...) and for PHP Code Sniffer (phpcs) path
insert an absolute path to the phpcs installation:
vendor/bin/phpcs
The next step is defining a coding standard to be used. In PhpStorm preferences go to:
Editor / Inspections / PHP / PHP Code Sniffer validation
MEQP coding standard consists of two rulesets - MEQP for Magento and MEQP2 for Magento 2. Each of them contains the rules depending on the requirements of each version. Both of the standards will be offered in the list atomatically (if not click to refresh the list).
- Enable
PHP Code Sniffer validation
- Select MEQP1 or MEQP2 from the list, depending on Magento version
- On the fly when writing code. PhpStorm will automatically check for irregularities in the editor and mark them as warnings out of the box.
- Inspecting the whole project, extension or any other scope
Inspecting any other scope, or in batch, is available in PhpStorm with the tool available from the main menu:
Code / Inspect Code...
$ cd magento-coding-standard
Once in the MEQP installation define the phpcs tool, directory to scan and a standard. Two standards are avaialble, MEQP1 and MEQP2, depending on Magento version. For Magento 2 run:
$ vendor/bin/phpcs /path/to/your/extension --standard=MEQP2
For automatical error fixing, dynamic sniffs and Marketplace Technical Review plese reffer to the MEQP Code Standard documentation from the sources.