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:
1 | vendor/squizlabs/php_codesniffer/bin/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:
Enter Root directory
Enter Root directory of Custom Coding Standart:
1 | dev/tests/static/framework/Magento |
Change settings for code scope validation
We can change settings of code scope validation, go to Code > Inspect Code:
Result of validation
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:
1 2 3 | composer create-project --repository=https://repo.magento.com magento/marketplace-eqp magento-coding-standard && cd magento-coding-standard |
2. Use it by running next commands:
For Magento 1.x
1 | vendor/bin/phpcs /path/to/your/extension --standard=MEQP1 |
For Magento 2.x
1 | vendor/bin/phpcs /path/to/your/extension --standard=MEQP2 |
3. The result of validation:
Official Documentation
Read also the official documentation:
http://devdocs.magento.com/guides/v2.0/coding-standards/bk-coding-standards.html
Lilya GogolevaMagento Developer
- If you’re a newbie merchant and don’t know how to install Magento 2...
- Get frustrated when it comes to adding Magento multiple languages? Not a...
- Although it can take much time and efforts to migrate Magento 1 to Magento 2 it...
- If you don’t know how to create a custom cache type in Magento, this article...
- Find out about Magento Enterprise Cloud Edition in our article to get more from...
- Are you planning to turn your ordinary store into a great online mall but...
- This article is devoted to the best free Magento Themes. View the list of 7 the...
- In the Magento world, it becomes more and more popular to create Progressive...