Replace gettype() with get_debug_type() in debug/log/test output

get_debug_type() does the same thing but better (spelling type names
in the same way as in type declarations, and including names of
object classes and resource types). It was added in PHP 8, but the
symfony/polyfill-php80 package provides it while we still support 7.4.

Also remove uses of get_class() and get_resource_type() where the new
method already provides the same information.

For reference:
https://www.php.net/manual/en/function.get-debug-type.php
https://www.php.net/manual/en/function.gettype.php

In this commit I'm only changing code where it looks like the result
is used only for some king of debug, log, or test output. This
probably won't break anything important, but I'm not sure whether
anything might depend on the exact values.

Change-Id: I7c1f0a8f669228643e86f8e511c0e26a2edb2948
This commit is contained in:
Bartosz Dziewoński 2024-07-31 18:56:55 +02:00
parent c045fa0291
commit df4cbf5ac6
15 changed files with 21 additions and 26 deletions

View file

@ -455,7 +455,7 @@ class LegacyLogger extends AbstractLogger {
return '[Resource ' . get_resource_type( $item ) . ']';
}
return '[Unknown ' . gettype( $item ) . ']';
return '[Unknown ' . get_debug_type( $item ) . ']';
}
/**

View file

@ -482,9 +482,7 @@ TXT;
public static function redactTrace( array $trace ) {
return array_map( static function ( $frame ) {
if ( isset( $frame['args'] ) ) {
$frame['args'] = array_map( static function ( $arg ) {
return is_object( $arg ) ? get_class( $arg ) : gettype( $arg );
}, $frame['args'] );
$frame['args'] = array_map( 'get_debug_type', $frame['args'] );
}
return $frame;
}, $trace );

View file

@ -871,7 +871,7 @@ class LocalFile extends File {
} else {
$logger = LoggerFactory::getInstance( 'LocalFile' );
$logger->warning( __METHOD__ . ' given invalid metadata of type ' .
gettype( $info['metadata'] ) );
get_debug_type( $info['metadata'] ) );
$this->metadataArray = [];
}
$this->extraDataLoaded = true;

View file

@ -384,7 +384,7 @@ abstract class Job implements RunnableJob {
foreach ( $value as $k => $v ) {
$json = FormatJson::encode( $v );
if ( $json === false || mb_strlen( $json ) > 512 ) {
$filteredValue[$k] = gettype( $v ) . '(...)';
$filteredValue[$k] = get_debug_type( $v ) . '(...)';
} else {
$filteredValue[$k] = $v;
}
@ -395,7 +395,7 @@ abstract class Job implements RunnableJob {
$value = "array(" . count( $value ) . ")";
}
} elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
$value = "object(" . get_class( $value ) . ")";
$value = get_debug_type( $value );
}
$flatValue = (string)$value;

View file

@ -287,7 +287,7 @@ class JsonCodec implements JsonDeserializer, JsonSerializer {
}
}
} elseif ( !is_scalar( $value ) && $value !== null ) {
return $accumulatedPath . ': nonscalar ' . gettype( $value );
return $accumulatedPath . ': nonscalar ' . get_debug_type( $value );
}
return null;
}

View file

@ -18,7 +18,7 @@ class Placeholder {
if ( is_object( $value ) ) {
$this->desc = get_class( $value ) . '#' . spl_object_id( $value );
} else {
$this->desc = gettype( $value );
$this->desc = get_debug_type( $value );
}
}
}

View file

@ -497,10 +497,7 @@ class StatusValue implements Stringable {
$errorcount = "no errors detected";
}
if ( isset( $this->value ) ) {
$valstr = gettype( $this->value ) . " value set";
if ( is_object( $this->value ) ) {
$valstr .= "\"" . get_class( $this->value ) . "\" instance";
}
$valstr = get_debug_type( $this->value ) . " value set";
} else {
$valstr = "no value set";
}

View file

@ -950,7 +950,7 @@ class Exif {
if ( !$this->log ) {
return;
}
$type = gettype( $in );
$type = get_debug_type( $in );
$class = ucfirst( __CLASS__ );
if ( is_array( $in ) ) {
$in = print_r( $in, true );

View file

@ -440,7 +440,7 @@ class MergeHistory {
. 'to return Content object from ContentHandler::makeRedirectContent().'
. ' {value} returned instead.',
[
'value' => gettype( $newContent ),
'value' => get_debug_type( $newContent ),
'model' => $sourceModel
]
);

View file

@ -48,7 +48,7 @@ class JobTest extends MediaWikiIntegrationTestCase {
],
[
$this->getMockJob( [ (object)[] ] ),
'someCommand Special: 0=object(stdClass) ' . $requestId
'someCommand Special: 0=stdClass ' . $requestId
],
[
$this->getMockJob( [ $mockToStringObj ] ),

View file

@ -181,7 +181,7 @@ trait SerializationTestTrait {
if ( is_array( $expected ) ) {
$this->validateArrayEquality( $expected, $actual, $propName );
} elseif ( is_object( $expected ) ) {
$this->assertIsObject( $actual, "Expected an object, but found: " . gettype( $actual ) );
$this->assertIsObject( $actual, "Expected an object, but found: " . get_debug_type( $actual ) );
$this->validateObjectEquality( $expected, $actual );
} else {
$this->assertSame( $expected, $actual, $propName );

View file

@ -324,7 +324,7 @@ class MagicVariableTest extends MediaWikiIntegrationTestCase {
$msg = sprintf( "Magic %s should be <%s:%s>",
$magic,
$expected,
gettype( $expected )
get_debug_type( $expected )
);
$this->assertSame(

View file

@ -133,7 +133,7 @@ TEXT;
/**
* @dataProvider provideJsonSerializedKeys
* @param string $expectedKeyType Type expected as returned by gettype()
* @param string $expectedKeyType Type expected as returned by get_debug_type()
* @param string $exClass An exception class (ie: Exception, MWException)
* @param string $key Name of the key to validate in the serialized JSON
*/
@ -142,7 +142,7 @@ TEXT;
MWExceptionHandler::jsonSerializeException( new $exClass() )
);
$this->assertObjectHasProperty( $key, $json );
$this->assertSame( $expectedKeyType, gettype( $json->$key ), "Type of the '$key' key" );
$this->assertSame( $expectedKeyType, get_debug_type( $json->$key ), "Type of the '$key' key" );
}
/**
@ -152,9 +152,9 @@ TEXT;
foreach ( [ Exception::class, MWException::class ] as $exClass ) {
yield [ 'string', $exClass, 'id' ];
yield [ 'string', $exClass, 'file' ];
yield [ 'integer', $exClass, 'line' ];
yield [ 'int', $exClass, 'line' ];
yield [ 'string', $exClass, 'message' ];
yield [ 'NULL', $exClass, 'url' ];
yield [ 'null', $exClass, 'url' ];
// Backtrace only enabled with wgLogExceptionBacktrace = true
yield [ 'array', $exClass, 'backtrace' ];
}

View file

@ -18,7 +18,7 @@ class PlaceholderTest extends TestCase {
],
[
1,
'/^integer$/'
'/^int$/'
],
[
'test',

View file

@ -36,12 +36,12 @@ class StatusValueTest extends MediaWikiUnitTestCase {
yield [
true, 42, null,
'<OK, no errors detected, integer value set>',
'<OK, no errors detected, int value set>',
'Simple int, good state'
];
yield [
false, 42, null,
'<Error, no errors detected, integer value set>',
'<Error, no errors detected, int value set>',
'Simple int, error state'
];
@ -80,7 +80,7 @@ class StatusValueTest extends MediaWikiUnitTestCase {
yield [
false, 42, [ 'This is the error' ],
'<Error, collected 1 message(s) on the way, integer value set>' . $basicErrorReport,
'<Error, collected 1 message(s) on the way, int value set>' . $basicErrorReport,
'Simple int, string error, error state'
];