Umherirrender has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/54019


Change subject: Use gettype only for debugging text
......................................................................

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
---
M includes/api/ApiFormatWddx.php
M includes/api/ApiFormatXml.php
M includes/media/Exif.php
M languages/Language.php
M tests/phpunit/includes/db/DatabaseSqliteTest.php
5 files changed, 95 insertions(+), 107 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/19/54019/1

diff --git a/includes/api/ApiFormatWddx.php b/includes/api/ApiFormatWddx.php
index 62b69bb..884a1dc 100644
--- a/includes/api/ApiFormatWddx.php
+++ b/includes/api/ApiFormatWddx.php
@@ -67,41 +67,36 @@
                $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 ) );
                }
        }
 
diff --git a/includes/api/ApiFormatXml.php b/includes/api/ApiFormatXml.php
index b4e8e33..744785d 100644
--- a/includes/api/ApiFormatXml.php
+++ b/includes/api/ApiFormatXml.php
@@ -131,84 +131,77 @@
                }
                $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 );
                                }
+                               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;
+                               $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 );
                                }
 
-                               $indElements = array();
-                               $subElements = array();
-                               foreach ( $elemValue as $subElemId => & 
$subElemValue ) {
-                                       if ( is_string( $subElemValue ) && 
$doublequote ) {
-                                               $subElemValue = 
Sanitizer::encodeAttribute( $subElemValue );
-                                       }
+                               if ( is_int( $subElemId ) === 'integer' ) {
+                                       $indElements[] = $subElemValue;
+                                       unset( $elemValue[$subElemId] );
+                               } elseif ( is_array( $subElemValue ) ) {
+                                       $subElements[$subElemId] = 
$subElemValue;
+                                       unset ( $elemValue[$subElemId] );
+                               }
+                       }
 
-                                       if ( gettype( $subElemId ) === 
'integer' ) {
-                                               $indElements[] = $subElemValue;
-                                               unset( $elemValue[$subElemId] );
-                                       } elseif ( is_array( $subElemValue ) ) {
-                                               $subElements[$subElemId] = 
$subElemValue;
-                                               unset ( $elemValue[$subElemId] 
);
-                                       }
+                       if ( is_null( $subElemIndName ) && count( $indElements 
) ) {
+                               ApiBase::dieDebug( __METHOD__, "($elemName, 
...) has integer keys without _element value. Use 
ApiResult::setIndexedTagName()." );
+                       }
+
+                       if ( count( $subElements ) && count( $indElements ) && 
!is_null( $subElemContent ) ) {
+                               ApiBase::dieDebug( __METHOD__, "($elemName, 
...) has content and subelements" );
+                       }
+
+                       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 ( $subElements as $subElemId => & 
$subElemValue ) {
+                                       $retval .= self::recXmlPrint( 
$subElemId, $subElemValue, $indent );
                                }
 
-                               if ( is_null( $subElemIndName ) && count( 
$indElements ) ) {
-                                       ApiBase::dieDebug( __METHOD__, 
"($elemName, ...) has integer keys without _element value. Use 
ApiResult::setIndexedTagName()." );
+                               foreach ( $indElements as &$subElemValue ) {
+                                       $retval .= self::recXmlPrint( 
$subElemIndName, $subElemValue, $indent );
                                }
 
-                               if ( count( $subElements ) && count( 
$indElements ) && !is_null( $subElemContent ) ) {
-                                       ApiBase::dieDebug( __METHOD__, 
"($elemName, ...) has content and subelements" );
-                               }
-
-                               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 ( $subElements as $subElemId => 
& $subElemValue ) {
-                                               $retval .= self::recXmlPrint( 
$subElemId, $subElemValue, $indent );
-                                       }
-
-                                       foreach ( $indElements as 
&$subElemValue ) {
-                                               $retval .= self::recXmlPrint( 
$subElemIndName, $subElemValue, $indent );
-                                       }
-
-                                       $retval .= $indstr . Xml::closeElement( 
$elemName );
-                               }
-                               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 );
-                               }
-                               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 c50b2f0..66bb0b2 100644
--- a/includes/media/Exif.php
+++ b/includes/media/Exif.php
@@ -806,12 +806,12 @@
                if ( !$this->log ) {
                        return;
                }
-               $type = gettype( $in );
                $class = ucfirst( __CLASS__ );
-               if ( $type === 'array' ) {
+               if ( is_array( $in ) ) {
                        $in = print_r( $in, true );
                }
 
+               $type = gettype( $in );
                if ( $action === true ) {
                        wfDebugLog( $this->log, "$class::$fname: accepted: 
'$in' (type: $type)\n" );
                } elseif ( $action === false ) {
diff --git a/languages/Language.php b/languages/Language.php
index 01751db..50d8676 100644
--- a/languages/Language.php
+++ b/languages/Language.php
@@ -349,12 +349,12 @@
        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 7b84d47..097e57a 100644
--- a/tests/phpunit/includes/db/DatabaseSqliteTest.php
+++ b/tests/phpunit/includes/db/DatabaseSqliteTest.php
@@ -311,7 +311,7 @@
                        $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" );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/54019
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I07bfc063b03be1200989dd6facee66b35ab51d77
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to