How To Setup Magento 2.x Coding Standard

Coding Standard is a set of rules / regulations to specify Magento 2.xCoding Standard, every developer / designer must follow while a participating in the Magento 2.x Project Development / Designing.

Magento 2.x Coding Standard design Application’s Security, Stability, Maintainability and Extensibility (extend or modify existing behavior)

The following below steps need to follow.

Step [1] – Go to Magento 2 Root Directory & run below command

composer create-project magento/magento-coding-standard --stability=dev magento-coding-standard

Once successfully installed message as below

Config value “installed_paths” added successfully

Step [2] – Add below line in Magento 2 Root Directory composer.json

"scripts": {
	"post-install-cmd": [
	"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"],
	"post-update-cmd": [
	"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"]
	}

Step [3] – Run below command

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:flush

Step [4] – Verify Installation
Command should return the list of installed coding standards .
go inside magento-coding-standard

cd magento-coding-standard

Step [4.1] – Once go inside magento-coding-standard, Run below command

cd vendor\bin
phpcs -i

The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, Magento2, Magento2Framework and PHPCompatibility

Finally Magento 2.x Coding Standard has been installed successfully.

Step [5] – Run Below command to check either Installed Magento 2 Coding Standard proper working or not

phpcs --standard=Magento2 D:/xampp/htdocs/VendorName/ModuleName or Particular File Path

Once running phpcs command Solution as
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
Everytime once running phpcs after that need to run below command always
vendor/bin/phpcbf –standard=Magento2 D:/xampp/htdocs/HNI/yourcustomModule or particular file
Once running above command, minor issue (Line indented Issue, Space Issue, Whitespace
Issue, End of Line Issue etc) auto resolved
Again need to run below command & resolve issue according instruction
vendor/bin/phpcs –standard=Magento2 D:/xampp/htdocs/HNI/yourcustomModule or particular file

In Magento 2.4.x or Adobe Commerce 2.4.x — Magento 2.x Coding Standard already inbuilt just need to run CLI command as below

Syntax::

AdobeCommerce Root-Directory vendor/bin/phpcbf --standard=Magento2 app/code/John/Quicksupport
root@fad926ccdc9b:/var/www/html/wwwroot/magento246# vendor/bin/phpcs --standard=Magento2 app/code/John/Mage2db
root@fad926ccdc9b:/var/www/html/wwwroot/magento246# vendor/bin/phpcbf --standard=Magento2 app/code/John/Mage2db

Magento 2.x Never Allow The Direct Use of The ObjectManager Code

Leave a Reply

Your email address will not be published. Required fields are marked *