jenkins-bot has submitted this change and it was merged.
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, 9 insertions(+), 6 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
index 03421aa..688dbb5 100644
--- a/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
+++ b/MediaWiki/Sniffs/VariableAnalysis/UnusedGlobalVariablesSniff.php
@@ -27,19 +27,21 @@
$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" ) {
+ if ( $tokens[$i]['type'] === 'T_DOUBLE_QUOTED_STRING'
+ || $tokens[$i]['type'] === 'T_HEREDOC'
+ ) {
preg_match_all( '/\$\w+/',
$tokens[$i]['content'], $matches );
$strVariables = array_merge_recursive(
$strVariables, $matches );
}
@@ -49,8 +51,9 @@
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: merged
Gerrit-Change-Id: Idb4128ab8179376c7a85b64ac9c895ae858f7cd9
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Phoenix303 <[email protected]>
Gerrit-Reviewer: Polybuildr <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits