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