Skip to content

Instantly share code, notes, and snippets.

@franxo
Last active March 29, 2019 08:48
Show Gist options
  • Save franxo/0af0276d34af481d58443f081bb47918 to your computer and use it in GitHub Desktop.
Save franxo/0af0276d34af481d58443f081bb47918 to your computer and use it in GitHub Desktop.
Installing Coder Sniffer & Drupal Coder

Installing Coder Sniffer & Drupal Coder

  • Install PHP CodeSniffer

    $ composer global require/squizlabs/php_codesniffer
    
  • Install Coder

    $ composer global require drupal/coder
    
  • Register Drupal standards in PHP Codesniffer

    $ composer global require dealerdirect/phpcodesniffer-composer-installer
    
  • Configure PHPStorm

    Menu > Edit > settings
    Preferences, Languages & Frameworks > PHP > Quality Tools > Code Sniffer
    Configuration: ~/.composer/vendor/bin/phpcs
    
    Menu > Edit > settings
    Preferences, Editor > Inspections > PHP > Quality tools > PHP Code Sniffer validation > Coding standard
    Coding Standar: Drupal
    
    *** To set multiple Coding Standars
    Open the following file: .idea/inspectionProfiles/Project_Default.xml
    Find CODING_STANDARD option and change it for:
    <option name="CODING_STANDARD" value="Drupal,DrupalPractice" />
    
  • Run the sniffs

    • Check Drupal coding standards

      phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /path/to/drupal/example_module
      
    • Check Drupal best practices

      phpcs --standard=DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /path/to/drupal/example_module       
      
    • Automatically fix coding standards

      phpcbf --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /path/to/drupal/example_module       
      
  • Setting up aliases for scripts

    • Open .bashrc

      vim ~/.bashrc
      
    • At the bottom of file add

      alias drupalcs="phpcs --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md'"
      alias drupalcsp="phpcs --standard=DrupalPractice --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md'"
      alias drupalcbf="phpcbf --standard=Drupal --extensions='php,module,inc,install,test,profile,theme,css,info,txt,md'"
      
    • Re-run .bashrc file so it picks up your new alias.

      source ~/.bashrc 
      
    • Use the aliases:

      $ drupalcs sites/all/modules/mymodule
      $ drupalcsp sites/all/modules/mymodule
      $ drupalcbf sites/all/modules/mymodule
      

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment