Setup the PHPCS and WPCS

Advertisement

PHP_CodeSniffer along with WordPress coding standards rule-set for phpcs is used to detect violations of a defined set of coding standards.

  1. phpcs is command line tool, instead of using windows bash it is recommended to use Git bash for this tutorial.
  2. First, check if Composer is already installed in your system by typing
    composer

    in command prompt. If the composer is not installed go to getcomposer and install it.

  3. Install PHPCS by running following command in your terminal –
    composer global require squizlabs/php_codesniffer=*
  4. Install WordPress Coding Standards for PHP_CodeSniffer by command
    composer global require wp-coding-standards/wpcs
  5. Configure phpcs to use WordPress coding standards rulesets by running following command
    phpcs --config-set installed_paths /c/Users/$(whoami)/AppData/Roaming/Composer/vendor/wp-coding-standards/wpcs
  6. Now you have installed and configured phpcs and WordPress coding standards rules with phpcs.
  7. Open your project in the terminal and If you don’t have already, add the phpcs ruleset to your project.
  8. Use the following command to run phpcs check.
    phpcs --standard=phpcs.ruleset.xml $(find . -name '*.php')
  9. phpcs also comes with a beautifier which can fix simple errors automatically, run this with a command –
    phpcbf --standard=phpcs.ruleset.xml $(find . -name '*.php')

    # Generate Log File: # Generate Log File:

    Syntax:

    phpcs --report-full=/path/to/full.log --report-summary=/path/to/summary.log /path/to/code

    Use:

    phpcs --report-full=phpcs-full.log --report-summary=phpcs-summary.log $(find . -name '*.php')

    —–

    It’ll generate phpcs-full.log & phpcs-summary.log files.

%d bloggers like this: