wiki.techinc.nl/includes/search/SearchResultTrait.php
Umherirrender 8de3b7d324 Use static closures where safe to use
This is micro-optimization of closure code to avoid binding the closure
to $this where it is not needed.

Created by I25a17fb22b6b669e817317a0f45051ae9c608208

Change-Id: I0ffc6200f6c6693d78a3151cb8cea7dce7c21653
2021-02-11 00:13:52 +00:00

50 lines
1.3 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 = is_object( $extensionData )
? get_class( $extensionData )
: gettype( $extensionData );
throw new \InvalidArgumentException(
__METHOD__ . " must be called with Closure|array, but received $type" );
}
}
}