Completed
Push — master ( 6b6a70...d90581 )
by Simonas
02:43 queued 51s
created

Bucketing/AutoDateHistogramAggregation.php (1 issue)

Check for loose comparison of integers.

Best Practice Bug Major

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
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
18
/**
19
 * Class representing AutoDateHistogramAggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-autodatehistogram-aggregation.html
22
 */
23
class AutoDateHistogramAggregation extends AbstractAggregation
24
{
25
    use BucketingTrait;
26
27
    /**
28
     * Inner aggregations container init.
29
     *
30
     * @param string $name
31
     * @param string $field
32
     * @param int    $buckets
33
     * @param string $format
34
     */
35
    public function __construct($name, $field, $buckets = null, $format = null)
36
    {
37
        parent::__construct($name);
38
39
        $this->setField($field);
40
41
        if ($buckets) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $buckets of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
42
            $this->addParameter('buckets', $buckets);
43
        }
44
45
        if ($format) {
46
            $this->addParameter('format', $format);
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getArray()
54
    {
55
        $data = array_filter(
56
            [
57
                'field' => $this->getField(),
58
            ]
59
        );
60
61
        return $data;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getType()
68
    {
69
        return 'auto_date_histogram';
70
    }
71
}
72