Use parsekit instead. php -l sucks.
[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();
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 public function execute() {
37 if( !function_exists( 'parsekit_compile_file' ) ) {
38 $this->error( 'Requires PHP with parsekit', true );
39 }
40
41 $this->output( "Building file list..." );
42 $this->buildFileList();
43 $this->output( "done.\n" );
44
45 $this->output( "Checking syntax (this can take a really long time)...\n\n" );
46 foreach( $this->mFiles as $f ) {
47 $this->checkFile( $f );
48 }
49 $this->output( "\nDone! " . count( $this->mFiles ) . " files checked, " .
50 count( $this->mFailures ) . " failures found" );
51 }
52
53 /**
54 * Build the list of files we'll check for syntax errors
55 */
56 private function buildFileList() {
57 global $IP;
58
59 // Only check files in these directories.
60 // Don't just put $IP, because the recursive dir thingie goes into all subdirs
61 $dirs = array(
62 $IP . '/includes',
63 $IP . '/config',
64 $IP . '/languages',
65 $IP . '/maintenance',
66 $IP . '/skins',
67 );
68 if( $this->hasOption( 'with-extensions' ) ) {
69 $dirs[] = $IP . '/extensions';
70 }
71
72 foreach( $dirs as $d ) {
73 $iterator = new RecursiveIteratorIterator(
74 new RecursiveDirectoryIterator( $d ),
75 RecursiveIteratorIterator::SELF_FIRST
76 );
77 foreach ( $iterator as $file ) {
78 $ext = pathinfo( $file->getFilename(), PATHINFO_EXTENSION );
79 if ( $ext == 'php' || $ext == 'inc' || $ext == 'php5' ) {
80 $this->mFiles[] = $file->getRealPath();
81 }
82 }
83 }
84 }
85
86 /**
87 * Check a file for syntax errors. Shamelessly stolen
88 * from tools/lint.php by TimStarling
89 *
90 * @param $file String Path to a file to check for syntax errors
91 */
92 private function checkFile( $file ) {
93 static $okErrors = array(
94 'Redefining already defined constructor',
95 'Assigning the return value of new by reference is deprecated',
96 );
97 $errors = array();
98 parsekit_compile_file( $file, $errors, PARSEKIT_SIMPLE );
99 $ret = true;
100 if ( $errors ) {
101 foreach ( $errors as $error ) {
102 foreach ( $okErrors as $okError ) {
103 if ( substr( $error['errstr'], 0, strlen( $okError ) ) == $okError ) {
104 continue 2;
105 }
106 }
107 $ret = false;
108 $this->output( "Error in $file line {$error['lineno']}: {$error['errstr']}\n" );
109 }
110 $this->mFailures[ $file ] = $errors;
111 }
112 return $ret;
113 }
114 }
115
116 $maintClass = "SyntaxChecker";
117 require_once( DO_MAINTENANCE );