Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.43% covered (warning)
71.43%
10 / 14
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchResultTrait
76.92% covered (warning)
76.92%
10 / 13
50.00% covered (danger)
50.00%
1 / 2
5.31
0.00% covered (danger)
0.00%
0 / 1
 getExtensionData
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setExtensionData
70.00% covered (warning)
70.00%
7 / 10
0.00% covered (danger)
0.00%
0 / 1
3.24
1<?php
2
3namespace MediaWiki\Search;
4
5use Closure;
6use InvalidArgumentException;
7
8/**
9 * Trait for SearchResult subclasses to share non-obvious behaviors or methods
10 * that rarely specialized
11 */
12trait SearchResultTrait {
13    /**
14     * A function returning a set of extension data.
15     * @var Closure|null
16     */
17    protected $extensionData;
18
19    /**
20     * Get the extension data as:
21     * augmentor name => data
22     * @return array[]
23     */
24    public function getExtensionData() {
25        if ( $this->extensionData ) {
26            return ( $this->extensionData )();
27        } else {
28            return [];
29        }
30    }
31
32    /**
33     * Set extension data for this result.
34     * The data is:
35     * augmentor name => data
36     * @param Closure|array $extensionData Takes no arguments, returns
37     *  either array of extension data or null.
38     */
39    public function setExtensionData( $extensionData ) {
40        if ( $extensionData instanceof Closure ) {
41            $this->extensionData = $extensionData;
42        } elseif ( is_array( $extensionData ) ) {
43            wfDeprecated( __METHOD__ . ' with array argument', '1.32' );
44            $this->extensionData = static function () use ( $extensionData ) {
45                return $extensionData;
46            };
47        } else {
48            $type = get_debug_type( $extensionData );
49            throw new InvalidArgumentException(
50                __METHOD__ . " must be called with Closure|array, but received $type" );
51        }
52    }
53}
54
55/** @deprecated class alias since 1.46 */
56class_alias( SearchResultTrait::class, 'SearchResultTrait' );