Filed Under (Coding, debugging) by steve on 17-05-2009

I recently started using PHP’s built-in syntax checker on a more regular basis - it cuts down that whole eyeball-ing routine where you think you know that there is a missing semi-colon, and you insist on searching for it all over the place, or you’re confident you can find the extra closing parentheiss, and you waste time scanning the page.

I set up two tools (basically launchers) in UltraEdit to launch php -l (small L).

One of them to syntax-check 1.) the Active File, (if you’re not setting this up in UltraEdit, which I assume the majority, just basically just call php -l filename.php)

check single file



2.) Multiple files (all of the .php files in a single directory)

check multiple files

The looplint (i.e. loop over all files and apply lint to them) file is as follows:

<?php
$allowedtypes = array("php");
$filerun = outputfiles ($allowedtypes, ".");
foreach ($filerun as $f)
    system("php -l " . $f)

function outputfiles ($allowedtypes, $thedir){
	$file_array = array();
    //First, we ensure that the directory exists.
    if (is_dir ($thedir)){

	  //Now, we scan the files in this directory using scandir.
	  $scanarray = scandir ($thedir);

	  //Then we begin parsing the array.
	  //Since scandir() counts the "." and ".." file navigation listings
	  //as files, we should not list them.
	  for ($i = 0; $i < count ($scanarray); $i++){
		if ($scanarray[$i] != "." && $scanarray[$i] != ".."){

		  //Now, we check to make sure this is a file, and not a directory.
		  if (is_file ($thedir . "/" . $scanarray[$i])){

			//Now, since we are going to allow the client to edit this file,
		    //we must check if it is read and writable.
		    if (is_writable ($thedir. "/" . $scanarray[$i]) && is_readable($thedir . "/" . $scanarray[$i])){

			  //Now, we check to see if the file type exists within our allowed type array.
			  $thepath = pathinfo ($thedir . "/" . $scanarray[$i]);
			  if (in_array ($thepath['extension'], $allowedtypes)){

				//If the file follows our stipulations, then we can proceed to output it.
				$file_array[] = $scanarray[$i];

			  }
		    }
	      }
	    }
	  }
	    return $file_array;
    } else {
	  echo "Sorry, no files matching those types.";
    }
  }
?>

A couple of notes about this second one (lint-ing multiple files)

1.) To simply run it without this Ultraedit business above, it would obviously be:
A. ( first go into the directory containing the php files you want to apply the lint to)
B. php looplint.php

2.)
The outputfiles function is largely a function I saw in the excellent book, PHP Recipes



Post a comment
Name: 
Email: 
URL: 
Comments: