From 3dd97bf6511bfd2773f616241e9e35445882901b Mon Sep 17 00:00:00 2001 From: umherirrender Date: Fri, 15 Mar 2013 22:50:42 +0100 Subject: [PATCH] Use gettype only for debugging text 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 | 63 ++++----- includes/api/ApiFormatXml.php | 130 +++++++++--------- includes/media/Exif.php | 2 +- languages/Language.php | 4 +- .../includes/db/DatabaseSqliteTest.php | 2 +- 5 files changed, 95 insertions(+), 106 deletions(-) diff --git a/includes/api/ApiFormatWddx.php b/includes/api/ApiFormatWddx.php index 62b69bb677..884a1dcdba 100644 --- a/includes/api/ApiFormatWddx.php +++ b/includes/api/ApiFormatWddx.php @@ -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 () - // or a regular 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$nl" ); - } else { - // Associative array () - $this->printText( "$indstr$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$nl" ); - } - $this->printText( "$indstr$nl" ); + if ( is_array( $elemValue ) ) { + // Check whether we've got an associative array () + // or a regular 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$nl" ); + } else { + // Associative array () + $this->printText( "$indstr$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$nl" ); + } + $this->printText( "$indstr$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 ) ); } } diff --git a/includes/api/ApiFormatXml.php b/includes/api/ApiFormatXml.php index b4e8e330e6..183d48c44f 100644 --- a/includes/api/ApiFormatXml.php +++ b/includes/api/ApiFormatXml.php @@ -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; } diff --git a/includes/media/Exif.php b/includes/media/Exif.php index c50b2f05f3..e0b75c8130 100644 --- a/includes/media/Exif.php +++ b/includes/media/Exif.php @@ -808,7 +808,7 @@ class Exif { } $type = gettype( $in ); $class = ucfirst( __CLASS__ ); - if ( $type === 'array' ) { + if ( is_array( $in ) ) { $in = print_r( $in, true ); } diff --git a/languages/Language.php b/languages/Language.php index 01751db7ed..50d8676fee 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -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" ); } diff --git a/tests/phpunit/includes/db/DatabaseSqliteTest.php b/tests/phpunit/includes/db/DatabaseSqliteTest.php index 7b84d4712e..097e57aaa4 100644 --- a/tests/phpunit/includes/db/DatabaseSqliteTest.php +++ b/tests/phpunit/includes/db/DatabaseSqliteTest.php @@ -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" ); } -- 2.20.1