PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/maintenance/findDeprecated.php

https://gitlab.com/link233/bootmw
PHP | 198 lines | 133 code | 28 blank | 37 comment | 11 complexity | 0a2984771ca445145b313d5b8723ce51 MD5 | raw file
  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. foreach ( $node->stmts as $stmt ) {
  65. if (
  66. $stmt instanceof PhpParser\Node\Expr\FuncCall
  67. && $stmt->name->toString() === 'wfDeprecated'
  68. ) {
  69. return true;
  70. }
  71. return false;
  72. }
  73. }
  74. public function enterNode( PhpParser\Node $node ) {
  75. $retVal = parent::enterNode( $node );
  76. if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) {
  77. $this->currentClass = $node->name;
  78. }
  79. if ( $node instanceof PhpParser\Node\FunctionLike ) {
  80. $docComment = $node->getDocComment();
  81. if ( !$docComment ) {
  82. return;
  83. }
  84. if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) {
  85. return;
  86. }
  87. $version = $matches[1];
  88. if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) {
  89. $name = $this->currentClass . '::' . $node->name;
  90. } else {
  91. $name = $node->name;
  92. }
  93. $this->foundNodes[ $version ][] = [
  94. 'filename' => $node->filename,
  95. 'line' => $node->getLine(),
  96. 'name' => $name,
  97. 'hard' => $this->isHardDeprecated( $node ),
  98. ];
  99. }
  100. return $retVal;
  101. }
  102. }
  103. /**
  104. * Maintenance task that recursively scans MediaWiki PHP files for deprecated
  105. * functions and interfaces and produces a report.
  106. */
  107. class FindDeprecated extends Maintenance {
  108. public function __construct() {
  109. parent::__construct();
  110. $this->addDescription( 'Find deprecated interfaces' );
  111. }
  112. public function getFiles() {
  113. global $IP;
  114. $files = new RecursiveDirectoryIterator( $IP . '/includes' );
  115. $files = new RecursiveIteratorIterator( $files );
  116. $files = new RegexIterator( $files, '/\.php$/' );
  117. return iterator_to_array( $files, false );
  118. }
  119. public function execute() {
  120. global $IP;
  121. $files = $this->getFiles();
  122. $chunkSize = ceil( count( $files ) / 72 );
  123. $parser = new PhpParser\Parser( new PhpParser\Lexer\Emulative );
  124. $traverser = new PhpParser\NodeTraverser;
  125. $finder = new DeprecatedInterfaceFinder;
  126. $traverser->addVisitor( $finder );
  127. $fileCount = count( $files );
  128. for ( $i = 0; $i < $fileCount; $i++ ) {
  129. $file = $files[$i];
  130. $code = file_get_contents( $file );
  131. if ( strpos( $code, '@deprecated' ) === -1 ) {
  132. continue;
  133. }
  134. $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) );
  135. $nodes = $parser->parse( $code, [ 'throwOnError' => false ] );
  136. $traverser->traverse( $nodes );
  137. if ( $i % $chunkSize === 0 ) {
  138. $percentDone = 100 * $i / $fileCount;
  139. fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone );
  140. }
  141. }
  142. fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' );
  143. // Colorize output if STDOUT is an interactive terminal.
  144. if ( posix_isatty( STDOUT ) ) {
  145. $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n";
  146. $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n";
  147. } else {
  148. $versionFmt = "\n* Deprecated since %s:\n";
  149. $entryFmt = " %s %s (%s:%d)\n";
  150. }
  151. foreach ( $finder->getFoundNodes() as $version => $nodes ) {
  152. printf( $versionFmt, $version );
  153. foreach ( $nodes as $node ) {
  154. printf(
  155. $entryFmt,
  156. $node['hard'] ? '+' : '-',
  157. $node['name'],
  158. $node['filename'],
  159. $node['line']
  160. );
  161. }
  162. }
  163. printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" );
  164. }
  165. }
  166. $maintClass = 'FindDeprecated';
  167. require_once RUN_MAINTENANCE_IF_MAIN;