I am currently working on Magento 2 coding standard checking project.
Is any other tool available, other than PHP_CodeSniffer, to check Magento 2 and PHP coding standing?
I am currently working on Magento 2 coding standard checking project.
Is any other tool available, other than PHP_CodeSniffer, to check Magento 2 and PHP coding standing?
Magento 2 use ECGM2 coding standard
You can download from here
ECG Magento Code Sniffer Coding Standard is a set of rules and sniffs for PHP_CodeSniffer tool.
It allows automatically check your code against some of the common Magento and PHP coding issues, like:
and many others.
Both Magento and Magento 2 are supported.
Magento 2 use ECGM2 coding standard:
Install Coding Standard :
cd to/your/magento/install/directory
composer create-project --repository=https://repo.magento.com magento/marketplace-eqp magento-coding-standard
composer require magento/marketplace-eqp
cd magento-coding-standard
If you get error:
PHP Fatal error: Uncaught PHP_CodeSniffer_Exception: Referenced sniff "MEQP1.Exceptions.Namespace" does not exist
vendor/bin/phpcs --config-set installed_paths ../../..
vendor/bin/phpcs --config-set m2-path <path-to-magento2> ( for eg. sudo vendor/bin/phpcs --config-set m2-path /var/www/html/magento2 )
Test your M2 extension:
To auto correct errors use phpcbf :
vendor/bin/phpcbf <path-to-magento2>/app/code/Vendor/Module/Block/ --standard=MEQP2 --no-patch --extensions=php,phtml,xml,css,js
For View File
vendor/bin/phpcbf <path-to-magento2>/app/code/Vendor/Module/view/frontend/templates --standard=MEQP2 --extensions=php,phtml
To Check Error/ Warnnings :
vendor/bin/phpcs <path-to-magento2>/app/code/Vendor/Module/Block/ --standard=MEQP2 --extensions=phtml,xml,css,js,php
For View File
vendor/bin/phpcbf <path-to-magento2>/app/code/Vendor/Module/view/frontend/templates --standard=MEQP2 --extensions=php,phtml
Solution for some warnings available here :
How to fix warnings / errors raised by the Magento Marketplace technical review report?
Marketplace Technical Review :
vendor/bin/phpcs /path/to/your/extension --standard=MEQP2 --severity=10
Validate Magento 2 extension:
Download "validate_m2_package.php" from https://gist.github.com/alankent/fcf280dd9c599921b71d#file-validate_m2_package_v2-php and put it on root of magento:
cd <path-to-magento2>
php validate_m2_package.php Vendor_Module.zip
composer require magento/marketplace-eqp
fix it with this composer config repositories.magento-eqp git https://github.com/magento/marketplace-eqp
. github.com/magento/marketplace-eqp/issues/2
– obscure
Jan 29 at 10:56
In addition to Prince Patels answer ...
Magento 2 also uses PSR-2 coding standard, so you can also run phpcs --standard=PSR2 /path/to/module
. For fixing PSR-2 "errors" you can use "PHP Code Beautifier and Fixer (phpcbf)"
For me this works very good ...
Commands:
phpcbf --standard=PSR2 ./app/
phpcs --report-gitblame=CS-blame --report-source=CS-code --report-full=CS-full ./app/
cat CS-* >> ECG-PSR2 ; rm CS-*
If you use git
, you can add a pre-commit
hook to run phpcs
/phpcbf
automatically before commiting your changes.
Update:
Not espaccially for magento coding standards, but good practice imho ...
phpcbf
with PSR2
and MEQP2
standardphpcs -s
with both standardsphpmd -s
phpstan -l 7
0.8.5
-s
option is to display sniff name ... maybe use ful if you want to supress warnings
For nice reports I'd suggest phpdox. It aggregates data from ...
phploc
phpcs
phpmd
phpunit
code coverageSample (w/o code coverage): http://phpdox.de/demo/PHPUnit/index.xhtml
Other tools:
To validate vulnerabilities of M2 application, we can use :
ZAP(Zed Attack Proxy).