r60974 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60973‎ | r60974 | r60975 >
Date:18:35, 12 January 2010
Author:mah
Status:ok (Comments)
Tags:
Comment:
follow up r60961 just test for truth rather than using isset. Found a bug in the process.
Modified paths:
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)
  • /trunk/phase3/tests/LanguageConverterTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/LanguageConverterTest.php
@@ -81,10 +81,11 @@
8282 $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true));
8383
8484 $wgRequest->setVal('variant', 'tg');
 85+ $wgUser->setOption('variant', 'tg-latn');
8586 $this->lc = new TestConverter( $this->lang, 'tg',
8687 array( 'tg', 'tg-latn' ) );
87 - $this->assertEquals('tg', $this->lc->getPreferredVariant(true, false));
88 - $this->assertEquals('tg', $this->lc->getPreferredVariant(true, true));
 88+ $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, false));
 89+ $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true));
8990
9091 $wgRequest->setVal('variant', null);
9192 $wgDefaultLanguageVariant = 'tg-latn';
Index: trunk/phase3/languages/LanguageConverter.php
@@ -124,7 +124,7 @@
125125 * @public
126126 */
127127 function getVariantFallbacks( $v ) {
128 - if ( isset( $this->mVariantFallbacks[$v] ) ) {
 128+ if ( array_key_exists( $v, $this->mVariantFallbacks ) ) {
129129 return $this->mVariantFallbacks[$v];
130130 }
131131 return $this->mMainLanguageCode;
@@ -142,15 +142,15 @@
143143
144144 $req = $this->getURLVariant();
145145
146 - if ( $fromUser && !isset( $req ) ) {
 146+ if ( $fromUser && !$req ) {
147147 $req = $this->getUserVariant();
148148 }
149149
150 - if ( $fromHeader && !isset( $req ) ) {
 150+ if ( $fromHeader && !$req ) {
151151 $req = $this->getHeaderVariant();
152152 }
153153
154 - if ( $wgDefaultLanguageVariant && !isset( $req ) ) {
 154+ if ( $wgDefaultLanguageVariant && !$req ) {
155155 $req = $this->validateVariant( $wgDefaultLanguageVariant );
156156 }
157157
@@ -158,7 +158,7 @@
159159 // not memoized (i.e. there return value is not cached) since
160160 // new information might appear during processing after this
161161 // is first called.
162 - if ( isset( $req ) ) {
 162+ if ( $req ) {
163163 return $req;
164164 }
165165 return $this->mMainLanguageCode;
@@ -170,7 +170,7 @@
171171 * @returns mixed returns the variant if it is valid, null otherwise
172172 */
173173 function validateVariant( $v = null ) {
174 - if ( isset( $v ) && in_array( $v, $this->mVariants ) ) {
 174+ if ( $v !== null && in_array( $v, $this->mVariants ) ) {
175175 return $v;
176176 }
177177 return null;
@@ -192,7 +192,7 @@
193193 // see if the preference is set in the request
194194 $ret = $wgRequest->getText( 'variant' );
195195
196 - if ( !isset( $ret ) ) {
 196+ if ( $ret ) {
197197 $ret = $wgRequest->getVal( 'uselang' );
198198 }
199199
@@ -246,7 +246,7 @@
247247 // http header.
248248
249249 $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' );
250 - if ( !$acceptLanguage ) { // not using isset because getHeader returns false
 250+ if ( !$acceptLanguage ) {
251251 return null;
252252 }
253253
@@ -269,7 +269,7 @@
270270 // strip whitespace
271271 $language = trim( $language );
272272 $this->mHeaderVariant = $this->validateVariant( $language );
273 - if ( isset( $this->mHeaderVariant ) ) {
 273+ if ( $this->mHeaderVariant ) {
274274 break;
275275 }
276276
@@ -286,12 +286,12 @@
287287 }
288288 }
289289
290 - if ( !isset( $this->mHeaderVariant ) ) {
 290+ if ( !$this->mHeaderVariant ) {
291291 // process fallback languages now
292292 $fallback_languages = array_unique( $fallback_languages );
293293 foreach ( $fallback_languages as $language ) {
294294 $this->mHeaderVariant = $this->validateVariant( $language );
295 - if ( isset( $this->mHeaderVariant ) ) {
 295+ if ( $this->mHeaderVariant ) {
296296 break;
297297 }
298298 }
@@ -710,7 +710,7 @@
711711 wfProfileOut( __METHOD__ . '-cache' );
712712 }
713713 if ( !$this->mTables
714 - || !isset( $this->mTables[self::CACHE_VERSION_KEY] ) ) {
 714+ || !array_key_exists( self::CACHE_VERSION_KEY, $this->mTables ) ) {
715715 wfProfileIn( __METHOD__ . '-recache' );
716716 // not in cache, or we need a fresh reload.
717717 // we will first load the default tables

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r60961Further logic cleanup on getPreferredVariant & memoization of functions where...mah05:31, 12 January 2010

Comments

#Comment by Tim Starling (talk | contribs)   07:33, 21 January 2010

isset($a[$k]) is 3 or 4 times faster than array_key_exists($k,$a), that's why we use isset() when we can. In previous versions of PHP, array_key_exists() was reported to be O(N) in array size, and isset() O(1), but thankfully that doesn't seem to be true anymore.

isset() ignores warnings, like empty(), so it doesn't make sense to use it in cases where you don't want to ignore warnings. So most of your changes here are very sensible. isset() is basically $x === null except with warnings suppressed.

Status & tagging log