Completed
Push — master ( ec75c5...ca1ddf )
by Nicolas
03:01
created

lib/Elastica/Aggregation/ScriptedMetric.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elastica\Aggregation;
3
4
/**
5
 * Class ScriptedMetric.
6
 *
7
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
8
 */
9
class ScriptedMetric extends AbstractAggregation
10
{
11
    /**
12
     * @param string      $name          the name if this aggregation
13
     * @param string|null $initScript    Executed prior to any collection of documents
14
     * @param string|null $mapScript     Executed once per document collected
15
     * @param string|null $combineScript Executed once on each shard after document collection is complete
16
     * @param string|null $reduceScript  Executed once on the coordinating node after all shards have returned their results
17
     */
18
    public function __construct($name, $initScript = null, $mapScript = null, $combineScript = null, $reduceScript = null)
19
    {
20
        parent::__construct($name);
21
        if ($initScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $initScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
22
            $this->setInitScript($initScript);
23
        }
24
        if ($mapScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mapScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25
            $this->setMapScript($mapScript);
26
        }
27
        if ($combineScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $combineScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            $this->setCombineScript($combineScript);
29
        }
30
        if ($reduceScript) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $reduceScript of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
31
            $this->setReduceScript($reduceScript);
32
        }
33
    }
34
35
    /**
36
     * Executed once on each shard after document collection is complete.
37
     *
38
     * Allows the aggregation to consolidate the state returned from each shard.
39
     * If a combine_script is not provided the combine phase will return the aggregation variable.
40
     *
41
     * @param string $script
42
     *
43
     * @return $this
44
     */
45
    public function setCombineScript($script)
46
    {
47
        return $this->setParam('combine_script', $script);
48
    }
49
50
    /**
51
     * Executed prior to any collection of documents.
52
     *
53
     * Allows the aggregation to set up any initial state.
54
     *
55
     * @param string $script
56
     *
57
     * @return $this
58
     */
59
    public function setInitScript($script)
60
    {
61
        return $this->setParam('init_script', $script);
62
    }
63
64
    /**
65
     * Executed once per document collected.
66
     *
67
     * This is the only required script. If no combine_script is specified, the resulting state needs to be stored in
68
     * an object named _agg.
69
     *
70
     * @param string $script
71
     *
72
     * @return $this
73
     */
74
    public function setMapScript($script)
75
    {
76
        return $this->setParam('map_script', $script);
77
    }
78
79
    /**
80
     * Executed once on the coordinating node after all shards have returned their results.
81
     *
82
     * The script is provided with access to a variable _aggs which is an array of the result of the combine_script on
83
     * each shard. If a reduce_script is not provided the reduce phase will return the _aggs variable.
84
     *
85
     * @param string $script
86
     *
87
     * @return $this
88
     */
89
    public function setReduceScript($script)
90
    {
91
        return $this->setParam('reduce_script', $script);
92
    }
93
}
94