wiki.techinc.nl/includes/search/SearchResultTrait.php
Bartosz Dziewoński c045fa0291 Replace gettype() with get_debug_type() in exception messages
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

To keep this safe and simple to review, I'm only changing cases where
the type is immediately used in an exception message.

Change-Id: I325efcddcb58be63b1592b9c20ac0845393c15e2
2024-07-31 19:24:39 +02:00

48 lines
1.2 KiB
PHP

<?php
/**
* Trait for SearchResult subclasses to share non-obvious behaviors or methods
* that rarely specialized
*/
trait SearchResultTrait {
/**
* A function returning a set of extension data.
* @var Closure|null
*/
protected $extensionData;
/**
* Get the extension data as:
* augmentor name => data
* @return array[]
*/
public function getExtensionData() {
if ( $this->extensionData ) {
return call_user_func( $this->extensionData );
} else {
return [];
}
}
/**
* Set extension data for this result.
* The data is:
* augmentor name => data
* @param Closure|array $extensionData Takes no arguments, returns
* either array of extension data or null.
*/
public function setExtensionData( $extensionData ) {
if ( $extensionData instanceof Closure ) {
$this->extensionData = $extensionData;
} elseif ( is_array( $extensionData ) ) {
wfDeprecated( __METHOD__ . ' with array argument', '1.32' );
$this->extensionData = static function () use ( $extensionData ) {
return $extensionData;
};
} else {
$type = get_debug_type( $extensionData );
throw new InvalidArgumentException(
__METHOD__ . " must be called with Closure|array, but received $type" );
}
}
}