Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.36% |
76 / 87 |
|
40.00% |
4 / 10 |
CRAP | |
0.00% |
0 / 3 |
FileAwareNodeVisitor | |
80.00% |
4 / 5 |
|
66.67% |
2 / 3 |
3.07 | |
0.00% |
0 / 1 |
enterNode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setCurrentFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCurrentFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
DeprecatedInterfaceFinder | |
86.49% |
32 / 37 |
|
0.00% |
0 / 3 |
16.63 | |
0.00% |
0 / 1 |
getFoundNodes | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
isHardDeprecated | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
8.38 | |||
enterNode | |
90.00% |
18 / 20 |
|
0.00% |
0 / 1 |
6.04 | |||
FindDeprecated | |
88.89% |
40 / 45 |
|
50.00% |
2 / 4 |
13.23 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getMwInstallPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFiles | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
89.47% |
34 / 38 |
|
0.00% |
0 / 1 |
10.12 |
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 | * @phan-file-suppress PhanUndeclaredProperty Lots of custom properties |
24 | */ |
25 | |
26 | use MediaWiki\Maintenance\Maintenance; |
27 | |
28 | // @codeCoverageIgnoreStart |
29 | require_once __DIR__ . '/Maintenance.php'; |
30 | require_once __DIR__ . '/../vendor/autoload.php'; |
31 | // @codeCoverageIgnoreEnd |
32 | |
33 | /** |
34 | * A PHPParser node visitor that associates each node with its file name. |
35 | */ |
36 | class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract { |
37 | /** @var string|null */ |
38 | private $currentFile = null; |
39 | |
40 | public function enterNode( PhpParser\Node $node ) { |
41 | $retVal = parent::enterNode( $node ); |
42 | $node->filename = $this->currentFile; |
43 | return $retVal; |
44 | } |
45 | |
46 | public function setCurrentFile( $filename ) { |
47 | $this->currentFile = $filename; |
48 | } |
49 | |
50 | public function getCurrentFile() { |
51 | return $this->currentFile; |
52 | } |
53 | } |
54 | |
55 | /** |
56 | * A PHPParser node visitor that finds deprecated functions and methods. |
57 | */ |
58 | class DeprecatedInterfaceFinder extends FileAwareNodeVisitor { |
59 | |
60 | /** @var string */ |
61 | private $currentClass = null; |
62 | |
63 | /** @var array[] */ |
64 | private $foundNodes = []; |
65 | |
66 | public function getFoundNodes() { |
67 | // Sort results by version, then by filename, then by name. |
68 | foreach ( $this->foundNodes as &$nodes ) { |
69 | uasort( $nodes, static function ( $a, $b ) { |
70 | return ( $a['filename'] . $a['name'] ) <=> ( $b['filename'] . $b['name'] ); |
71 | } ); |
72 | } |
73 | ksort( $this->foundNodes ); |
74 | return $this->foundNodes; |
75 | } |
76 | |
77 | /** |
78 | * Check whether a function or method includes a call to wfDeprecated(), |
79 | * indicating that it is a hard-deprecated interface. |
80 | * @param PhpParser\Node $node |
81 | * @return bool |
82 | */ |
83 | public function isHardDeprecated( PhpParser\Node $node ) { |
84 | if ( !$node->stmts ) { |
85 | return false; |
86 | } |
87 | foreach ( $node->stmts as $stmt ) { |
88 | $functionExpression = null; |
89 | if ( $stmt instanceof PhpParser\Node\Expr\FuncCall ) { |
90 | $functionExpression = $stmt; |
91 | } |
92 | if ( isset( $stmt->expr ) && $stmt->expr instanceof PhpParser\Node\Expr\FuncCall ) { |
93 | $functionExpression = $stmt->expr; |
94 | } |
95 | if ( $functionExpression && $functionExpression->name->toString() === 'wfDeprecated' ) { |
96 | return true; |
97 | } |
98 | return false; |
99 | } |
100 | } |
101 | |
102 | public function enterNode( PhpParser\Node $node ) { |
103 | $retVal = parent::enterNode( $node ); |
104 | |
105 | if ( $node instanceof PhpParser\Node\Stmt\ClassLike ) { |
106 | $this->currentClass = $node->name; |
107 | } |
108 | |
109 | if ( $node instanceof PhpParser\Node\FunctionLike ) { |
110 | $docComment = $node->getDocComment(); |
111 | if ( !$docComment ) { |
112 | return; |
113 | } |
114 | if ( !preg_match( '/@deprecated.*(\d+\.\d+)/', $docComment->getText(), $matches ) ) { |
115 | return; |
116 | } |
117 | $version = $matches[1]; |
118 | |
119 | if ( $node instanceof PhpParser\Node\Stmt\ClassMethod ) { |
120 | $name = $this->currentClass . '::' . $node->name; |
121 | } else { |
122 | $name = $node->name; |
123 | } |
124 | |
125 | $this->foundNodes[ $version ][] = [ |
126 | 'filename' => $node->filename, |
127 | 'line' => $node->getLine(), |
128 | 'name' => $name, |
129 | 'hard' => $this->isHardDeprecated( $node ), |
130 | ]; |
131 | } |
132 | |
133 | return $retVal; |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * Maintenance task that recursively scans MediaWiki PHP files for deprecated |
139 | * functions and interfaces and produces a report. |
140 | */ |
141 | class FindDeprecated extends Maintenance { |
142 | public function __construct() { |
143 | parent::__construct(); |
144 | $this->addDescription( 'Find deprecated interfaces' ); |
145 | } |
146 | |
147 | /** |
148 | * @return string The installation path of MediaWiki. This method is mocked in PHPUnit tests. |
149 | */ |
150 | protected function getMwInstallPath() { |
151 | return MW_INSTALL_PATH; |
152 | } |
153 | |
154 | /** |
155 | * @return SplFileInfo[] |
156 | */ |
157 | public function getFiles() { |
158 | $files = new RecursiveDirectoryIterator( $this->getMwInstallPath() . '/includes' ); |
159 | $files = new RecursiveIteratorIterator( $files ); |
160 | $files = new RegexIterator( $files, '/\.php$/' ); |
161 | return iterator_to_array( $files, false ); |
162 | } |
163 | |
164 | public function execute() { |
165 | global $IP; |
166 | |
167 | $files = $this->getFiles(); |
168 | $chunkSize = (int)ceil( count( $files ) / 72 ); |
169 | |
170 | $parser = ( new PhpParser\ParserFactory )->createForVersion( PhpParser\PhpVersion::fromComponents( 7, 0 ) ); |
171 | $traverser = new PhpParser\NodeTraverser; |
172 | $finder = new DeprecatedInterfaceFinder; |
173 | $traverser->addVisitor( $finder ); |
174 | |
175 | $fileCount = count( $files ); |
176 | |
177 | $outputProgress = !defined( 'MW_PHPUNIT_TEST' ); |
178 | |
179 | for ( $i = 0; $i < $fileCount; $i++ ) { |
180 | $file = $files[$i]; |
181 | $code = file_get_contents( $file ); |
182 | |
183 | if ( !str_contains( $code, '@deprecated' ) ) { |
184 | continue; |
185 | } |
186 | |
187 | $finder->setCurrentFile( substr( $file->getPathname(), strlen( $IP ) + 1 ) ); |
188 | $nodes = $parser->parse( $code ); |
189 | $traverser->traverse( $nodes ); |
190 | |
191 | if ( $i % $chunkSize === 0 ) { |
192 | $percentDone = 100 * $i / $fileCount; |
193 | if ( $outputProgress ) { |
194 | fprintf( STDERR, "\r[%-72s] %d%%", str_repeat( '#', $i / $chunkSize ), $percentDone ); |
195 | } |
196 | } |
197 | } |
198 | |
199 | if ( $outputProgress ) { |
200 | fprintf( STDERR, "\r[%'#-72s] 100%%\n", '' ); |
201 | } |
202 | |
203 | // Colorize output if STDOUT is an interactive terminal. |
204 | if ( parent::posix_isatty( STDOUT ) ) { |
205 | $versionFmt = "\n* Deprecated since \033[37;1m%s\033[0m:\n"; |
206 | $entryFmt = " %s \033[33;1m%s\033[0m (%s:%d)\n"; |
207 | } else { |
208 | $versionFmt = "\n* Deprecated since %s:\n"; |
209 | $entryFmt = " %s %s (%s:%d)\n"; |
210 | } |
211 | |
212 | foreach ( $finder->getFoundNodes() as $version => $nodes ) { |
213 | printf( $versionFmt, $version ); |
214 | foreach ( $nodes as $node ) { |
215 | printf( |
216 | $entryFmt, |
217 | $node['hard'] ? '+' : '-', |
218 | $node['name'], |
219 | $node['filename'], |
220 | $node['line'] |
221 | ); |
222 | } |
223 | } |
224 | printf( "\nlegend:\n -: soft-deprecated\n +: hard-deprecated (via wfDeprecated())\n" ); |
225 | } |
226 | } |
227 | |
228 | // @codeCoverageIgnoreStart |
229 | $maintClass = FindDeprecated::class; |
230 | require_once RUN_MAINTENANCE_IF_MAIN; |
231 | // @codeCoverageIgnoreEnd |