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