MageDirect

Magento Code Standards

PHP Code Sniffer

All developers, that work with Magento, should follow code standards –  PSR-1: Basic Coding Standard and PSR-2: Coding Style Guide. It is necessary in order to maintain a standard of code quality.

Nowadays there are many tools that help developers to keep code clean and consistent. PHP Code Sniffer is one of such tools, which allows automatically detect violations of a defined set of coding standards.

Each Magento 2 project has preinstalled PHP Code Sniffer and set of rules and sniffs for this tool.

To define PHP Code Sniffer for Magento 2 project in PHPStorm IDE, follow next steps:

Open PHPStorm IDE

Open PHPStorm IDE and go to Settings > Languages & Frameworks > PHP > Code Sniffer and set PHP Code Sniffer (phpcs) path. In Magento 2 project it will be:


 

configuration local

 

phpcs

Go to the Settings > Editor

Go to the Settings > Editor / Inspections / PHP / PHP Code Sniffer validation.

Put a check near PHP Code Sniffer validation, select Custom Coding standard and then press three dots near Coding standard:

sniffer validation

Enter Root directory

Enter Root directory of Custom Coding Standart:

Change settings for code scope validation

We can change settings of code scope validation, go to Code > Inspect Code:

 

specify inspection code

 

Result of validation

 

results for inspection

 

For custom extensions, developers should use Magento Extension Quality Program Coding Standard. This is a set of rules and sniffs for PHP Code Sniffer, that 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.

To use Magento EQP Coding Standard, follow next steps:

1. Install Magento EQP via composer:


2. Use it by running next commands:

For Magento 1.x


For Magento 2.x


3. The result of validation:

 

console

 

Official Documentation

Read also the official documentation:

http://devdocs.magento.com/guides/v2.0/coding-standards/bk-coding-standards.html

Was this article helpful?
Dislike 0
Views: 49
Lilya Gogoleva

Lilya GogolevaMagento Developer

Rate us
1 Star2 Stars3 Stars4 Stars5 Stars
(5 votes, average: 4.80 out of 5)
rating-magedirectLoading...
Bottom Top