2bnvaa
Last Updated: February 25, 2016
·
2.061K
· stevehalford
Bd22fa12d879b47c4b1a5d91eb432055

Run Code Sniffer on modified files

If you've got a big project, running

phpcs .

before each commit can be fairly time consuming. Doing a git status and running phpcs on each modified file manually is just as fiddly.

Try this...

#!/bin/bash

files=$(git ls-files -om --exclude-standard)

phpcs $files

Save it as an executable called git-cs in your PATH and typing the following will run phpcs on just the modified files...

git cs

Currently, there's no filtering of file types, but phpcs seems to do a pretty good job of ignoring files it can't sniff.

Say Thanks
Respond

5 Responses
Add your response

5601
A208c6ddf9836a1e2a32a0dab0362713

Great tip!

I found if no files had changed it would just run phpcs for the entire project, so use the following:

#!/bin/bash

files=$(git ls-files -om --exclude-standard)

if [ -z "$files" ]; then
    echo 'No files to check';
else
    phpcs $files
fi
over 1 year ago ·
5602
Bd22fa12d879b47c4b1a5d91eb432055

Thanks @jenkoian !

over 1 year ago ·
18185
None

Awesome idea! Thanks for sharing :)

over 1 year ago ·
18192
None

One more improvement. If I want to have pre-commit hook, I need to include files in the commit. git ls-files -om --exclude-standard exclude those files.

This is the solution (so far):

#!/bin/bash

files=$(git diff --name-only HEAD master);
if [ -z $files ]; then
    phpcs $files
fi
over 1 year ago ·
18193
None

Negation was missing:

#!/bin/bash

FILES=$(git diff --name-only HEAD master);
if [ ! -z "$FILES" ]; then
    phpcs $FILES
fi
over 1 year ago ·
Filed Under