ECG Magento Code Sniffer Coding Standard
, (*1)
ECG Magento Code Sniffer Coding Standard is a set of rules and sniffs for PHP_CodeSniffer tool., (*2)
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 instantiation of Mage and Enterprise classes;
- unnecessary collection loading;
- excessive code complexity;
- use of dangerous functions;
- use of PHP Superglobals;, (*3)
and many others., (*4)
Both Magento and Magento 2 are supported., (*5)
Installation
Before starting using our coding standard install PHP_CodeSniffer., (*6)
The recommended installation method for PHPCS is globally with Composer:, (*7)
composer global require "squizlabs/php_codesniffer=*"
Make sure Composer's bin directory (defaulted to ~/.composer/vendor/bin/
) is in your PATH., (*8)
Clone or download this repo somewhere on your computer or install it with Composer:, (*9)
composer require magento-ecg/coding-standard
Note: PHP_CodeSniffer 3.x is now required to run our coding standard. To install PHP_CodeSniffer 2.x compatible version:, (*10)
composer require magento-ecg/coding-standard:4.*
Add the ECG standards directory to PHP_CodeSniffer's installed paths:, (*11)
$ ./vendor/bin/phpcs --config-set installed_paths /path/to/your/folder/vendor/magento-ecg/coding-standard
You can check Installation by running following command:, (*12)
$ ./vendor/bin/phpcs -i
It should do the following output:, (*13)
The installed coding standards are EcgM2, PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12 and Magento2
Select a standard to run with CodeSniffer: EcgM2 for Magento 2, (*14)
Run CodeSniffer:, (*15)
$ ./vendor/bin/phpcs --standard=EcgM2,PSR2,Magento2 /path/to/code
This package is compatible with Composer Installer Plugins for PHPCS coding standards (such as https://github.com/Dealerdirect/phpcodesniffer-composer-installer) and can be automatically registered with PHPCS during installation., (*16)
To check design templates, you must specify phtml
in the --extensions
argument: --extensions=php,phtml
, (*17)
Usage Hook pre-commit
Get hook file from the existing repository., (*18)
$ /path/to/your/folder/vendor/magento-ecg/coding-standard/.githooks
Install Hooks on the project. Copy hook into the project GIT folder, (*19)
cp -R ./vendor/magento-ecg/coding-standard/.githooks ./
To automate hooks installation for all developers, it needs to include the following lines into your project's composer.json, (*20)
"scripts": {
"post-install-cmd": [
"git config core.hooksPath .githooks"
],
"post-update-cmd": [
"git config core.hooksPath .githooks"
]
}
Run composer install to refresh the project state., (*21)
composer install
You are done!
Now, each product commit will be validated for an alignment with Magento Coding Standards., (*22)
Usage PHPStorm
PhpStorm provides code style check through integration with the PHP Code Sniffer tool,
which validates your code for consistency with a coding standard and best practices., (*23)
Once Tool installed and configured in PhpStorm, the tool is available in any opened PHP file.
Errors and warnings reported by PHP Code Sniffer on-the-fly are displayed as popup messages., (*24)
Install coding standard for development
See Installation steps., (*25)
Configure PhpStorm
* Open Preference: "Settings > Preferences > Editor > Inspections > PHP > Quality Tools".
* Choose "PHP_CodeSniffer validation" option.
* It is ok to configure everything under the scope "Everything else":
* In the "Check files with extensions" field, provide the comma-separated list of file extensions that should be checked by PHP Code Sniffer: php, js, phtml.
* Installed standard path should be checked and selected project path, (*26)
/path/to/your/folder/vendor/magento-ecg/coding-standard
- Choose installed coding standard path as the main for standards
- Select "EcgM2" or "Custom" it the "Coding standard" list.
, (*27)
https://www.jetbrains.com/help/phpstorm/using-php-code-sniffer.html#installing-configuring-code-sniffer, (*28)
Note: We would also suggest enabling PHP Mess Detector validation as well.
Note: in case of using a Docker or VM, It needs to configure new PHPCS CLI Interpreter which will point out to VM/Docker., (*29)
Requirements
PHP 5.4 and up., (*30)
Checkout the php-5.3-compatible
branch to get the PHP 5.3 version., (*31)
Contribution
Please feel free to contribute new sniffs or any fixes or improvements for the existing ones., (*32)