Phoenix303 has uploaded a new change for review.
https://gerrit.wikimedia.org/r/192248
Change subject: Sniff to detect unused global variables
......................................................................
Sniff to detect unused global variables
Bug: T53279
Change-Id: I856564e2de73c113aa9863efdd69ba65b1d40cb6
---
A MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
A MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_fail.php
2 files changed, 49 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer
refs/changes/48/192248/1
diff --git a/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
b/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
new file mode 100644
index 0000000..1cfceb2
--- /dev/null
+++ b/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Verify MediaWiki global variable naming convention.
+ * Unused global variables should be removed.
+ */
+class MediaWiki_Sniffs_VariableAnalysis_UnusedGlobalVariablesSniff implements
PHP_CodeSniffer_Sniff {
+
+ public function register() {
+ return array( T_FUNCTION );
+ }
+
+ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
+ $tokens = $phpcsFile->getTokens();
+
+ $scopeOpener = ++$tokens[ $stackPtr ][ 'scope_opener' ];
+ $scopeCloser = $tokens[ $stackPtr ][ 'scope_closer' ];
+
+ $global_line = 0;
+ $globalVariables = array();
+ $otherVariables = array();
+
+ for( $i = $scopeOpener; $i < $scopeCloser; $i++) {
+ if( in_array( $tokens[ $i ][ 'type' ],
PHP_CodeSniffer_Tokens::$emptyTokens ) === true ) {
+ continue;
+ }
+ if( $tokens[ $i ][ 'type' ] == "T_GLOBAL" ) {
+ $global_line = $tokens[ $i ]['line'];
+ }
+ if( $tokens[ $i ][ 'type' ] == "T_VARIABLE" && $tokens[
$i ][ 'line' ] == $global_line ) {
+ $globalVariables[] = $tokens[ $i ][ 'content' ]
."#". $i;
+ }
+ if( $tokens[ $i ][ 'type' ] == "T_VARIABLE" && $tokens[
$i ][ 'line' ] != $global_line ) {
+ $otherVariables[] = $tokens[ $i ][ 'content' ];
+ }
+ }
+ foreach( $globalVariables as $global ) {
+ $global = explode( "#", $global );
+ if( !in_array( $global[0], $otherVariables ) ) {
+ $phpcsFile -> addWarning( "Global " .
$global[0] ." is never used.", $global[1] );
+ }
+ }
+ }
+}
diff --git
a/MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_fail.php
b/MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_fail.php
new file mode 100644
index 0000000..e500e0a
--- /dev/null
+++ b/MediaWiki/Tests/files/VariableAnalysis/unused_global_variables_fail.php
@@ -0,0 +1,6 @@
+<?php
+
+function fooFoo () {
+ // The global variable is not used
+ global $wgSomething;
+}
--
To view, visit https://gerrit.wikimedia.org/r/192248
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I856564e2de73c113aa9863efdd69ba65b1d40cb6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Phoenix303 <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits