From d0dbbc376c95931fbe4f925b08fc05daabfb64ac Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thiemo=20M=C3=A4ttig?= Date: Tue, 1 Apr 2014 23:44:03 +0200 Subject: [PATCH] Replace in_array( array_keys( ... ) ) with array_key_exists( ... ) It's the same, right? in_array() must search for a value and therefor is expensive. array_key_exists() searches for a key and should be much cheaper. Also easier to read. Change-Id: Ide17d5af13c416c62a40029848ac17ba24eb5563 --- includes/installer/WebInstallerPage.php | 5 ++--- includes/media/Exif.php | 4 ++-- includes/specials/SpecialAllpages.php | 4 ++-- includes/specials/SpecialPrefixindex.php | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/includes/installer/WebInstallerPage.php b/includes/installer/WebInstallerPage.php index 4bc6cad246..69a460a859 100644 --- a/includes/installer/WebInstallerPage.php +++ b/includes/installer/WebInstallerPage.php @@ -1144,8 +1144,7 @@ class WebInstaller_Options extends WebInstallerPage { 'wgEmailAuthentication', 'wgMainCacheType', '_MemCachedServers', 'wgUseInstantCommons' ) ); - if ( !in_array( $this->getVar( '_RightsProfile' ), - array_keys( $this->parent->rightsProfiles ) ) + if ( !array_key_exists( $this->getVar( '_RightsProfile' ), $this->parent->rightsProfiles ) ) { reset( $this->parent->rightsProfiles ); $this->setVar( '_RightsProfile', key( $this->parent->rightsProfiles ) ); @@ -1158,7 +1157,7 @@ class WebInstaller_Options extends WebInstallerPage { return false; } - } elseif ( in_array( $code, array_keys( $this->parent->licenses ) ) ) { + } elseif ( array_key_exists( $code, $this->parent->licenses ) ) { // Messages: // config-license-cc-by, config-license-cc-by-sa, config-license-cc-by-nc-sa, // config-license-cc-0, config-license-pd, config-license-gfdl, config-license-none, diff --git a/includes/media/Exif.php b/includes/media/Exif.php index 1cb5542b74..b39e042eda 100644 --- a/includes/media/Exif.php +++ b/includes/media/Exif.php @@ -319,13 +319,13 @@ class Exif { $this->mFilteredExifData = array(); foreach ( array_keys( $this->mRawExifData ) as $section ) { - if ( !in_array( $section, array_keys( $this->mExifTags ) ) ) { + if ( !array_key_exists( $section, $this->mExifTags ) ) { $this->debug( $section, __FUNCTION__, "'$section' is not a valid Exif section" ); continue; } foreach ( array_keys( $this->mRawExifData[$section] ) as $tag ) { - if ( !in_array( $tag, array_keys( $this->mExifTags[$section] ) ) ) { + if ( !array_key_exists( $tag, $this->mExifTags[$section] ) ) { $this->debug( $tag, __FUNCTION__, "'$tag' is not a valid tag in '$section'" ); continue; } diff --git a/includes/specials/SpecialAllpages.php b/includes/specials/SpecialAllpages.php index 97e7238426..70949d4ec2 100644 --- a/includes/specials/SpecialAllpages.php +++ b/includes/specials/SpecialAllpages.php @@ -97,7 +97,7 @@ class SpecialAllpages extends IncludableSpecialPage { $namespaces = $this->getContext()->getLanguage()->getNamespaces(); $out->setPageTitle( - ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) ? + ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ? $this->msg( 'allinnamespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) : $this->msg( 'allarticles' ) ); @@ -375,7 +375,7 @@ class SpecialAllpages extends IncludableSpecialPage { if ( !$fromList || !$toList ) { $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock(); - } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) { + } elseif ( !array_key_exists( $namespace, $namespaces ) ) { // Show errormessage and reset to NS_MAIN $out = $this->msg( 'allpages-bad-ns', $namespace )->parse(); $namespace = NS_MAIN; diff --git a/includes/specials/SpecialPrefixindex.php b/includes/specials/SpecialPrefixindex.php index 81376518f5..f13fc5924b 100644 --- a/includes/specials/SpecialPrefixindex.php +++ b/includes/specials/SpecialPrefixindex.php @@ -66,7 +66,7 @@ class SpecialPrefixindex extends SpecialAllpages { $namespaces = $wgContLang->getNamespaces(); $out->setPageTitle( - ( $namespace > 0 && in_array( $namespace, array_keys( $namespaces ) ) ) + ( $namespace > 0 && array_key_exists( $namespace, $namespaces ) ) ? $this->msg( 'prefixindex-namespace', str_replace( '_', ' ', $namespaces[$namespace] ) ) : $this->msg( 'prefixindex' ) ); @@ -166,7 +166,7 @@ class SpecialPrefixindex extends SpecialAllpages { if ( !$prefixList || !$fromList ) { $out = $this->msg( 'allpagesbadtitle' )->parseAsBlock(); - } elseif ( !in_array( $namespace, array_keys( $namespaces ) ) ) { + } elseif ( !array_key_exists( $namespace, $namespaces ) ) { // Show errormessage and reset to NS_MAIN $out = $this->msg( 'allpages-bad-ns', $namespace )->parse(); $namespace = NS_MAIN; -- 2.20.1