From: Ori Livneh Date: Mon, 14 Dec 2015 20:07:17 +0000 (-0800) Subject: Add a maintenance script for finding deprecated interfaces X-Git-Tag: 1.31.0-rc.0~8614 X-Git-Url: http://git.cyclocoop.org/%22%2C%20generer_url_ecrire%28?a=commitdiff_plain;h=a41102bd565bac6589f38593fcdec0e60225b2fc;p=lhc%2Fweb%2Fwiklou.git Add a maintenance script for finding deprecated interfaces Add a maintenance script, findDeprecated.php, which iterates through $IP/include/**.php, looking for functions and methods which have been marked as deprecated (via a '@deprecated' tag in the doc-block), and then prints a detailed report indicating the version, file, line and version of each deprecated interface. Change-Id: I9518a52b8c51ee972552a94f5367c1faa7e04a3a --- diff --git a/composer.json b/composer.json index b4878c9d08..9775823822 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ "justinrainbow/json-schema": "~1.3", "mediawiki/mediawiki-codesniffer": "0.4.0", "monolog/monolog": "~1.17.2", + "nikic/php-parser": "1.4.1", "nmred/kafka-php": "0.1.4", "phpunit/phpunit": "3.7.37", "wikimedia/avro": "1.7.7" diff --git a/maintenance/findDeprecated.php b/maintenance/findDeprecated.php new file mode 100644 index 0000000000..cbb1d5bf19 --- /dev/null +++ b/maintenance/findDeprecated.php @@ -0,0 +1,199 @@ +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 { + + private $currentClass = null; + + private $foundNodes = array(); + + public function getFoundNodes() { + // Sort results by version, then by filename, then by name. + foreach ( $this->foundNodes as $version => &$nodes ) { + uasort( $nodes, function ( $a, $b ) { + return ( $a['filename'] . $a['name'] ) < ( $b['filename'] . $b['name'] ) ? -1 : 1; + } ); + } + 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. + */ + public function isHardDeprecated( PhpParser\Node $node ) { + foreach ( $node->stmts as $stmt ) { + if ( + $stmt instanceof PhpParser\Node\Expr\FuncCall + && $stmt->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 ][] = array( + '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->mDescription = 'Find deprecated interfaces'; + } + + public function getFiles() { + global $IP; + + $files = new RecursiveDirectoryIterator( $IP . '/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 = ceil( count( $files ) / 72 ); + + $parser = new PhpParser\Parser( new PhpParser\Lexer\Emulative ); + $traverser = new PhpParser\NodeTraverser; + $finder = new DeprecatedInterfaceFinder; + $traverser->addVisitor( $finder ); + + $fileCount = count( $files ); + + for ( $i = 0; $i < $fileCount; $i++ ) { + $file = $files[$i]; + $code = file_get_contents( $file ); + + if ( strpos( $code, '@deprecated' ) === -1 ) { + continue; + } + + $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) ); + $nodes = $parser->parse( $code, array( 'throwOnError' => false ) ); + $traverser->traverse( $nodes ); + + if ( $i % $chunkSize === 0 ) { + $percentDone = 100 * $i / $fileCount; + fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone ); + } + } + + 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" ); + } +} + +$maintClass = 'FindDeprecated'; +require_once RUN_MAINTENANCE_IF_MAIN;