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 | // @codeCoverageIgnoreStart |
27 | require_once __DIR__ . '/Maintenance.php'; |
28 | require_once __DIR__ . '/../vendor/autoload.php'; |
29 | // @codeCoverageIgnoreEnd |
30 | |
31 | /** |
32 | * A PHPParser node visitor that associates each node with its file name. |
33 | */ |
34 | class FileAwareNodeVisitor extends PhpParser\NodeVisitorAbstract { |
35 | /** @var string|null */ |
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 | * A PHPParser node visitor that finds deprecated functions and methods. |
55 | */ |
56 | class DeprecatedInterfaceFinder extends FileAwareNodeVisitor { |
57 | |
58 | /** @var string */ |
59 | private $currentClass = null; |
60 | |
61 | /** @var array[] */ |
62 | private $foundNodes = []; |
63 | |
64 | public function getFoundNodes() { |
65 | // Sort results by version, then by filename, then by name. |
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 | * Check whether a function or method includes a call to wfDeprecated(), |
77 | * indicating that it is a hard-deprecated interface. |
78 | * @param PhpParser\Node $node |
79 | * @return bool |
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 | * Maintenance task that recursively scans MediaWiki PHP files for deprecated |
137 | * functions and interfaces and produces a report. |
138 | */ |
139 | class FindDeprecated extends Maintenance { |
140 | public function __construct() { |
141 | parent::__construct(); |
142 | $this->addDescription( 'Find deprecated interfaces' ); |
143 | } |
144 | |
145 | /** |
146 | * @return string The installation path of MediaWiki. This method is mocked in PHPUnit tests. |
147 | */ |
148 | protected function getMwInstallPath() { |
149 | return MW_INSTALL_PATH; |
150 | } |
151 | |
152 | /** |
153 | * @return SplFileInfo[] |
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 | // Colorize output if STDOUT is an interactive terminal. |
202 | if ( parent::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 | // @codeCoverageIgnoreStart |
227 | $maintClass = FindDeprecated::class; |
228 | require_once RUN_MAINTENANCE_IF_MAIN; |
229 | // @codeCoverageIgnoreEnd |