Now syntaxChecker.php checks for typical coding errors such as BOMSs and trailing ?>
[lhc/web/wiklou.git] / maintenance / syntaxChecker.php
1 <?php
2 /**
3 * Check syntax of all PHP files in MediaWiki
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class SyntaxChecker extends Maintenance {
26
27 // List of files we're going to check
28 private $mFiles, $mFailures = array(), $mWarnings = array();
29
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "Check syntax for all PHP files in MediaWiki";
33 $this->addOption( 'with-extensions', 'Also recurse the extensions folder' );
34 }
35
36 protected function getDbType() {
37 return Maintenance::DB_NONE;
38 }
39
40 public function execute() {
41 $this->output( "Building file list..." );
42 $this->buildFileList();
43 $this->output( "done.\n" );
44
45 // ParseKit is broken on PHP 5.3+, disabled until this is fixed
46 $useParseKit = function_exists( 'parsekit_compile_file' ) && version_compare( PHP_VERSION, '5.3', '<' );
47
48 $this->output( "Checking syntax (this can take a really long time)...\n\n" );
49 foreach( $this->mFiles as $f ) {
50 if( $useParseKit ) {
51 $this->checkFileWithParsekit( $f );
52 } else {
53 $this->checkFileWithCli( $f );
54 }
55 $this->checkForMistakes( $f );
56 }
57 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
58 count( $this->mFailures ) . " failures and " . count( $this->mWarnings ) .
59 " warnings found\n" );
60 }
61
62 /**
63 * Build the list of files we'll check for syntax errors
64 */
65 private function buildFileList() {
66 global $IP;
67
68 // Only check files in these directories.
69 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
70 $dirs = array(
71 $IP . '/includes',
72 $IP . '/config',
73 $IP . '/languages',
74 $IP . '/maintenance',
75 $IP . '/skins',
76 );
77 if( $this->hasOption( 'with-extensions' ) ) {
78 $dirs[] = $IP . '/extensions';
79 }
80
81 foreach( $dirs as $d ) {
82 $iterator = new RecursiveIteratorIterator(
83 new RecursiveDirectoryIterator( $d ),
84 RecursiveIteratorIterator::SELF_FIRST
85 );
86 foreach ( $iterator as $file ) {
87 $ext = pathinfo( $file->getFilename(), PATHINFO_EXTENSION );
88 if ( $ext == 'php' || $ext == 'inc' || $ext == 'php5' ) {
89 $this->mFiles[] = $file->getRealPath();
90 }
91 }
92 }
93 }
94
95 /**
96 * Check a file for syntax errors using Parsekit. Shamelessly stolen
97 * from tools/lint.php by TimStarling
98 * @param $file String Path to a file to check for syntax errors
99 * @return boolean
100 */
101 private function checkFileWithParsekit( $file ) {
102 static $okErrors = array(
103 'Redefining already defined constructor',
104 'Assigning the return value of new by reference is deprecated',
105 );
106 $errors = array();
107 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
108 $ret = true;
109 if ( $errors ) {
110 foreach ( $errors as $error ) {
111 foreach ( $okErrors as $okError ) {
112 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
113 continue 2;
114 }
115 }
116 $ret = false;
117 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
118 $this->mFailures[$file] = $errors;
119 }
120 }
121 return $ret;
122 }
123
124 /**
125 * Check a file for syntax errors using php -l
126 * @param $file String Path to a file to check for syntax errors
127 * @return boolean
128 */
129 private function checkFileWithCli( $file ) {
130 $res = exec( 'php -l ' . wfEscapeShellArg( $file ) );
131 if( strpos( $res, 'No syntax errors detected' ) === false ) {
132 $this->mFailures[$file] = $res;
133 $this->output( $res . "\n" );
134 return false;
135 }
136 return true;
137 }
138
139 /**
140 * Check a file for non-fatal coding errors, such as byte-order marks in the beginning
141 * or pointless ?> closing tags at the end.
142 *
143 * @param $file String String Path to a file to check for errors
144 * @return boolean
145 */
146 private function checkForMistakes( $file ) {
147 $text = file_get_contents( $file );
148
149 $this->checkRegex( $file, $text, '/^[\s\r\n]+<\?/', 'leading whitespace' );
150 $this->checkRegex( $file, $text, '/\?>[\s\r\n]*$/', 'trailing ?>' );
151 $this->checkRegex( $file, $text, '/^[\xFF\xFE\xEF]/', 'byte-order mark' );
152 }
153
154 private function checkRegex( $file, $text, $regex, $desc ) {
155 if ( !preg_match( $regex, $text ) ) {
156 return;
157 }
158
159 if ( !isset( $this->mWarnings[$file] ) ) {
160 $this->mWarnings[$file] = array();
161 }
162 $this->mWarnings[$file][] = $desc;
163 $this->output( "Warning in file $file: $desc found.\n" );
164 }
165 }
166
167 $maintClass = "SyntaxChecker";
168 require_once( DO_MAINTENANCE );
169