r70436 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70435‎ | r70436 | r70437 >
Date:22:34, 3 August 2010
Author:platonides
Status:deferred
Tags:
Comment:
Add detection of calls to deprecated methods.
Currently this is very basic, based just on the method name, and there is a number of false positives.
Modified paths:
  • /trunk/tools/code-utils/check-vars.php (modified) (history)

Diff [purge]

Index: trunk/tools/code-utils/check-vars.php
@@ -10,6 +10,9 @@
1111 require_once( dirname( __FILE__ ) . "/../../phase3/includes/Defines.php" ); # Faster than parsing
1212 require_once( dirname( __FILE__ ) . "/../../phase3/includes/AutoLoader.php" );
1313
 14+$mwDeprecatedFunctions = false;
 15+@include( dirname( __FILE__ ) . "/deprecated.functions" );
 16+
1417 if ( !extension_loaded( 'sockets' ) ) dl('sockets.so');
1518 if ( !extension_loaded( 'PDO' ) ) dl('pdo.so');
1619
@@ -42,6 +45,7 @@
4346 static $mDefaultSettingsGlobals = null;
4447
4548 static $constantIgnorePrefixes = array( "PGSQL_", "OCI_", "SQLT_BLOB", "DB2_", "XMLREADER_" ); # Ignore constants with these prefixes
 49+ protected $generateDeprecatedList = false;
4650
4751 /* Values for status */
4852 const WAITING_FUNCTION = 0;
@@ -54,7 +58,11 @@
5559 const CLASS_NAME = -4;
5660 const CLASS_MEMBER = -5;
5761 const FUNCTION_NAME = -6;
 62+ const FUNCTION_DEFINITION = -7;
5863
 64+ /* Function attribute */
 65+ const FUNCTION_DEPRECATED = -8;
 66+
5967 function __construct() {
6068 if ( self::$mDefaultSettingsGlobals == null ) {
6169 $this->load( dirname( dirname( dirname( __FILE__ ) ) ) . "/phase3/includes/DefaultSettings.php", false );
@@ -114,6 +122,17 @@
115123 }
116124 }
117125
 126+ function setGenerateDeprecatedList( $bool = true ) {
 127+ $this->generateDeprecatedList = $bool;
 128+ }
 129+ function getGenerateDeprecatedList() {
 130+ return $this->generateDeprecatedList;
 131+ }
 132+ function saveDeprecatedList( $filename ) {
 133+ file_put_contents( $filename, "<?php\n\$mwDeprecatedFunctions = array( " . implode( ",\n\t", $this->mDeprecatedFunctionList ) . "\n);\n\n" );
 134+ }
 135+
 136+
118137 function load($file, $shortcircuit = true) {
119138 $this->mProblemCount = 0;
120139 $this->mFilename = $file;
@@ -182,6 +201,11 @@
183202 if ( $token == ';' )
184203 $this->mFunctionQualifiers = array();
185204
 205+ if ( $token[0] == T_DOC_COMMENT ) {
 206+ if ( strpos( $token[1], '@deprecated' ) !== false ) {
 207+ $this->mFunctionQualifiers[] = self::FUNCTION_DEPRECATED;
 208+ }
 209+ }
186210 if ( in_array( $token[0], self::$functionQualifiers ) ) {
187211 $this->mFunctionQualifiers[] = $token[0];
188212 }
@@ -191,6 +215,7 @@
192216
193217 if ( ( $lastMeaningfulToken[0] == T_CLASS ) && ( $token[0] == T_STRING ) ) {
194218 $this->mKnownFileClasses[] = $token[1];
 219+ $this->mClass = $token[1];
195220 }
196221
197222 if ($token[0] != T_FUNCTION)
@@ -206,7 +231,14 @@
207232 $this->mStatus = self::IN_FUNCTION;
208233 $this->mBraces = 0;
209234 $this->mFunctionGlobals = array();
 235+ $currentToken[0] = self::FUNCTION_DEFINITION;
210236
 237+ if ( $this->generateDeprecatedList && in_array( self::FUNCTION_DEPRECATED, $this->mFunctionQualifiers ) ) {
 238+ if ( ( substr( $this->mFunction, 0, 2 ) != "__" ) && $this->mClass != 'Image' ) {
 239+ $this->mDeprecatedFunctionList[] = "/*$this->mClass::*/'$this->mFunction'";
 240+ }
 241+ }
 242+
211243 $this->debug("Entering into function {$token[1]} at line {$token[2]} ");
212244 continue;
213245 }
@@ -267,7 +299,7 @@
268300 # Bug of r69904
269301 $this->warning("$lastMeaningfulToken[1]:: used in line $lastMeaningfulToken[2] It probably should have been " . substr( $lastMeaningfulToken[1], 1 ) . "::");
270302 }
271 - } elseif ( ( $token[0] == T_STRING ) && ( is_array($lastMeaningfulToken)
 303+ } elseif ( ( $token[0] == T_STRING ) && ( is_array( $lastMeaningfulToken)
272304 && in_array( $lastMeaningfulToken[0], array( T_OBJECT_OPERATOR, T_PAAMAYIM_NEKUDOTAYIM ) ) ) ) {
273305 # Class member or class constant
274306 $currentToken[0] = self::CLASS_MEMBER;
@@ -279,8 +311,13 @@
280312 }
281313 }
282314
283 - if ( ( $token == '(' ) && is_array( $lastMeaningfulToken ) && ( $lastMeaningfulToken[0] == T_STRING ) ) {
284 - $lastMeaningfulToken[0] = self::FUNCTION_NAME;
 315+ if ( ( $token == '(' ) && is_array( $lastMeaningfulToken ) ) {
 316+ if ( $lastMeaningfulToken[0] == T_STRING ) {
 317+ $lastMeaningfulToken[0] = self::FUNCTION_NAME;
 318+ $this->checkDeprecation($lastMeaningfulToken);
 319+ } else if ( $lastMeaningfulToken[0] == self::CLASS_MEMBER) {
 320+ $this->checkDeprecation($lastMeaningfulToken);
 321+ }
285322 }
286323
287324 /* Detect constants */
@@ -342,6 +379,13 @@
343380 $this->checkPendingClasses();
344381 }
345382
 383+ function checkDeprecation( $token ) {
 384+ global $mwDeprecatedFunctions;
 385+ if ( $mwDeprecatedFunctions && !in_array( self::FUNCTION_DEPRECATED, $this->mFunctionQualifiers ) && in_array( $token[1], $mwDeprecatedFunctions ) ) {
 386+ $this->warning( "Non deprecated function $this->mFunction calls deprecated function {$token[1]} in line {$token[2]}" );
 387+ }
 388+ }
 389+
346390 function error( $token ) {
347391 $msg = "Unexpected token " . ( is_string( $token ) ? $token : token_name( $token[0] ) ) ;
348392 if ( is_array( $token ) && isset( $token[2] ) ) {
@@ -458,7 +502,14 @@
459503 //$cv->mDebug = true;
460504
461505 array_shift($argv);
 506+if ( $argv[0] == '--generate-deprecated-list' ) {
 507+ $cv->setGenerateDeprecatedList( true );
 508+ array_shift( $argv );
 509+}
462510 foreach ( $argv as $arg ) {
463511 $cv->load( $arg );
464512 $cv->execute();
465513 }
 514+if ( $cv->getGenerateDeprecatedList( ) ) {
 515+ $cv->saveDeprecatedList( dirname( __FILE__ ) . "/deprecated.functions" );
 516+}

Status & tagging log