To use the Magento coding standards within an existing Magento 2 project, install the package as a development dependency using Composer.
Note: For security reasons, the standard is not added to PHP_CodeSniffer's installed paths automatically. You must manually configure your composer.json to register the paths during installation or updates.
composer require --dev magento/magento-coding-standard
Add this to your composer.json to automate path registration:
"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/)
]
}