Index: trunk/phase3/maintenance/syntaxChecker.php |
— | — | @@ -25,7 +25,7 @@ |
26 | 26 | class SyntaxChecker extends Maintenance { |
27 | 27 | |
28 | 28 | // List of files we're going to check |
29 | | - private $mFiles, $mFailures = array(); |
| 29 | + private $mFiles, $mFailures = array(), $mWarnings = array(); |
30 | 30 | |
31 | 31 | public function __construct() { |
32 | 32 | parent::__construct(); |
— | — | @@ -52,9 +52,11 @@ |
53 | 53 | } else { |
54 | 54 | $this->checkFileWithCli( $f ); |
55 | 55 | } |
| 56 | + $this->checkForMistakes( $f ); |
56 | 57 | } |
57 | 58 | $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " . |
58 | | - count( $this->mFailures ) . " failures found\n" ); |
| 59 | + count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) . |
| 60 | + " warnings found\n" ); |
59 | 61 | } |
60 | 62 | |
61 | 63 | /** |
— | — | @@ -133,6 +135,33 @@ |
134 | 136 | } |
135 | 137 | return true; |
136 | 138 | } |
| 139 | + |
| 140 | + /** |
| 141 | + * Check a file for non-fatal coding errors, such as byte-order marks in the beginning |
| 142 | + * or pointless ?> closing tags at the end. |
| 143 | + * |
| 144 | + * @param $file String String Path to a file to check for errors |
| 145 | + * @return boolean |
| 146 | + */ |
| 147 | + private function checkForMistakes( $file ) { |
| 148 | + $text = file_get_contents( $file ); |
| 149 | + |
| 150 | + $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' ); |
| 151 | + $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' ); |
| 152 | + $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' ); |
| 153 | + } |
| 154 | + |
| 155 | + private function checkRegex( $file, $text, $regex, $desc ) { |
| 156 | + if ( !preg_match( $regex, $text ) ) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + if ( !isset( $this->mWarnings[$file] ) ) { |
| 161 | + $this->mWarnings[$file] = array(); |
| 162 | + } |
| 163 | + $this->mWarnings[$file][] = $desc; |
| 164 | + $this->output( "Warning in file $file: $desc found.\n" ); |
| 165 | + } |
137 | 166 | } |
138 | 167 | |
139 | 168 | $maintClass = "SyntaxChecker"; |