Skip to content

Instantly share code, notes, and snippets.

@devdrops
Last active March 13, 2017 19:37
Show Gist options
  • Select an option

  • Save devdrops/4bdb5791d7dc20fbbb178dca08c6b9a4 to your computer and use it in GitHub Desktop.

Select an option

Save devdrops/4bdb5791d7dc20fbbb178dca08c6b9a4 to your computer and use it in GitHub Desktop.
Run PHP CodeSniffer for all changed/new files on Git, using Docker

Run PHP CodeSniffer for all changed/new files on Git, using Docker

  • The -a flag instructs phpcs to execute interactively, so then you can edit the files and re-check it again with no need to run the same phpcs command again. Awesome, eh?
  • The Git part:
    • git status --porcelain will show us all files and their state: ?? for a new one, M for modified;
    • We combine egrep and cut to give us exactly what we want, a perfect list.
    • We put this as a subcommand to pass the expected file list to phpcs.
  • Docker here is a mere tool. You can enjoy the same functionality with or without it 😉
docker run -it --rm \
  -v $(pwd):/mycode \
  -w /mycode \
  devdrops/php-toolbox \
  phpcs -a --standard=PSR2 \
  $(git status --porcelain | egrep "^(\ M|\?\?)" | cut -c 4-)
@devdrops
Copy link
Copy Markdown
Author

devdrops commented Mar 13, 2017

FIX

git status --porcelain | egrep "^[ M?]" | cut -c 4- will also provide the deleted files, which will break phpcs execution. Instead, use git status --porcelain | egrep "^(\ M|\?\?)" | cut -c 4- to avoid those deleted files.

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