Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.30% covered (success)
91.30%
21 / 23
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
LabelsDescriptionsFieldTrait
91.30% covered (success)
91.30%
21 / 23
33.33% covered (danger)
33.33%
1 / 3
13.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEngineHints
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 merge
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
10.01
1<?php
2
3declare( strict_types = 1 );
4
5namespace Wikibase\Search\Elastic\Fields;
6
7use CirrusSearch\CirrusSearch;
8use MediaWiki\Search\SearchEngine;
9use MediaWiki\Search\SearchIndexField;
10
11/**
12 * Trait for code shared between {@link LabelsField} and {@link DescriptionsField}.
13 */
14trait LabelsDescriptionsFieldTrait {
15
16    /**
17     * @var string
18     */
19    protected $type;
20
21    /**
22     * @param string[] $languages List of available languages
23     * @param array $stemmingSettings Stemming config
24     */
25    public function __construct(
26        private readonly array $languages,
27        private readonly array $stemmingSettings,
28    ) {
29        /* @phan-suppress-next-line PhanTraitParentReference, PhanUndeclaredConstantOfClass */
30        parent::__construct( static::NAME, SearchIndexField::INDEX_TYPE_NESTED );
31    }
32
33    /**
34     * Set engine hints.
35     * Specifically, sets noop hint so that labels/descriptions would be compared
36     * as arrays and removal of labels/descriptions would be processed correctly.
37     * @param SearchEngine $engine
38     * @return array
39     */
40    public function getEngineHints( SearchEngine $engine ) {
41        if ( !( $engine instanceof CirrusSearch ) ) {
42            // For now only Cirrus/Elastic is supported
43            return [];
44        }
45        return [ \CirrusSearch\Search\CirrusIndexField::NOOP_HINT => "equals" ];
46    }
47
48    /** @inheritDoc */
49    public function merge( SearchIndexField $that ) {
50        if ( !( $that instanceof self ) || $this->type !== $that->type ) {
51            return false;
52        }
53
54        if (
55            $this->stemmingSettings == $that->stemmingSettings ||
56            $that->stemmingSettings === []
57        ) {
58            $mergedStemmingSettings = $this->stemmingSettings;
59        } elseif ( $this->stemmingSettings === [] ) {
60            $mergedStemmingSettings = $that->stemmingSettings;
61        } else {
62            return false;
63        }
64
65        $mergedLanguages = array_values( array_unique( array_merge(
66            $this->languages,
67            $that->languages
68        ) ) );
69
70        if (
71            $this->languages === $mergedLanguages &&
72            $this->stemmingSettings === $mergedStemmingSettings
73        ) {
74            return $this;
75        } elseif (
76            $that->languages === $mergedLanguages &&
77            $that->stemmingSettings === $mergedStemmingSettings
78        ) {
79            return $that;
80        } else {
81            /* @phan-suppress-next-line PhanTypeInstantiateTraitStaticOrSelf */
82            return new self( $mergedLanguages, $mergedStemmingSettings );
83        }
84    }
85
86}