Skip to content
jrfnl edited this page Aug 20, 2023 · 2 revisions

You can run PHPCS automatically whenever a new commit is pushed to your repository using Travis CI, just follow these steps.

Provided that:

  1. You are installing WordPressCS using Composer and can run it locally using vendor/bin/phpcs.
  2. ... and you have a [.]phpcs.xml[.dist] file in your project root directory. (If not, add --standard=path/to/yourrulesetfile.xml to the PHPCS command below).
  3. Create the file .travis.yml and add the below yaml script to it.

Note: If you use a matrix setup in Travis to test your code against different PHP and/or WordPress versions, you don't need to run PHPCS on each variant of the matrix as the results will be same. You can set an environment variable in the Travis matrix to only run the sniffs against one setup in the matrix.

Travis CI example

language: php

jobs:
  include:
    # Arbitrary PHP version to run the sniffs against.
    - php: 8.1
      env: SNIFF=1

cache:
  directories:
    - $HOME/.composer/cache

before_install:
  # Turn off Xdebug for performance reasons.
  - phpenv config-rm xdebug.ini || echo 'No xdebug config.'

install:
  # Install WordPressCS via Composer.
  - if [[ "$SNIFF" == "1" ]]; then travis_retry composer install --no-interaction; fi
  - if [[ "$SNIFF" == "1" ]]; then phpenv rehash; fi

script:
  # Run against WordPress Coding Standards.
  # You can use any of the normal PHPCS command line arguments in the command:
  # https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage
  - if [[ "$SNIFF" == "1" ]]; then vendor/bin/phpcs -p . --standard=WordPress; fi