Completed
Push — master ( 6734fa...551366 )
by
unknown
07:45
created

src/Filter/DateFilter.php (1 issue)

Check for implicit conversion of array to boolean.

Best Practice Bug Minor

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 Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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 Sonata\DoctrinePHPCRAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\AdminBundle\Form\Type\Filter\DateType;
16
17
class DateFilter extends Filter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
23
    {
24
        if (!$data || !is_array($data) || !isset($data['value'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
25
            return;
26
        }
27
28
        $data['type'] = isset($data['type']) ? $data['type'] : DateType::TYPE_EQUAL;
29
30
        $where = $this->getWhere($proxyQuery);
31
32
        $from = $data['value'];
33
        $to = new \DateTime($from->format('Y-m-d').' +86399 seconds'); // 23 hours 59 minutes 59 seconds
34
35
        switch ($data['type']) {
36
            case DateType::TYPE_GREATER_EQUAL:
37
                $where->gte()->field('a.'.$field)->literal($from);
38
39
                break;
40
            case DateType::TYPE_GREATER_THAN:
41
                $where->gt()->field('a.'.$field)->literal($from);
42
43
                break;
44
            case DateType::TYPE_LESS_EQUAL:
45
                $where->lte()->field('a.'.$field)->literal($from);
46
47
                break;
48
            case DateType::TYPE_LESS_THAN:
49
                $where->lt()->field('a.'.$field)->literal($from);
50
51
                break;
52
            case DateType::TYPE_NULL:
53
                $where->eq()->field('a.'.$field)->literal(null);
54
55
                break;
56
            case DateType::TYPE_NOT_NULL:
57
                $where->neq()->field('a.'.$field)->literal(null);
58
59
                break;
60
            case DateType::TYPE_EQUAL:
61
            default:
62
                $where->andX()
63
                    ->gte()->field('a.'.$field)->literal($from)->end()
64
                    ->lte()->field('a.'.$field)->literal($to)->end();
65
        }
66
67
        // filter is active as we have now modified the query
68
        $this->active = true;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getDefaultOptions()
75
    {
76
        return [
77
            'date_format' => 'yyyy-MM-dd',
78
        ];
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getRenderSettings()
85
    {
86
        return ['sonata_type_filter_date', [
87
            'field_type' => $this->getFieldType(),
88
            'field_options' => $this->getFieldOptions(),
89
            'label' => $this->getLabel(),
90
        ]];
91
    }
92
}
93