Now syntaxChecker.php checks for typical coding errors such as BOMSs and trailing ?>
authorMax Semenik <maxsem@users.mediawiki.org>
Fri, 13 Nov 2009 22:18:32 +0000 (22:18 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Fri, 13 Nov 2009 22:18:32 +0000 (22:18 +0000)
maintenance/syntaxChecker.php

index 90d1f87..6dd7833 100644 (file)
@@ -25,7 +25,7 @@ require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 class SyntaxChecker extends Maintenance {
 
        // List of files we're going to check
-       private $mFiles, $mFailures = array();
+       private $mFiles, $mFailures = array(), $mWarnings = array();
 
        public function __construct() {
                parent::__construct();
@@ -52,9 +52,11 @@ class SyntaxChecker extends Maintenance {
                        } else {
                                $this->checkFileWithCli( $f );
                        }
+                       $this->checkForMistakes( $f );
                }
                $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
-                       count( $this->mFailures ) . " failures found\n" );
+                       count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
+                       " warnings found\n" );
        }
 
        /**
@@ -133,6 +135,33 @@ class SyntaxChecker extends Maintenance {
                }
                return true;
        }
+
+       /**
+        * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
+        * or pointless ?> closing tags at the end.
+        *
+        * @param $file String String Path to a file to check for errors
+        * @return boolean
+        */
+       private function checkForMistakes( $file ) {
+               $text = file_get_contents( $file );
+
+               $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
+               $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
+               $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
+       }
+
+       private function checkRegex( $file, $text, $regex, $desc ) {
+               if ( !preg_match( $regex, $text ) ) {
+                       return;
+               }
+
+               if ( !isset( $this->mWarnings[$file] ) ) {
+                       $this->mWarnings[$file] = array();
+               }
+               $this->mWarnings[$file][] = $desc;
+               $this->output( "Warning in file $file: $desc found.\n" );
+       }
 }
 
 $maintClass = "SyntaxChecker";