Replace in_array( array_keys( ... ) ) with array_key_exists( ... )
authorThiemo Mättig <thiemo.maettig@wikimedia.de>
Tue, 1 Apr 2014 21:44:03 +0000 (23:44 +0200)
committerThiemo Mättig <thiemo.maettig@wikimedia.de>
Tue, 1 Apr 2014 21:44:03 +0000 (23:44 +0200)
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
includes/media/Exif.php
includes/specials/SpecialAllpages.php
includes/specials/SpecialPrefixindex.php

index 4bc6cad..69a460a 100644 (file)
@@ -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,
index 1cb5542..b39e042 100644 (file)
@@ -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;
                                }
index 97e7238..70949d4 100644 (file)
@@ -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;
index 8137651..f13fc59 100644 (file)
@@ -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;