findDeprecated.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Maintenance script that recursively scans MediaWiki's PHP source tree
  4. * for deprecated functions and methods and pretty-prints the results.
  5. *
  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.
  10. *
  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.
  15. *
  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
  20. *
  21. * @file
  22. * @ingroup Maintenance
  23. */
  24. require_once __DIR__ . '/Maintenance.php';
  25. require_once __DIR__ . '/../vendor/autoload.php';
  26. /**
  27. * A PHPParser node visitor that associates each node with its file name.
  28. */
  29. class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract {
  30. private $currentFile = null;
  31. public function enterNode( PhpParser\Node $node ) {
  32. $retVal = parent::enterNode( $node );
  33. $node->filename = $this->currentFile;
  34. return $retVal;
  35. }
  36. public function setCurrentFile( $filename ) {
  37. $this->currentFile = $filename;
  38. }
  39. public function getCurrentFile() {
  40. return $this->currentFile;
  41. }
  42. }
  43. /**
  44. * A PHPParser node visitor that finds deprecated functions and methods.
  45. */
  46. class DeprecatedInterfaceFinder extends FileAwareNodeVisitor {
  47. private $currentClass = null;
  48. private $foundNodes = [];
  49. public function getFoundNodes() {
  50. // Sort results by version, then by filename, then by name.
  51. foreach ( $this->foundNodes as $version => &$nodes ) {
  52. uasort( $nodes, function ( $a, $b ) {
  53. return ( $a['filename'] . $a['name'] ) < ( $b['filename'] . $b['name'] ) ? -1 : 1;
  54. } );
  55. }
  56. ksort( $this->foundNodes );
  57. return $this->foundNodes;
  58. }
  59. /**
  60. * Check whether a function or method includes a call to wfDeprecated(),
  61. * indicating that it is a hard-deprecated interface.
  62. */
  63. public function isHardDeprecated( PhpParser\Node $node ) {
  64. if ( !$node->stmts ) {
  65. return false;
  66. }
  67. foreach ( $node->stmts as $stmt ) {
  68. if (
  69. $stmt instanceof PhpParser\Node\Expr\FuncCall
  70. && $stmt->name->toString() === 'wfDeprecated'
  71. ) {
  72. return true;
  73. }
  74. return false;
  75. }
  76. }
  77. public function enterNode( PhpParser\Node $node ) {
  78. $retVal = parent::enterNode( $node );
  79. if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
  80. $this->currentClass = $node->name;
  81. }
  82. if ( $node instanceof PhpParser\Node\FunctionLike ) {
  83. $docComment = $node->getDocComment();
  84. if ( !$docComment ) {
  85. return;
  86. }
  87. if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
  88. return;
  89. }
  90. $version = $matches[1];
  91. if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
  92. $name = $this->currentClass . '::' . $node->name;
  93. } else {
  94. $name = $node->name;
  95. }
  96. $this->foundNodes[ $version ][] = [
  97. 'filename' => $node->filename,
  98. 'line' => $node->getLine(),
  99. 'name' => $name,
  100. 'hard' => $this->isHardDeprecated( $node ),
  101. ];
  102. }
  103. return $retVal;
  104. }
  105. }
  106. /**
  107. * Maintenance task that recursively scans MediaWiki PHP files for deprecated
  108. * functions and interfaces and produces a report.
  109. */
  110. class FindDeprecated extends Maintenance {
  111. public function __construct() {
  112. parent::__construct();
  113. $this->addDescription( 'Find deprecated interfaces' );
  114. }
  115. public function getFiles() {
  116. global $IP;
  117. $files = new RecursiveDirectoryIterator( $IP . '/includes' );
  118. $files = new RecursiveIteratorIterator( $files );
  119. $files = new RegexIterator( $files, '/\.php$/' );
  120. return iterator_to_array( $files, false );
  121. }
  122. public function execute() {
  123. global $IP;
  124. $files = $this->getFiles();
  125. $chunkSize = ceil( count( $files ) / 72 );
  126. $parser = ( new PhpParser\ParserFactory )->create( PhpParser\ParserFactory::PREFER_PHP7 );
  127. $traverser = new PhpParser\NodeTraverser;
  128. $finder = new DeprecatedInterfaceFinder;
  129. $traverser->addVisitor( $finder );
  130. $fileCount = count( $files );
  131. for ( $i = 0; $i < $fileCount; $i++ ) {
  132. $file = $files[$i];
  133. $code = file_get_contents( $file );
  134. if ( strpos( $code, '@deprecated' ) === -1 ) {
  135. continue;
  136. }
  137. $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
  138. $nodes = $parser->parse( $code, [ 'throwOnError' => false ] );
  139. $traverser->traverse( $nodes );
  140. if ( $i % $chunkSize === 0 ) {
  141. $percentDone = 100 * $i / $fileCount;
  142. fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
  143. }
  144. }
  145. fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
  146. // Colorize output if STDOUT is an interactive terminal.
  147. if ( posix_isatty( STDOUT ) ) {
  148. $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
  149. $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
  150. } else {
  151. $versionFmt = "\n* Deprecated since %s:\n";
  152. $entryFmt = " %s %s (%s:%d)\n";
  153. }
  154. foreach ( $finder->getFoundNodes() as $version => $nodes ) {
  155. printf( $versionFmt, $version );
  156. foreach ( $nodes as $node ) {
  157. printf(
  158. $entryFmt,
  159. $node['hard'] ? '+' : '-',
  160. $node['name'],
  161. $node['filename'],
  162. $node['line']
  163. );
  164. }
  165. }
  166. printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
  167. }
  168. }
  169. $maintClass = 'FindDeprecated';
  170. require_once RUN_MAINTENANCE_IF_MAIN;