| 1 |
<?php |
| 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 |
require_once __DIR__ . '/Maintenance.php'; |
| 28 |
require_once __DIR__ . '/../vendor/autoload.php'; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract { |
| 35 |
|
| 36 |
private $currentFile = null; |
| 37 |
|
| 38 |
public function enterNode( PhpParser\Node $node ) { |
| 39 |
$retVal = parent::enterNode( $node ); |
| 40 |
$node->filename = $this->currentFile; |
| 41 |
return $retVal; |
| 42 |
} |
| 43 |
|
| 44 |
public function setCurrentFile( $filename ) { |
| 45 |
$this->currentFile = $filename; |
| 46 |
} |
| 47 |
|
| 48 |
public function getCurrentFile() { |
| 49 |
return $this->currentFile; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class DeprecatedInterfaceFinder extends FileAwareNodeVisitor { |
| 57 |
|
| 58 |
|
| 59 |
private $currentClass = null; |
| 60 |
|
| 61 |
|
| 62 |
private $foundNodes = []; |
| 63 |
|
| 64 |
public function getFoundNodes() { |
| 65 |
|
| 66 |
foreach ( $this->foundNodes as &$nodes ) { |
| 67 |
uasort( $nodes, static function ( $a, $b ) { |
| 68 |
return ( $a['filename'] . $a['name'] ) <=> ( $b['filename'] . $b['name'] ); |
| 69 |
} ); |
| 70 |
} |
| 71 |
ksort( $this->foundNodes ); |
| 72 |
return $this->foundNodes; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function isHardDeprecated( PhpParser\Node $node ) { |
| 82 |
if ( !$node->stmts ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
foreach ( $node->stmts as $stmt ) { |
| 86 |
$functionExpression = null; |
| 87 |
if ( $stmt instanceof PhpParser\Node\Expr\FuncCall ) { |
| 88 |
$functionExpression = $stmt; |
| 89 |
} |
| 90 |
if ( isset( $stmt->expr ) && $stmt->expr instanceof PhpParser\Node\Expr\FuncCall ) { |
| 91 |
$functionExpression = $stmt->expr; |
| 92 |
} |
| 93 |
if ( $functionExpression && $functionExpression->name->toString() === 'wfDeprecated' ) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public function enterNode( PhpParser\Node $node ) { |
| 101 |
$retVal = parent::enterNode( $node ); |
| 102 |
|
| 103 |
if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) { |
| 104 |
$this->currentClass = $node->name; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $node instanceof PhpParser\Node\FunctionLike ) { |
| 108 |
$docComment = $node->getDocComment(); |
| 109 |
if ( !$docComment ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
$version = $matches[1]; |
| 116 |
|
| 117 |
if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) { |
| 118 |
$name = $this->currentClass . '::' . $node->name; |
| 119 |
} else { |
| 120 |
$name = $node->name; |
| 121 |
} |
| 122 |
|
| 123 |
$this->foundNodes[ $version ][] = [ |
| 124 |
'filename' => $node->filename, |
| 125 |
'line' => $node->getLine(), |
| 126 |
'name' => $name, |
| 127 |
'hard' => $this->isHardDeprecated( $node ), |
| 128 |
]; |
| 129 |
} |
| 130 |
|
| 131 |
return $retVal; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
class FindDeprecated extends Maintenance { |
| 140 |
public function __construct() { |
| 141 |
parent::__construct(); |
| 142 |
$this->addDescription( 'Find deprecated interfaces' ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
protected function getMwInstallPath() { |
| 149 |
return MW_INSTALL_PATH; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getFiles() { |
| 156 |
$files = new RecursiveDirectoryIterator( $this->getMwInstallPath() . '/includes' ); |
| 157 |
$files = new RecursiveIteratorIterator( $files ); |
| 158 |
$files = new RegexIterator( $files, '/\.php$/' ); |
| 159 |
return iterator_to_array( $files, false ); |
| 160 |
} |
| 161 |
|
| 162 |
public function execute() { |
| 163 |
global $IP; |
| 164 |
|
| 165 |
$files = $this->getFiles(); |
| 166 |
$chunkSize = (int)ceil( count( $files ) / 72 ); |
| 167 |
|
| 168 |
$parser = ( new PhpParser\ParserFactory )->createForVersion( PhpParser\PhpVersion::fromComponents( 7, 0 ) ); |
| 169 |
$traverser = new PhpParser\NodeTraverser; |
| 170 |
$finder = new DeprecatedInterfaceFinder; |
| 171 |
$traverser->addVisitor( $finder ); |
| 172 |
|
| 173 |
$fileCount = count( $files ); |
| 174 |
|
| 175 |
$outputProgress = !defined( 'MW_PHPUNIT_TEST' ); |
| 176 |
|
| 177 |
for ( $i = 0; $i < $fileCount; $i++ ) { |
| 178 |
$file = $files[$i]; |
| 179 |
$code = file_get_contents( $file ); |
| 180 |
|
| 181 |
if ( !str_contains( $code, '@deprecated' ) ) { |
| 182 |
continue; |
| 183 |
} |
| 184 |
|
| 185 |
$finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) ); |
| 186 |
$nodes = $parser->parse( $code ); |
| 187 |
$traverser->traverse( $nodes ); |
| 188 |
|
| 189 |
if ( $i % $chunkSize === 0 ) { |
| 190 |
$percentDone = 100 * $i / $fileCount; |
| 191 |
if ( $outputProgress ) { |
| 192 |
fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone ); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if ( $outputProgress ) { |
| 198 |
fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
if ( posix_isatty( STDOUT ) ) { |
| 203 |
$versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n"; |
| 204 |
$entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n"; |
| 205 |
} else { |
| 206 |
$versionFmt = "\n* Deprecated since %s:\n"; |
| 207 |
$entryFmt = " %s %s (%s:%d)\n"; |
| 208 |
} |
| 209 |
|
| 210 |
foreach ( $finder->getFoundNodes() as $version => $nodes ) { |
| 211 |
printf( $versionFmt, $version ); |
| 212 |
foreach ( $nodes as $node ) { |
| 213 |
printf( |
| 214 |
$entryFmt, |
| 215 |
$node['hard'] ? '+' : '-', |
| 216 |
$node['name'], |
| 217 |
$node['filename'], |
| 218 |
$node['line'] |
| 219 |
); |
| 220 |
} |
| 221 |
} |
| 222 |
printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
$maintClass = FindDeprecated::class; |
| 228 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 229 |
|