MediaWiki master
findDeprecated.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30require_once __DIR__ . '/../vendor/autoload.php';
31// @codeCoverageIgnoreEnd
32
36class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract {
38 private $currentFile = null;
39
40 public function enterNode( PhpParser\Node $node ) {
41 $retVal = parent::enterNode( $node );
42 $node->filename = $this->currentFile;
43 return $retVal;
44 }
45
46 public function setCurrentFile( $filename ) {
47 $this->currentFile = $filename;
48 }
49
50 public function getCurrentFile() {
51 return $this->currentFile;
52 }
53}
54
59
61 private $currentClass = null;
62
64 private $foundNodes = [];
65
66 public function getFoundNodes() {
67 // Sort results by version, then by filename, then by name.
68 foreach ( $this->foundNodes as &$nodes ) {
69 uasort( $nodes, static function ( $a, $b ) {
70 return ( $a['filename'] . $a['name'] ) <=> ( $b['filename'] . $b['name'] );
71 } );
72 }
73 ksort( $this->foundNodes );
74 return $this->foundNodes;
75 }
76
83 public function isHardDeprecated( PhpParser\Node $node ) {
84 if ( !$node->stmts ) {
85 return false;
86 }
87 foreach ( $node->stmts as $stmt ) {
88 $functionExpression = null;
89 if ( $stmt instanceof PhpParser\Node\Expr\FuncCall ) {
90 $functionExpression = $stmt;
91 }
92 if ( isset( $stmt->expr ) && $stmt->expr instanceof PhpParser\Node\Expr\FuncCall ) {
93 $functionExpression = $stmt->expr;
94 }
95 if ( $functionExpression && $functionExpression->name->toString() === 'wfDeprecated' ) {
96 return true;
97 }
98 return false;
99 }
100 }
101
102 public function enterNode( PhpParser\Node $node ) {
103 $retVal = parent::enterNode( $node );
104
105 if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
106 $this->currentClass = $node->name;
107 }
108
109 if ( $node instanceof PhpParser\Node\FunctionLike ) {
110 $docComment = $node->getDocComment();
111 if ( !$docComment ) {
112 return;
113 }
114 if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
115 return;
116 }
117 $version = $matches[1];
118
119 if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
120 $name = $this->currentClass . '::' . $node->name;
121 } else {
122 $name = $node->name;
123 }
124
125 $this->foundNodes[ $version ][] = [
126 'filename' => $node->filename,
127 'line' => $node->getLine(),
128 'name' => $name,
129 'hard' => $this->isHardDeprecated( $node ),
130 ];
131 }
132
133 return $retVal;
134 }
135}
136
142 public function __construct() {
143 parent::__construct();
144 $this->addDescription( 'Find deprecated interfaces' );
145 }
146
150 protected function getMwInstallPath() {
151 return MW_INSTALL_PATH;
152 }
153
157 public function getFiles() {
158 $files = new RecursiveDirectoryIterator( $this->getMwInstallPath() . '/includes' );
159 $files = new RecursiveIteratorIterator( $files );
160 $files = new RegexIterator( $files, '/\.php$/' );
161 return iterator_to_array( $files, false );
162 }
163
164 public function execute() {
165 global $IP;
166
167 $files = $this->getFiles();
168 $chunkSize = (int)ceil( count( $files ) / 72 );
169
170 $parser = ( new PhpParser\ParserFactory )->createForVersion( PhpParser\PhpVersion::fromComponents( 7, 0 ) );
171 $traverser = new PhpParser\NodeTraverser;
172 $finder = new DeprecatedInterfaceFinder;
173 $traverser->addVisitor( $finder );
174
175 $fileCount = count( $files );
176
177 $outputProgress = !defined( 'MW_PHPUNIT_TEST' );
178
179 for ( $i = 0; $i < $fileCount; $i++ ) {
180 $file = $files[$i];
181 $code = file_get_contents( $file );
182
183 if ( !str_contains( $code, '@deprecated' ) ) {
184 continue;
185 }
186
187 $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
188 $nodes = $parser->parse( $code );
189 $traverser->traverse( $nodes );
190
191 if ( $i % $chunkSize === 0 ) {
192 $percentDone = 100 * $i / $fileCount;
193 if ( $outputProgress ) {
194 fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
195 }
196 }
197 }
198
199 if ( $outputProgress ) {
200 fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
201 }
202
203 // Colorize output if STDOUT is an interactive terminal.
204 if ( parent::posix_isatty( STDOUT ) ) {
205 $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
206 $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
207 } else {
208 $versionFmt = "\n* Deprecated since %s:\n";
209 $entryFmt = " %s %s (%s:%d)\n";
210 }
211
212 foreach ( $finder->getFoundNodes() as $version => $nodes ) {
213 printf( $versionFmt, $version );
214 foreach ( $nodes as $node ) {
215 printf(
216 $entryFmt,
217 $node['hard'] ? '+' : '-',
218 $node['name'],
219 $node['filename'],
220 $node['line']
221 );
222 }
223 }
224 printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
225 }
226}
227
228// @codeCoverageIgnoreStart
229$maintClass = FindDeprecated::class;
230require_once RUN_MAINTENANCE_IF_MAIN;
231// @codeCoverageIgnoreEnd
if(!defined( 'MEDIAWIKI')) if(ini_get('mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition Setup.php:105
A PHPParser node visitor that finds deprecated functions and methods.
enterNode(PhpParser\Node $node)
isHardDeprecated(PhpParser\Node $node)
Check whether a function or method includes a call to wfDeprecated(), indicating that it is a hard-de...
A PHPParser node visitor that associates each node with its file name.
enterNode(PhpParser\Node $node)
Maintenance task that recursively scans MediaWiki PHP files for deprecated functions and interfaces a...
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addDescription( $text)
Set the description text.
$maintClass