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
22 * @ingroup Maintenance
23 * @phan-file-suppress PhanUndeclaredProperty Lots of custom properties
26 require_once __DIR__
. '/Maintenance.php';
27 require_once __DIR__
. '/../vendor/autoload.php';
30 * A PHPParser node visitor that associates each node with its file name.
32 class FileAwareNodeVisitor
extends PhpParser\NodeVisitorAbstract
{
33 private $currentFile = null;
35 public function enterNode( PhpParser\Node
$node ) {
36 $retVal = parent
::enterNode( $node );
37 $node->filename
= $this->currentFile
;
41 public function setCurrentFile( $filename ) {
42 $this->currentFile
= $filename;
45 public function getCurrentFile() {
46 return $this->currentFile
;
51 * A PHPParser node visitor that finds deprecated functions and methods.
53 class DeprecatedInterfaceFinder
extends FileAwareNodeVisitor
{
55 private $currentClass = null;
57 private $foundNodes = [];
59 public function getFoundNodes() {
60 // Sort results by version, then by filename, then by name.
61 foreach ( $this->foundNodes
as &$nodes ) {
62 uasort( $nodes, static function ( $a, $b ) {
63 return ( $a['filename'] . $a['name'] ) <=> ( $b['filename'] . $b['name'] );
66 ksort( $this->foundNodes
);
67 return $this->foundNodes
;
71 * Check whether a function or method includes a call to wfDeprecated(),
72 * indicating that it is a hard-deprecated interface.
73 * @param PhpParser\Node $node
76 public function isHardDeprecated( PhpParser\Node
$node ) {
77 if ( !$node->stmts
) {
80 foreach ( $node->stmts
as $stmt ) {
82 $stmt instanceof PhpParser\Node\Expr\FuncCall
83 && $stmt->name
->toString() === 'wfDeprecated'
91 public function enterNode( PhpParser\Node
$node ) {
92 $retVal = parent
::enterNode( $node );
94 if ( $node instanceof PhpParser\Node\Stmt\ClassLike
) {
95 $this->currentClass
= $node->name
;
98 if ( $node instanceof PhpParser\Node\FunctionLike
) {
99 $docComment = $node->getDocComment();
100 if ( !$docComment ) {
103 if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
106 $version = $matches[1];
108 if ( $node instanceof PhpParser\Node\Stmt\ClassMethod
) {
109 $name = $this->currentClass
. '::' . $node->name
;
114 $this->foundNodes
[ $version ][] = [
115 'filename' => $node->filename
,
116 'line' => $node->getLine(),
118 'hard' => $this->isHardDeprecated( $node ),
127 * Maintenance task that recursively scans MediaWiki PHP files for deprecated
128 * functions and interfaces and produces a report.
130 class FindDeprecated
extends Maintenance
{
131 public function __construct() {
132 parent
::__construct();
133 $this->addDescription( 'Find deprecated interfaces' );
137 * @return SplFileInfo[]
139 public function getFiles() {
142 $files = new RecursiveDirectoryIterator( $IP . '/includes' );
143 $files = new RecursiveIteratorIterator( $files );
144 $files = new RegexIterator( $files, '/\.php$/' );
145 return iterator_to_array( $files, false );
148 public function execute() {
151 $files = $this->getFiles();
152 $chunkSize = (int)ceil( count( $files ) / 72 );
154 $parser = ( new PhpParser\ParserFactory
)->create( PhpParser\ParserFactory
::PREFER_PHP7
);
155 $traverser = new PhpParser\NodeTraverser
;
156 $finder = new DeprecatedInterfaceFinder
;
157 $traverser->addVisitor( $finder );
159 $fileCount = count( $files );
161 for ( $i = 0; $i < $fileCount; $i++
) {
163 $code = file_get_contents( $file );
165 if ( strpos( $code, '@deprecated' ) === -1 ) {
169 $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) +
1 ) );
170 $nodes = $parser->parse( $code );
171 $traverser->traverse( $nodes );
173 if ( $i %
$chunkSize === 0 ) {
174 $percentDone = 100 * $i / $fileCount;
175 fprintf( STDERR
, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
179 fprintf( STDERR
, "\r[%'#-72s] 100%%\n", '' );
181 // Colorize output if STDOUT is an interactive terminal.
182 if ( posix_isatty( STDOUT
) ) {
183 $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
184 $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
186 $versionFmt = "\n* Deprecated since %s:\n";
187 $entryFmt = " %s %s (%s:%d)\n";
190 foreach ( $finder->getFoundNodes() as $version => $nodes ) {
191 printf( $versionFmt, $version );
192 foreach ( $nodes as $node ) {
195 $node['hard'] ?
'+' : '-',
202 printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
206 $maintClass = FindDeprecated
::class;
207 require_once RUN_MAINTENANCE_IF_MAIN
;