Don't use deprecated User::checkPassword() in TestUser
[mediawiki.git] / maintenance / findDeprecated.php
blobbc1b34aa67f9b988b82558d65a9504f1c5fda377
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.
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.
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.
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
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
26 require_once __DIR__ . '/../vendor/autoload.php';
28 /**
29 * A PHPParser node visitor that associates each node with its file name.
31 class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract {
32 private $currentFile = null;
34 public function enterNode( PhpParser\Node $node ) {
35 $retVal = parent::enterNode( $node );
36 $node->filename = $this->currentFile;
37 return $retVal;
40 public function setCurrentFile( $filename ) {
41 $this->currentFile = $filename;
44 public function getCurrentFile() {
45 return $this->currentFile;
49 /**
50 * A PHPParser node visitor that finds deprecated functions and methods.
52 class DeprecatedInterfaceFinder extends FileAwareNodeVisitor {
54 private $currentClass = null;
56 private $foundNodes = [];
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 } );
65 ksort( $this->foundNodes );
66 return $this->foundNodes;
69 /**
70 * Check whether a function or method includes a call to wfDeprecated(),
71 * indicating that it is a hard-deprecated interface.
73 public function isHardDeprecated( PhpParser\Node $node ) {
74 foreach ( $node->stmts as $stmt ) {
75 if (
76 $stmt instanceof PhpParser\Node\Expr\FuncCall
77 && $stmt->name->toString() === 'wfDeprecated'
78 ) {
79 return true;
81 return false;
85 public function enterNode( PhpParser\Node $node ) {
86 $retVal = parent::enterNode( $node );
88 if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
89 $this->currentClass = $node->name;
92 if ( $node instanceof PhpParser\Node\FunctionLike ) {
93 $docComment = $node->getDocComment();
94 if ( !$docComment ) {
95 return;
97 if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
98 return;
100 $version = $matches[1];
102 if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
103 $name = $this->currentClass . '::' . $node->name;
104 } else {
105 $name = $node->name;
108 $this->foundNodes[ $version ][] = [
109 'filename' => $node->filename,
110 'line' => $node->getLine(),
111 'name' => $name,
112 'hard' => $this->isHardDeprecated( $node ),
116 return $retVal;
121 * Maintenance task that recursively scans MediaWiki PHP files for deprecated
122 * functions and interfaces and produces a report.
124 class FindDeprecated extends Maintenance {
125 public function __construct() {
126 parent::__construct();
127 $this->addDescription( 'Find deprecated interfaces' );
130 public function getFiles() {
131 global $IP;
133 $files = new RecursiveDirectoryIterator( $IP . '/includes' );
134 $files = new RecursiveIteratorIterator( $files );
135 $files = new RegexIterator( $files, '/\.php$/' );
136 return iterator_to_array( $files, false );
139 public function execute() {
140 global $IP;
142 $files = $this->getFiles();
143 $chunkSize = ceil( count( $files ) / 72 );
145 $parser = new PhpParser\Parser( new PhpParser\Lexer\Emulative );
146 $traverser = new PhpParser\NodeTraverser;
147 $finder = new DeprecatedInterfaceFinder;
148 $traverser->addVisitor( $finder );
150 $fileCount = count( $files );
152 for ( $i = 0; $i < $fileCount; $i++ ) {
153 $file = $files[$i];
154 $code = file_get_contents( $file );
156 if ( strpos( $code, '@deprecated' ) === -1 ) {
157 continue;
160 $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
161 $nodes = $parser->parse( $code, [ 'throwOnError' => false ] );
162 $traverser->traverse( $nodes );
164 if ( $i % $chunkSize === 0 ) {
165 $percentDone = 100 * $i / $fileCount;
166 fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
170 fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
172 // Colorize output if STDOUT is an interactive terminal.
173 if ( posix_isatty( STDOUT ) ) {
174 $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
175 $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
176 } else {
177 $versionFmt = "\n* Deprecated since %s:\n";
178 $entryFmt = " %s %s (%s:%d)\n";
181 foreach ( $finder->getFoundNodes() as $version => $nodes ) {
182 printf( $versionFmt, $version );
183 foreach ( $nodes as $node ) {
184 printf(
185 $entryFmt,
186 $node['hard'] ? '+' : '-',
187 $node['name'],
188 $node['filename'],
189 $node['line']
193 printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
197 $maintClass = 'FindDeprecated';
198 require_once RUN_MAINTENANCE_IF_MAIN;