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    public function addFunction(
21        string $functionType,
22        $functionParams,
23        ?AbstractQuery $filter = null,
24        ?float $weight = null
25    ): FunctionScore {
26        $this->size ++;
27
28        parent::addFunction( $functionType, $functionParams, $filter, $weight );
29        return $this;
30    }
31
32    /**
33     * @return bool true if this function score is empty
34     */
35    public function isEmptyFunction() {
36        return $this->size == 0;
37    }
38
39    /**
40     * @return int the number of added functions.
41     */
42    public function getSize() {
43        return $this->size;
44    }
45
46    /**
47     * Default elastica behaviour is to use class name
48     * as property name. We must override this function
49     * to force the name to function_score
50     *
51     * @return string
52     */
53    protected function _getBaseName() {
54        return "function_score";
55    }
56}