Completed
Push — flintci-15636 ( b7d19a )
by Sullivan
04:58 queued 03:11
created

NumberFilter::filter()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.0111
c 0
b 0
f 0
cc 6
nc 3
nop 4
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\DoctrineORMAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
18
19
class NumberFilter extends Filter
20
{
21
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
22
    {
23
        if (!$data || !\is_array($data) || !array_key_exists('value', $data) || !is_numeric($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...
24
            return;
25
        }
26
27
        $type = $data['type'] ?? false;
28
29
        $operator = $this->getOperator($type);
30
31
        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...
32
            $operator = '=';
33
        }
34
35
        // c.name > '1' => c.name OPERATOR :FIELDNAME
36
        $parameterName = $this->getNewParameterName($queryBuilder);
37
        $this->applyWhere($queryBuilder, sprintf('%s.%s %s :%s', $alias, $field, $operator, $parameterName));
38
        $queryBuilder->setParameter($parameterName, $data['value']);
39
    }
40
41
    public function getDefaultOptions()
42
    {
43
        return [];
44
    }
45
46
    public function getRenderSettings()
47
    {
48
        return [NumberType::class, [
49
            'field_type' => $this->getFieldType(),
50
            'field_options' => $this->getFieldOptions(),
51
            'label' => $this->getLabel(),
52
        ]];
53
    }
54
55
    /**
56
     * @param string $type
57
     *
58
     * @return bool
59
     */
60
    private function getOperator($type)
61
    {
62
        $choices = [
63
            NumberType::TYPE_EQUAL => '=',
64
            NumberType::TYPE_GREATER_EQUAL => '>=',
65
            NumberType::TYPE_GREATER_THAN => '>',
66
            NumberType::TYPE_LESS_EQUAL => '<=',
67
            NumberType::TYPE_LESS_THAN => '<',
68
        ];
69
70
        return $choices[$type] ?? false;
71
    }
72
}
73