note 82704 added to function.php-check-syntax

From: Date: Tue, 22 Apr 2008 19:43:35 +0000
Subject: note 82704 added to function.php-check-syntax
Groups: php.notes 
Request: Send a blank email to php-notes+get-138664@lists.php.net to get a copy of this message
alx at inbox dot lv says: "Nope guys.. to check the valid php you must do the followed..." Actually that will fail when anything fatal occurs because the code will actually be executed! :) nicolas dot grekas+php at gmail dot com says: "I can't find a way to break the dead code sandbox. Who can ?" Why all the complications just to protect braces? Further, unbalanced braces is something we want to syntaxually check isn't it? Let's make this useful :) Here's what I've discovered to use without any issues over the years. From nicolas and alx's examples I remodeled it a little bit to hopefully catch any case now. By all means tell me if it is broken in some way :) Remember though that it is ONLY a syntax check! P.S. I couldn't seem to get this to work properly in CLI interactive even with the function below as separate file include. It appears that the CLI always outputs and that OB doesn't get buffred. Makes no sense... <?php /** * Check Eval * Performs a Syntax check without killing the parser (hopefully) * * @params string PHP to be evaluated * @return array Parse error info or true for success **/ function checkEval( $php ) { $evalStr = 'return;'; # Get the string tokens $crack = token_get_all( $php ); # Parser tokens to check for # we are only checking the first token because anything else # would result in an expected syntax error $tokens = array( T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO ); # Check to see how we need to proceed # prepare the string for parsing if( isset( $crack[0] ) && in_array( $crack[0][0], $tokens, true ) ) $evalStr .= '?>'; # Make sure syntax reporting is on...probably only need E_PARSE...but I'm anal $oldErr = error_reporting( E_ALL | E_STRICT ); # Prevent output ob_start(); $test = eval( $evalStr.$php ); $evaluput = ob_get_clean(); # Reset error reporting error_reporting( $oldErr ); # Only do this if there was output if( $evaluput !== '' ) { # Strip annoying html tags, in case html reporting is enabled in php.ini $evaluput = strip_tags( $evaluput ); # Fancy Regex to pull the error that killed us $pattern = '/Parse error:\s*syntax error,(.+?)\s+in\s+.+?\s*line\s+(\d+)/'; $parse_fail = (bool)preg_match( $pattern, $evaluput, $match ); # Parse error to report? if( $parse_fail === true ) { return array( 'line' => (int)$match[2], 'msg' => $match[1] ); } } return true; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 76.22.73.161 ---- Manual Page -- http://www.php.net/manual/en/function.php-check-syntax.php Edit -- https://master.php.net/note/edit/82704 Del: integrated -- https://master.php.net/note/delete/82704/integrated Del: useless -- https://master.php.net/note/delete/82704/useless Del: bad code -- https://master.php.net/note/delete/82704/bad+code Del: spam -- https://master.php.net/note/delete/82704/spam Del: non-english -- https://master.php.net/note/delete/82704/non-english Del: in docs -- https://master.php.net/note/delete/82704/in+docs Del: other reasons-- https://master.php.net/note/delete/82704 Reject -- https://master.php.net/note/reject/82704 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138664) next »