Magento Coding Standard is a set of rules and sniffs for PHP_CodeSniffer tool. It is basically the official Magento Extension Quality Program Coding Standard with some additions and modifications I use.
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 super globals;
- Code style issues
and many others.
Magento Coding Standard consists of two rulesets - Magento1 for Magento and Magento2 for Magento 2. Each of them contains the rules depending on the requirements of each version.
$ cd magento-coding-standardSelect the standard to run with PHP_CodeSniffer. To check Magento extension run:
$ vendor/bin/phpcs /path/to/your/extension --standard=Magento1To check Magento 2 extension run:
$ vendor/bin/phpcs /path/to/your/extension --standard=Magento2By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension. To check design templates you can specify --extensions=php,phtml option.
To check syntax with specific PHP version set paths to php binary dir:
$ vendor/bin/phpcs --config-set php7.1_path /path/to/your/php7.1
$ vendor/bin/phpcs --config-set php7.0_path /path/to/your/php7
$ vendor/bin/phpcs --config-set php5.4_path /path/to/your/php5.4PHP_CodeSniffer offers the PHP Code Beautifier and Fixer (phpcbf) tool. It can be used in place of phpcs to automatically generate and fix all fixable issues. We highly recommend run following command to fix as many sniff violations as possible:
$ vendor/bin/phpcbf /path/to/your/extension --standard=Magento2Sniffs with complex logic, like Magento2.Classes.CollectionDependency and Magento2.SQL.CoreTablesModification require path to installed Magento 2 instance. You can specify it by running following command:
$ vendor/bin/phpcs --config-set m2-path /path/to/magento2Notice: Dynamic sniffs will not work without specified
m2-pathconfiguration option.
Notice: Don't forget to clear
cachefolder in project root directory if you want to run sniffs for other Magento versions.
- PHP >=5.5.0
- Composer
- PHP_CodeSniffer 3.*
Notice: PHP and Composer should be accessible globally.
Please feel free to contribute new sniffs or any fixes or improvements for the existing ones.