Completed
Push — 3.x ( 064670...3a8e9d )
by Vincent
01:24
created

src/Filter/NumberFilter.php (1 issue)

Check for loose comparison of strings.

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
18
use Sonata\AdminBundle\Form\Type\Operator\NumberOperatorType;
19
20
class NumberFilter extends Filter
21
{
22
    /**
23
     * @param string $alias
24
     * @param string $field
25
     * @param string $data
26
     */
27
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
28
    {
29
        if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || !is_numeric($data['value'])) {
30
            return;
31
        }
32
33
        $type = $data['type'] ?? false;
34
35
        $operator = $this->getOperator($type);
36
37
        if (!$operator) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $operator of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false 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...
38
            $operator = 'equals';
39
        }
40
41
        $queryBuilder->field($field)->$operator((float) $data['value']);
42
        $this->active = true;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getDefaultOptions()
49
    {
50
        return [];
51
    }
52
53
    public function getRenderSettings()
54
    {
55
        return [NumberType::class, [
56
            'field_type' => $this->getFieldType(),
57
            'field_options' => $this->getFieldOptions(),
58
            'label' => $this->getLabel(),
59
        ]];
60
    }
61
62
    /**
63
     * @param $type
64
     *
65
     * @return bool
66
     */
67
    private function getOperator($type)
68
    {
69
        $choices = [
70
            NumberOperatorType::TYPE_EQUAL => 'equals',
71
            NumberOperatorType::TYPE_GREATER_EQUAL => 'gte',
72
            NumberOperatorType::TYPE_GREATER_THAN => 'gt',
73
            NumberOperatorType::TYPE_LESS_EQUAL => 'lte',
74
            NumberOperatorType::TYPE_LESS_THAN => 'lt',
75
        ];
76
77
        return $choices[$type] ?? false;
78
    }
79
}
80