Completed
Push — master ( 3a2d29...d0475e )
by Nicolas
02:57
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
     * Set the field for this aggregation.
37
     *
38
     * @param string $script the name of the document field on which to perform this aggregation
39
     *
40
     * @return $this
41
     */
42
    public function setCombineScript($script)
43
    {
44
        return $this->setParam('combine_script', $script);
45
    }
46
47
    /**
48
     * Set the field for this aggregation.
49
     *
50
     * @param string $script the name of the document field on which to perform this aggregation
51
     *
52
     * @return $this
53
     */
54
    public function setInitScript($script)
55
    {
56
        return $this->setParam('init_script', $script);
57
    }
58
59
    /**
60
     * Set the field for this aggregation.
61
     *
62
     * @param string $script the name of the document field on which to perform this aggregation
63
     *
64
     * @return $this
65
     */
66
    public function setMapScript($script)
67
    {
68
        return $this->setParam('map_script', $script);
69
    }
70
71
    /**
72
     * Set the field for this aggregation.
73
     *
74
     * @param string $script the name of the document field on which to perform this aggregation
75
     *
76
     * @return $this
77
     */
78
    public function setReduceScript($script)
79
    {
80
        return $this->setParam('reduce_script', $script);
81
    }
82
}
83