Merge "getConfiguration: Don't bail when a valid variable is set null"
[lhc/web/wiklou.git] / maintenance / findDeprecated.php
1 <?php
2 /**
3 * Maintenance script that recursively scans MediaWiki's PHP source tree
4 * for deprecated functions and methods and pretty-prints the results.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26 require_once __DIR__ . '/../vendor/autoload.php';
27
28 /**
29 * A PHPParser node visitor that associates each node with its file name.
30 */
31 class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract {
32 private $currentFile = null;
33
34 public function enterNode( PhpParser\Node $node ) {
35 $retVal = parent::enterNode( $node );
36 $node->filename = $this->currentFile;
37 return $retVal;
38 }
39
40 public function setCurrentFile( $filename ) {
41 $this->currentFile = $filename;
42 }
43
44 public function getCurrentFile() {
45 return $this->currentFile;
46 }
47 }
48
49 /**
50 * A PHPParser node visitor that finds deprecated functions and methods.
51 */
52 class DeprecatedInterfaceFinder extends FileAwareNodeVisitor {
53
54 private $currentClass = null;
55
56 private $foundNodes = [];
57
58 public function getFoundNodes() {
59 // Sort results by version, then by filename, then by name.
60 foreach ( $this->foundNodes as $version => &$nodes ) {
61 uasort( $nodes, function ( $a, $b ) {
62 return ( $a['filename'] . $a['name'] ) < ( $b['filename'] . $b['name'] ) ? -1 : 1;
63 } );
64 }
65 ksort( $this->foundNodes );
66 return $this->foundNodes;
67 }
68
69 /**
70 * Check whether a function or method includes a call to wfDeprecated(),
71 * indicating that it is a hard-deprecated interface.
72 * @param PhpParser\Node $node
73 * @return bool
74 */
75 public function isHardDeprecated( PhpParser\Node $node ) {
76 if ( !$node->stmts ) {
77 return false;
78 }
79 foreach ( $node->stmts as $stmt ) {
80 if (
81 $stmt instanceof PhpParser\Node\Expr\FuncCall
82 && $stmt->name->toString() === 'wfDeprecated'
83 ) {
84 return true;
85 }
86 return false;
87 }
88 }
89
90 public function enterNode( PhpParser\Node $node ) {
91 $retVal = parent::enterNode( $node );
92
93 if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
94 $this->currentClass = $node->name;
95 }
96
97 if ( $node instanceof PhpParser\Node\FunctionLike ) {
98 $docComment = $node->getDocComment();
99 if ( !$docComment ) {
100 return;
101 }
102 if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
103 return;
104 }
105 $version = $matches[1];
106
107 if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
108 $name = $this->currentClass . '::' . $node->name;
109 } else {
110 $name = $node->name;
111 }
112
113 $this->foundNodes[ $version ][] = [
114 'filename' => $node->filename,
115 'line' => $node->getLine(),
116 'name' => $name,
117 'hard' => $this->isHardDeprecated( $node ),
118 ];
119 }
120
121 return $retVal;
122 }
123 }
124
125 /**
126 * Maintenance task that recursively scans MediaWiki PHP files for deprecated
127 * functions and interfaces and produces a report.
128 */
129 class FindDeprecated extends Maintenance {
130 public function __construct() {
131 parent::__construct();
132 $this->addDescription( 'Find deprecated interfaces' );
133 }
134
135 public function getFiles() {
136 global $IP;
137
138 $files = new RecursiveDirectoryIterator( $IP . '/includes' );
139 $files = new RecursiveIteratorIterator( $files );
140 $files = new RegexIterator( $files, '/\.php$/' );
141 return iterator_to_array( $files, false );
142 }
143
144 public function execute() {
145 global $IP;
146
147 $files = $this->getFiles();
148 $chunkSize = ceil( count( $files ) / 72 );
149
150 $parser = ( new PhpParser\ParserFactory )->create( PhpParser\ParserFactory::PREFER_PHP7 );
151 $traverser = new PhpParser\NodeTraverser;
152 $finder = new DeprecatedInterfaceFinder;
153 $traverser->addVisitor( $finder );
154
155 $fileCount = count( $files );
156
157 for ( $i = 0; $i < $fileCount; $i++ ) {
158 $file = $files[$i];
159 $code = file_get_contents( $file );
160
161 if ( strpos( $code, '@deprecated' ) === -1 ) {
162 continue;
163 }
164
165 $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
166 $nodes = $parser->parse( $code, [ 'throwOnError' => false ] );
167 $traverser->traverse( $nodes );
168
169 if ( $i % $chunkSize === 0 ) {
170 $percentDone = 100 * $i / $fileCount;
171 fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
172 }
173 }
174
175 fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
176
177 // Colorize output if STDOUT is an interactive terminal.
178 if ( posix_isatty( STDOUT ) ) {
179 $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
180 $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
181 } else {
182 $versionFmt = "\n* Deprecated since %s:\n";
183 $entryFmt = " %s %s (%s:%d)\n";
184 }
185
186 foreach ( $finder->getFoundNodes() as $version => $nodes ) {
187 printf( $versionFmt, $version );
188 foreach ( $nodes as $node ) {
189 printf(
190 $entryFmt,
191 $node['hard'] ? '+' : '-',
192 $node['name'],
193 $node['filename'],
194 $node['line']
195 );
196 }
197 }
198 printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
199 }
200 }
201
202 $maintClass = 'FindDeprecated';
203 require_once RUN_MAINTENANCE_IF_MAIN;