Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/250956

Change subject: Avoid in_array for performance reasons
......................................................................

Avoid in_array for performance reasons

Change-Id: Idb4128ab8179376c7a85b64ac9c895ae858f7cd9
---
M MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
1 file changed, 4 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer 
refs/changes/56/250956/1

diff --git a/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php 
b/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
index 33a3405..c3933af 100644
--- a/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
+++ b/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
@@ -27,17 +27,17 @@
                $strVariables = array();
 
                for ( $i = $scopeOpener; $i < $scopeCloser; $i++ ) {
-                       if ( in_array( $tokens[$i]['type'], 
PHP_CodeSniffer_Tokens::$emptyTokens ) ) {
+                       if ( array_key_exists( $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;
+                               $globalVariables[] = array( 
$tokens[$i]['content'], $i );
                        }
                        if ( $tokens[$i]['type'] === 'T_VARIABLE' && 
$tokens[$i]['line'] != $globalLine ) {
-                               $otherVariables[] = $tokens[$i]['content'];
+                               $otherVariables[$tokens[$i]['content']] = null;
                        }
                        if ( $tokens[$i]['type'] === 'T_DOUBLE_QUOTED_STRING' 
|| $tokens[$i]['type'] === "T_HEREDOC" ) {
                                preg_match_all( '/[$]\w+/', 
$tokens[$i]['content'], $matches );
@@ -49,8 +49,7 @@
                        false
                );
                foreach ( $globalVariables as $global ) {
-                       $global = explode( '#', $global );
-                       if ( !in_array( $global[0], $otherVariables ) && 
!in_array( $global[0], $strVariables ) ) {
+                       if ( !array_key_exists( $global[0], $otherVariables ) 
&& !in_array( $global[0], $strVariables ) ) {
                                $phpcsFile->addWarning( 'Global ' . $global[0] 
.' is never used.', $global[1] );
                        }
                }

-- 
To view, visit https://gerrit.wikimedia.org/r/250956
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idb4128ab8179376c7a85b64ac9c895ae858f7cd9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to