Use gettype only for debugging text
authorumherirrender <umherirrender_de.wp@web.de>
Fri, 15 Mar 2013 21:50:42 +0000 (22:50 +0100)
committerumherirrender <umherirrender_de.wp@web.de>
Fri, 15 Mar 2013 21:50:42 +0000 (22:50 +0100)
Changed some gettype == 'array', 'object' and similar to is_array,
is_object or similar

Output of gettype must not stable across versions and it is slow

Change-Id: I07bfc063b03be1200989dd6facee66b35ab51d77

includes/api/ApiFormatWddx.php
includes/api/ApiFormatXml.php
includes/media/Exif.php
languages/Language.php
tests/phpunit/includes/db/DatabaseSqliteTest.php

index 62b69bb..884a1dc 100644 (file)
@@ -67,41 +67,36 @@ class ApiFormatWddx extends ApiFormatBase {
                $indstr = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent ) );
                $indstr2 = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent + 2 ) );
                $nl = ( $this->getIsHtml() ? '' : "\n" );
-               switch ( gettype( $elemValue ) ) {
-                       case 'array':
-                               // Check whether we've got an associative array (<struct>)
-                               // or a regular array (<array>)
-                               $cnt = count( $elemValue );
-                               if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
-                                       // Regular array
-                                       $this->printText( $indstr . Xml::element( 'array', array(
-                                               'length' => $cnt ), null ) . $nl );
-                                       foreach ( $elemValue as $subElemValue ) {
-                                               $this->slowWddxPrinter( $subElemValue, $indent + 2 );
-                                       }
-                                       $this->printText( "$indstr</array>$nl" );
-                               } else {
-                                       // Associative array (<struct>)
-                                       $this->printText( "$indstr<struct>$nl" );
-                                       foreach ( $elemValue as $subElemName => $subElemValue ) {
-                                               $this->printText( $indstr2 . Xml::element( 'var', array(
-                                                       'name' => $subElemName
-                                               ), null ) . $nl );
-                                               $this->slowWddxPrinter( $subElemValue, $indent + 4 );
-                                               $this->printText( "$indstr2</var>$nl" );
-                                       }
-                                       $this->printText( "$indstr</struct>$nl" );
+               if ( is_array( $elemValue ) ) {
+                       // Check whether we've got an associative array (<struct>)
+                       // or a regular array (<array>)
+                       $cnt = count( $elemValue );
+                       if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
+                               // Regular array
+                               $this->printText( $indstr . Xml::element( 'array', array(
+                                       'length' => $cnt ), null ) . $nl );
+                               foreach ( $elemValue as $subElemValue ) {
+                                       $this->slowWddxPrinter( $subElemValue, $indent + 2 );
                                }
-                               break;
-                       case 'integer':
-                       case 'double':
-                               $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
-                               break;
-                       case 'string':
-                               $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
-                               break;
-                       default:
-                               ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
+                               $this->printText( "$indstr</array>$nl" );
+                       } else {
+                               // Associative array (<struct>)
+                               $this->printText( "$indstr<struct>$nl" );
+                               foreach ( $elemValue as $subElemName => $subElemValue ) {
+                                       $this->printText( $indstr2 . Xml::element( 'var', array(
+                                               'name' => $subElemName
+                                       ), null ) . $nl );
+                                       $this->slowWddxPrinter( $subElemValue, $indent + 4 );
+                                       $this->printText( "$indstr2</var>$nl" );
+                               }
+                               $this->printText( "$indstr</struct>$nl" );
+                       }
+               } elseif ( is_int( $elemValue ) || is_float( $elemValue ) ) {
+                       $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
+               } elseif ( is_string( $elemValue ) ) {
+                       $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
+               } else {
+                       ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
                }
        }
 
index b4e8e33..183d48c 100644 (file)
@@ -131,84 +131,78 @@ class ApiFormatXml extends ApiFormatBase {
                }
                $elemName = str_replace( ' ', '_', $elemName );
 
-               switch ( gettype( $elemValue ) ) {
-                       case 'array':
-                               if ( isset( $elemValue['*'] ) ) {
-                                       $subElemContent = $elemValue['*'];
-                                       if ( $doublequote ) {
-                                               $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
-                                       }
-                                       unset( $elemValue['*'] );
-
-                                       // Add xml:space="preserve" to the
-                                       // element so XML parsers will leave
-                                       // whitespace in the content alone
-                                       $elemValue['xml:space'] = 'preserve';
-                               } else {
-                                       $subElemContent = null;
+               if ( is_array( $elemValue ) ) {
+                       if ( isset( $elemValue['*'] ) ) {
+                               $subElemContent = $elemValue['*'];
+                               if ( $doublequote ) {
+                                       $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
                                }
-
-                               if ( isset( $elemValue['_element'] ) ) {
-                                       $subElemIndName = $elemValue['_element'];
-                                       unset( $elemValue['_element'] );
-                               } else {
-                                       $subElemIndName = null;
-                               }
-
-                               $indElements = array();
-                               $subElements = array();
-                               foreach ( $elemValue as $subElemId => & $subElemValue ) {
-                                       if ( is_string( $subElemValue ) && $doublequote ) {
-                                               $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
-                                       }
-
-                                       if ( gettype( $subElemId ) === 'integer' ) {
-                                               $indElements[] = $subElemValue;
-                                               unset( $elemValue[$subElemId] );
-                                       } elseif ( is_array( $subElemValue ) ) {
-                                               $subElements[$subElemId] = $subElemValue;
-                                               unset ( $elemValue[$subElemId] );
-                                       }
+                               unset( $elemValue['*'] );
+
+                               // Add xml:space="preserve" to the
+                               // element so XML parsers will leave
+                               // whitespace in the content alone
+                               $elemValue['xml:space'] = 'preserve';
+                       } else {
+                               $subElemContent = null;
+                       }
+
+                       if ( isset( $elemValue['_element'] ) ) {
+                               $subElemIndName = $elemValue['_element'];
+                               unset( $elemValue['_element'] );
+                       } else {
+                               $subElemIndName = null;
+                       }
+
+                       $indElements = array();
+                       $subElements = array();
+                       foreach ( $elemValue as $subElemId => & $subElemValue ) {
+                               if ( is_string( $subElemValue ) && $doublequote ) {
+                                       $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
                                }
 
-                               if ( is_null( $subElemIndName ) && count( $indElements ) ) {
-                                       ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
+                               if ( is_int( $subElemId ) ) {
+                                       $indElements[] = $subElemValue;
+                                       unset( $elemValue[$subElemId] );
+                               } elseif ( is_array( $subElemValue ) ) {
+                                       $subElements[$subElemId] = $subElemValue;
+                                       unset ( $elemValue[$subElemId] );
                                }
+                       }
 
-                               if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
-                                       ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
-                               }
+                       if ( is_null( $subElemIndName ) && count( $indElements ) ) {
+                               ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
+                       }
 
-                               if ( !is_null( $subElemContent ) ) {
-                                       $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
-                               } elseif ( !count( $indElements ) && !count( $subElements ) ) {
-                                       $retval .= $indstr . Xml::element( $elemName, $elemValue );
-                               } else {
-                                       $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
+                       if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
+                               ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
+                       }
 
-                                       foreach ( $subElements as $subElemId => & $subElemValue ) {
-                                               $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
-                                       }
+                       if ( !is_null( $subElemContent ) ) {
+                               $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
+                       } elseif ( !count( $indElements ) && !count( $subElements ) ) {
+                               $retval .= $indstr . Xml::element( $elemName, $elemValue );
+                       } else {
+                               $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
 
-                                       foreach ( $indElements as &$subElemValue ) {
-                                               $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
-                                       }
-
-                                       $retval .= $indstr . Xml::closeElement( $elemName );
+                               foreach ( $subElements as $subElemId => & $subElemValue ) {
+                                       $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
                                }
-                               break;
-                       case 'object':
-                               // ignore
-                               break;
-                       default:
-                               // to make sure null value doesn't produce unclosed element,
-                               // which is what Xml::element( $elemName, null, null ) returns
-                               if ( $elemValue === null ) {
-                                       $retval .= $indstr . Xml::element( $elemName );
-                               } else {
-                                       $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
+
+                               foreach ( $indElements as &$subElemValue ) {
+                                       $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
                                }
-                               break;
+
+                               $retval .= $indstr . Xml::closeElement( $elemName );
+                       }
+               } elseif ( !is_object( $elemValue ) ) {
+                       // to make sure null value doesn't produce unclosed element,
+                       // which is what Xml::element( $elemName, null, null ) returns
+                       if ( $elemValue === null ) {
+                               $retval .= $indstr . Xml::element( $elemName );
+                       } else {
+                               $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
+                       }
                }
                return $retval;
        }
index c50b2f0..e0b75c8 100644 (file)
@@ -808,7 +808,7 @@ class Exif {
                }
                $type = gettype( $in );
                $class = ucfirst( __CLASS__ );
-               if ( $type === 'array' ) {
+               if ( is_array( $in ) ) {
                        $in = print_r( $in, true );
                }
 
index 01751db..50d8676 100644 (file)
@@ -349,12 +349,12 @@ class Language {
        public static function isValidBuiltInCode( $code ) {
 
                if ( !is_string( $code ) ) {
-                       $type = gettype( $code );
-                       if ( $type === 'object' ) {
+                       if ( is_object( $code ) ) {
                                $addmsg = " of class " . get_class( $code );
                        } else {
                                $addmsg = '';
                        }
+                       $type = gettype( $code );
                        throw new MWException( __METHOD__ . " must be passed a string, $type given$addmsg" );
                }
 
index 7b84d47..097e57a 100644 (file)
@@ -311,7 +311,7 @@ class DatabaseSqliteTest extends MediaWikiTestCase {
                        $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ ), "Database creationg" );
                $this->assertTrue( $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ ),
                        "Insertion worked" );
-               $this->assertEquals( "integer", gettype( $db->insertId() ), "Actual typecheck" );
+               $this->assertInternalType( 'integer', $db->insertId(), "Actual typecheck" );
                $this->assertTrue( $db->close(), "closing database" );
        }