Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
5 / 6
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FunctionScoreDecorator
83.33% covered (warning)
83.33%
5 / 6
75.00% covered (warning)
75.00%
3 / 4
4.07
0.00% covered (danger)
0.00%
0 / 1
 addFunction
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isEmptyFunction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 _getBaseName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Search\Rescore;
4
5use Elastica\Query\AbstractQuery;
6use Elastica\Query\FunctionScore;
7
8/**
9 * This is useful to check if the function score is empty
10 * Function score builders may not add any function if some
11 * criteria are not met. If there's no function we should not
12 * not build the rescore query.
13 * @todo find another pattern to deal with this problem and avoid
14 * this strong dependency to FunctionScore::addFunction signature.
15 */
16class FunctionScoreDecorator extends FunctionScore {
17    /** @var int */
18    private $size = 0;
19
20    /** @inheritDoc */
21    public function addFunction(
22        string $functionType,
23        $functionParams,
24        ?AbstractQuery $filter = null,
25        ?float $weight = null
26    ): FunctionScore {
27        $this->size++;
28
29        parent::addFunction( $functionType, $functionParams, $filter, $weight );
30        return $this;
31    }
32
33    /**
34     * @return bool true if this function score is empty
35     */
36    public function isEmptyFunction() {
37        return $this->size == 0;
38    }
39
40    /**
41     * @return int the number of added functions.
42     */
43    public function getSize() {
44        return $this->size;
45    }
46
47    /**
48     * Default elastica behaviour is to use class name
49     * as property name. We must override this function
50     * to force the name to function_score
51     *
52     * @return string
53     */
54    protected function _getBaseName() {
55        return "function_score";
56    }
57}