“Learn to install and utilize PHP CodeSniffer (PHPCS) and Fixer (PHPCBF) on Ubuntu for Drupal projects, ensuring coding standards adherence.”
Installation Code:
# Install Composer globally
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
# Install PHP CodeSniffer and Fixer globally
composer global require "squizlabs/php_codesniffer=*"
composer global require "drupal/coder"
# Add Composer global bin directory to the system PATH
echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc
source ~/.bashrcImplementation for Drupal:
# Run PHP CodeSniffer for Drupal coding standards
phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,js,info,txt,md,yml,twig module_folder/
# Run PHP CodeFixer to automatically fix some coding standard violations
phpcbf --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,js,info,txt,md,yml,twig module_folder/Ensure your Drupal module adheres to the Drupal coding standards effortlessly with these commands.
Summary:
composer global require drupal/coder
phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
composer global update
phpcs --standard=Drupal,DrupalPractice src/Form/CustomController.php
// autofix phpcs error with phpcbf:
composer global require squizlabs/php_codesniffer
phpcbf --standard=Drupal,DrupalPractice src/Form/CustomController.php
phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml module_name/
phpcbf --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml module_name/