Call us Toll-Free:
1-800-218-1525
Email us

 Sponsors

Realtime PHP Syntax Checking

Mike 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:

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

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)

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)

-

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

rainey, 01-07-2011
This line:
//send email to Infra
if (isset($msgToInfra)) {
mail($msgToInfra['to'], $msgToInfra['subject'], $msgToInfra['message'], $msgToInfra['headers'], "-f infosecautocreate2@standard.com");
}

Is giving me this error:
Parse error: syntax error, unexpected T_IF in /intranet/home-docs/business/information-security/incident-report-submit.php on line 209

My php guy is busy...can you help me fix the code?
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions