Use parsekit instead. php -l sucks.
authorChad Horohoe <demon@users.mediawiki.org>
Sat, 8 Aug 2009 23:13:03 +0000 (23:13 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Sat, 8 Aug 2009 23:13:03 +0000 (23:13 +0000)
maintenance/syntaxChecker.php

index addc457..fa68e49 100644 (file)
@@ -34,12 +34,20 @@ class SyntaxChecker extends Maintenance {
        }
 
        public function execute() {
+               if( !function_exists( 'parsekit_compile_file' ) ) {
+                       $this->error( 'Requires PHP with parsekit', true );
+               }
+
                $this->output( "Building file list..." );
                $this->buildFileList();
                $this->output( "done.\n" );
 
                $this->output( "Checking syntax (this can take a really long time)...\n\n" );
-               $res = $this->checkSyntax();
+               foreach( $this->mFiles as $f ) {
+                       $this->checkFile( $f );
+               }
+               $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
+                       count( $this->mFailures ) . " failures found" );
        }
 
        /**
@@ -76,18 +84,32 @@ class SyntaxChecker extends Maintenance {
        }
 
        /**
-        * Check the files for syntax errors
+        * Check a file for syntax errors. Shamelessly stolen
+        * from tools/lint.php by TimStarling
+        *
+        * @param $file String Path to a file to check for syntax errors
         */
-       private function checkSyntax() {
-               foreach( $this->mFiles as $f ) {
-                       $res = exec( 'php -l ' . $f ); 
-                       if( strpos( $res, 'No syntax errors detected' ) === false ) {
-                               $this->mFailures[] = $f;
-                               $this->error( $res . "\n" );
+       private function checkFile( $file ) {
+               static $okErrors = array(
+                       'Redefining already defined constructor',
+                       'Assigning the return value of new by reference is deprecated',
+               );
+               $errors = array();
+               parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
+               $ret = true;
+               if ( $errors ) {
+                       foreach ( $errors as $error ) {
+                               foreach ( $okErrors as $okError ) {
+                                       if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
+                                               continue 2;
+                                       }
+                               }
+                               $ret = false;
+                               $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
                        }
+                       $this->mFailures[ $file ] = $errors;
                }
-               $this->output( count($this->mFiles) . " files checked, " 
-                       . count($this->mFailures) . " failures\n" );
+               return $ret;
        }
 }