small PHP check before upload or test

In a world where Bash shell is under attack I attach a short shell script that make me waste less time in upload/commit/test loop:

#! /bin/sh

LIST=`find . -name '*.php'`
for  x in $LIST; do
    OUTPUT=`php -l $x`
    A=$?
    if [ $A -ne 0 ]; then
	echo "ERROR ON " $x "with " $OUTPUT;
    fi
done

simple enough as running

./php-lint.sh

on a git repository, do not traverse all tree but just the changed files, and launch just before commit the script

#! /bin/sh

LIST=`git diff --name-only |grep 'php$'`
for  x in $LIST; do
    OUTPUT=`php -l $x`
    A=$?
    if [ $A -ne 0 ]; then
	echo "ERROR ON " $x "with " $OUTPUT;
    fi
done

Or add it as .git/hooks/pre-commit code:

#!/bin/sh

ERRORS=0
LIST=`git diff --name-only | grep 'php$'`
for  x in $LIST; do
    OUTPUT=`php -l $x`
    A=$?
    if [ $A -ne 0 ]; then
	echo "ERROR ON " $x "with " $OUTPUT;
	ERRORS=1
    fi
done

if [ $ERRORS -ne 0 ]; then
   exit 1
fi

exit 0

(and use ‘git commit’ before committing changes).

It is the simpliest statical analisys, the syntax check, but It save enough time for me


Posted

in

,

by

Tags: