Completed
Push — master ( 7d3672...a34d6f )
by Simonas
357:48 queued 292:56
created

DateRange   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 6
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getState() 0 17 4
A preProcessSearch() 0 6 1
A getViewData() 0 13 1
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\FilterManagerBundle\Filter\Widget\Range;
13
14
use ONGR\ElasticsearchDSL\Aggregation\StatsAggregation;
15
use ONGR\ElasticsearchDSL\Search;
16
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Filter\ViewData;
19
use ONGR\FilterManagerBundle\Filter\ViewData\RangeAwareViewData;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * Date range filter, selects documents from lower date to upper date.
24
 */
25
class DateRange extends AbstractRange
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function getState(Request $request)
31
    {
32
        $state = parent::getState($request);
33
34
        if ($state->getValue()) {
35
            $values = explode(';', $state->getValue(), 2);
36
            $gt = $this->isInclusive() ? 'gte' : 'gt';
37
            $lt = $this->isInclusive() ? 'lte' : 'lt';
38
39
            $normalized[$gt] = $values[0];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$normalized was never initialized. Although not strictly required by PHP, it is generally a good practice to add $normalized = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
40
            $normalized[$lt] = $values[1];
41
42
            $state->setValue($normalized);
43
        }
44
45
        return $state;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
52
    {
53
        $stateAgg = new StatsAggregation('date_range_agg');
54
        $stateAgg->setField($this->getField());
55
        $search->addAggregation($stateAgg);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getViewData(DocumentIterator $result, ViewData $data)
62
    {
63
        /** @var $data RangeAwareViewData */
64
        $data->setMinBounds(
65
            new \DateTime('@' . (int) ($result->getAggregation('date_range_agg')->getValue()['min'] / 1000))
0 ignored issues
show
Bug introduced by
The method getValue does only exist in ONGR\ElasticsearchBundle...gation\ValueAggregation, but not in ONGR\ElasticsearchBundle...ion\AggregationIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
        );
67
68
        $data->setMaxBounds(
69
            new \DateTime('@' . (int) ($result->getAggregation('date_range_agg')->getValue()['max'] / 1000))
70
        );
71
72
        return $data;
73
    }
74
}
75