Full-service Internet Marketing & Web Development
Recent Posts

Sponsors
![]() |
Realtime PHP Syntax CheckingMike Peters, 04-14-2010 |
If you love Agile Development and PHP as much as we do, you're probably going to have developers editing critical PHP scripts on live machines.
While many PHP editors include built-in syntax checking, it doesn't hurt to have a fail-safe in place. We wanted to come up with a way to apply php -l SCRIPT syntax checking on any PHP script as soon as it is updated.
At first the idea was to write a plugin for Mercurial - our version control system, so that whenever a changeset is pushed, the syntax will be checked.
Unfortunately this doesn't cover changes done via SSH or files uploaded via FTP by clients.
We came up with a shell script, executed by cron every minute, that iterates through all files changed within the last 3 days, applying "php -l" on those files and sending an email to the appropriate developer with the information about the error.
Regardless of how files are updated, we now have an automated way to syntax-check every single change to a PHP script.
The emails generated by the script follow this format:
CheckSyntax cronjob:
CheckSyntax.sh:
-
To install, save the two files (CheckSyntax and CheckSyntax.sh), chmod a+rx, update the email settings in CheckSyntax and add this line to your cronjob:
While many PHP editors include built-in syntax checking, it doesn't hurt to have a fail-safe in place. We wanted to come up with a way to apply php -l SCRIPT syntax checking on any PHP script as soon as it is updated.
At first the idea was to write a plugin for Mercurial - our version control system, so that whenever a changeset is pushed, the syntax will be checked.
Unfortunately this doesn't cover changes done via SSH or files uploaded via FTP by clients.
We came up with a shell script, executed by cron every minute, that iterates through all files changed within the last 3 days, applying "php -l" on those files and sending an email to the appropriate developer with the information about the error.
Regardless of how files are updated, we now have an automated way to syntax-check every single change to a PHP script.
The emails generated by the script follow this format:
Oops! Found a FATAL syntax error in a production PHP script...
DIRECTORY: /magnetic/
---------------------------------------------
FILE: ./affiliate/offline_credit_commission.php
---------------------------------------------
Parse error: syntax error, unexpected '<' in /offline_credit_commission.php on line 8
changeset: 2673:fe54a1e4dbe0
user: Doug
date: Wed Apr 14 00:29:26 2010 +0300
summary: Change offline credit affiliate commission
DIRECTORY: /magnetic/
---------------------------------------------
FILE: ./affiliate/offline_credit_commission.php
---------------------------------------------
Parse error: syntax error, unexpected '<' in /offline_credit_commission.php on line 8
changeset: 2673:fe54a1e4dbe0
user: Doug
date: Wed Apr 14 00:29:26 2010 +0300
summary: Change offline credit affiliate commission
CheckSyntax cronjob:
#!/bin/sh
# Change to source directory directory
cd $(dirname $0)
# Generate report
./CheckSyntax.sh >log.$$
a=$?
# Email report
# TODO: Update these with your settings
if [ $a = 1 ]
then
./sendEmail -f CheckSyntax@mycompany.com \
-t developers@mycompany.com \
-u "Check Syntax" \
-s mail.mymailserver.com \
-m < log.$$
fi
# Remove temporary file
rm -rf log.$$
# (eof)
# Change to source directory directory
cd $(dirname $0)
# Generate report
./CheckSyntax.sh >log.$$
a=$?
# Email report
# TODO: Update these with your settings
if [ $a = 1 ]
then
./sendEmail -f CheckSyntax@mycompany.com \
-t developers@mycompany.com \
-u "Check Syntax" \
-s mail.mymailserver.com \
-m < log.$$
fi
# Remove temporary file
rm -rf log.$$
# (eof)
CheckSyntax.sh:
#!/usr/local/bin/bash
##################################
# Config
##################################
#
# TODO: Update these with your settings
php="/usr/local/bin/php"
hg="/usr/local/bin/hg"
def="/magnetic"
#
##################################
# Directory passed in parameters?
if [ "$1" != "" ]
then
# Get the directory from parameters
dir=$1
else
# No parameter, use default
dir=$def
fi
# Change to the directory we need to check
cd $dir
# Prepare tmp file
tmp=/tmp/CheckSyntax.$$
# Header
echo "Oops! Found a FATAL syntax error in a production PHP script..."
echo ""
echo "DIRECTORY: $dir"
# Find PHP files
# Note: '-newerct' only workds under FreeBSD
find . -newerct '3 days ago' ! -path "*/temp/*" | grep "\.php$" >$tmp
# Loop files
cnt=0
err=0
tot=0
while read i
do
# If file isn't a file, skip
[ ! -f "$i" ] && continue;
((tot++))
# Syntax check
a=$($php -l $i | grep "^No syntax errors detected in")
# Is it OK?
if [ "$a" = "" ]
then
# Not OK, display details
echo ""
echo "----------------------------------------------------------------------------------------------"
echo "FILE: $i"
echo "----------------------------------------------------------------------------------------------"
a=$($php -d display_errors=1 -l $i | grep "Parse error")
echo $a | sed "s/<[a-zA-Z\/][^>]*>//g"
$hg log $i | head -n 5 | grep -v "^$"
((cnt++))
err=1
fi
done < $tmp
echo ""
echo "--"
echo ""
echo "$cnt BAD FILES"
echo "$tot TOTAL FILES"
echo ""
# Remove tmp file
rm -rf $tmp
# 0 - no error | 1 - error
exit $err
# (eof)
##################################
# Config
##################################
#
# TODO: Update these with your settings
php="/usr/local/bin/php"
hg="/usr/local/bin/hg"
def="/magnetic"
#
##################################
# Directory passed in parameters?
if [ "$1" != "" ]
then
# Get the directory from parameters
dir=$1
else
# No parameter, use default
dir=$def
fi
# Change to the directory we need to check
cd $dir
# Prepare tmp file
tmp=/tmp/CheckSyntax.$$
# Header
echo "Oops! Found a FATAL syntax error in a production PHP script..."
echo ""
echo "DIRECTORY: $dir"
# Find PHP files
# Note: '-newerct' only workds under FreeBSD
find . -newerct '3 days ago' ! -path "*/temp/*" | grep "\.php$" >$tmp
# Loop files
cnt=0
err=0
tot=0
while read i
do
# If file isn't a file, skip
[ ! -f "$i" ] && continue;
((tot++))
# Syntax check
a=$($php -l $i | grep "^No syntax errors detected in")
# Is it OK?
if [ "$a" = "" ]
then
# Not OK, display details
echo ""
echo "----------------------------------------------------------------------------------------------"
echo "FILE: $i"
echo "----------------------------------------------------------------------------------------------"
a=$($php -d display_errors=1 -l $i | grep "Parse error")
echo $a | sed "s/<[a-zA-Z\/][^>]*>//g"
$hg log $i | head -n 5 | grep -v "^$"
((cnt++))
err=1
fi
done < $tmp
echo ""
echo "--"
echo ""
echo "$cnt BAD FILES"
echo "$tot TOTAL FILES"
echo ""
# Remove tmp file
rm -rf $tmp
# 0 - no error | 1 - error
exit $err
# (eof)
-
To install, save the two files (CheckSyntax and CheckSyntax.sh), chmod a+rx, update the email settings in CheckSyntax and add this line to your cronjob:
# CheckSyntax
# TODO: Update path with the location of CheckSyntax
*/1 * * * * /home/CheckSyntax
# TODO: Update path with the location of CheckSyntax
*/1 * * * * /home/CheckSyntax
|
|
Subscribe Now to receive new posts via Email as soon as they come out.
Comments
Post your comments

