jenkins-bot has submitted this change and it was merged.
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
A MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
A
MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
4 files changed, 71 insertions(+), 0 deletions(-)
Approvals:
Legoktm: Looks good to me, approved
jenkins-bot: Verified
diff --git a/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
b/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
new file mode 100644
index 0000000..f7409dd
--- /dev/null
+++ b/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Detect unused MediaWiki global variable.
+ * 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'];
+
+ $globalLine = 0;
+ $globalVariables = array();
+ $otherVariables = array();
+ $matches = array();
+ $strVariables = array();
+
+ for ( $i = $scopeOpener; $i < $scopeCloser; $i++ ) {
+ if ( in_array( $tokens[$i]['type'],
PHP_CodeSniffer_Tokens::$emptyTokens ) ) {
+ continue;
+ }
+ if ( $tokens[$i]['type'] === 'T_GLOBAL' ) {
+ $globalLine = $tokens[$i]['line'];
+ }
+ if ( $tokens[$i]['type'] === 'T_VARIABLE' &&
$tokens[$i]['line'] == $globalLine ) {
+ $globalVariables[] = $tokens[$i]['content']
.'#'. $i;
+ }
+ if ( $tokens[$i]['type'] === 'T_VARIABLE' &&
$tokens[$i]['line'] != $globalLine ) {
+ $otherVariables[] = $tokens[$i]['content'];
+ }
+ if ( $tokens[$i]['type'] === 'T_DOUBLE_QUOTED_STRING' )
{
+ preg_match_all( '/[$]\w+/',
$tokens[$i]['content'], $matches );
+ $strVariables = array_merge_recursive(
$strVariables, $matches );
+ }
+ }
+ $strVariables = iterator_to_array(
+ new RecursiveIteratorIterator( new
RecursiveArrayIterator( $strVariables ) ),
+ false
+ );
+ foreach ( $globalVariables as $global ) {
+ $global = explode( '#', $global );
+ if ( !in_array( $global[0], $otherVariables ) &&
!in_array( $global[0], $strVariables ) ) {
+ $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;
+}
diff --git
a/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
new file mode 100644
index 0000000..aa16ff6
--- /dev/null
+++ b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_pass.php
@@ -0,0 +1,6 @@
+<?php
+
+function fooFoo() {
+ global $wgSomething;
+ $foo = $wgSomething + 2;
+}
diff --git
a/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
new file mode 100644
index 0000000..33ecdc7
--- /dev/null
+++
b/MediaWiki/Tests/files/VariableAnalysis/used_global_variables_quote_string_pass.php
@@ -0,0 +1,6 @@
+<?php
+
+function fooFoo() {
+ global $wgSomething;
+ $foo = "foo$wgSomething";
+}
--
To view, visit https://gerrit.wikimedia.org/r/192248
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I856564e2de73c113aa9863efdd69ba65b1d40cb6
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Phoenix303 <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits