PHP Classes

What You Should Do Before Pushing PHP Code to your Production GIT Repository

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog What You Should Do Be...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)  

Author:

Viewers: 146

Last month viewers: 9

Categories: PHP Tutorials

Every time you commit new code to a Git repository, there is a great chance that the code has problems that were not detected with your usual batch of tests.

Read this article to learn how to integrate PHP Code Sniffer with your project Git commit process to inspect your PHP code automatically and prevent that code with problems is committed.




Loaded Article

Contents

Introduction

How to Avoid the Commiting Code with Problems?

Setup a Git repository to use PHP Code Sniffer

Integrate PHP Code Sniffer with Git

Conclusion


Introduction

I assume that you are already using Git as version control system for PHP projects.

It is true that sometimes we make spelling mistakes when typing code which may cause a warning or a fatal error. Usually those errors would be detected with tests, but not all errors are detected by the batch of tests that we perform.

Pushing the code that has errors to a production server may cause some serious problems. Even committing the same code to your Git repository may result in inconsistent git logs, for example when a commit was made just to fix typos.

How to Avoid the Commiting Code with Problems?

In this article I propose using for instance the PHP CodeSniffer tool to inspect our code and show the warnings, errors or deprecated function usage before we commit the changes to git repository. This way the good coding practices become easier to follow.

Setup a Git repository to use PHP Code Sniffer

To setup PHP Code Sniffer you need to PEAR and the PHP CodeSniffer package itself.

Install the PEAR Package Manager

Check if PEAR is installed on your system by running “pear version” on your terminal. If that command fails, you need to install PEAR on your system.

On Unix / Linux based distros

$ wget http://pear.php.net/go-pear.phar  
$ php go-pear.phar

On Mac OS X

$ wget http://pear.php.net/go-pear.phar
$ php -d detect_unicode=0 go-pear.phar

or

$ curl -O http://pear.php.net/go-pear.phar 
$ php -d detect_unicode=0 go-pear.phar

Install PHP Code Sniffer

Use PEAR to install PHP_CodeSniffer package, which will be used run tests on our PHP code and checks for errors and code standards.

$ pear install PHP_CodeSniffer

Integrate PHP Code Sniffer with Git

Lets configure the Git repository to run PHP code sniffer on every git commit. Change to your PHP project directory and create a “pre-commit” file under .git/hooks

$ cd /path/to/project
$ vi .git/hooks/pre-commit

Copy the script below and save it the file.

#!/bin/sh

PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`

# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
    oIFS=$IFS
    IFS='
    '
    SFILES="$1"
    IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}

echo "Checking PHP Lint..."
for FILE in $SFILES
do
    php -l -d display_errors=0 $PROJECT/$FILE
    if [ $? != 0 ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
    FILES="$FILES $PROJECT/$FILE"
done

if [ "$FILES" != "" ]
then
    echo "Running Code Sniffer..."
    ./vendor/bin/phpcs --standard=PSR1 --encoding=utf-8 -n -p $FILES
    if [ $? != 0 ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
fi

exit $?

It is very important to make the pre-commit script file executable so it can be run before actually commiting the files.

$ chmod +x .git/hooks/pre-commit

Conclusion

Using PHP Code Sniffer to check our PHP code changes before commiting to a Git repository it is easy once you figure how to create a pre-commit file. The same process could be used to add other code checks using other tools.

If you liked this article or you have a question about how to setup this automatic code quality process, post a comment to this article here.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

2. Git pre-commit on Windows - Stephan Krauß (2015-07-30 21:27)
Git pre-commit on Windows... - 1 reply
Read the whole comment and replies

3. Wonderful article in the style of a good coding practice - Shilov Gennadiy (2015-07-30 21:19)
Good idea... - 1 reply
Read the whole comment and replies

1. Good idea - Stephan (2015-07-30 21:16)
Good idea, got it working... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog What You Should Do Be...   Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)