ChoiceFilter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultOptions() 0 4 1
A getRenderSettings() 0 9 1
C filter() 0 39 12
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\DoctrinePHPCRAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
18
19
class ChoiceFilter extends Filter
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data): void
25
    {
26
        if (!$data || !\is_array($data) || !\array_key_exists('type', $data) || !\array_key_exists('value', $data)) {
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...
27
            return;
28
        }
29
30
        $values = (array) $data['value'];
31
        $type = $data['type'];
32
33
        // clean values
34
        foreach ($values as $key => $value) {
35
            $value = trim((string) $value);
36
            if (!$value) {
37
                unset($values[$key]);
38
            } else {
39
                $values[$key] = $value;
40
            }
41
        }
42
43
        // if values not set, do not do this filter
44
        if (!$values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values 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...
45
            return;
46
        }
47
48
        $andX = $this->getWhere($proxyQuery)->andX();
0 ignored issues
show
Compatibility introduced by
$proxyQuery of type object<Sonata\AdminBundl...id\ProxyQueryInterface> is not a sub-type of object<Sonata\DoctrinePH...le\Datagrid\ProxyQuery>. It seems like you assume a concrete implementation of the interface Sonata\AdminBundle\Datagrid\ProxyQueryInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
49
50
        foreach ($values as $value) {
51
            if (ChoiceType::TYPE_NOT_CONTAINS === $type) {
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...Type::TYPE_NOT_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_NOT_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
52
                $andX->not()->like()->field('a.'.$field)->literal('%'.$value.'%');
53
            } elseif (ChoiceType::TYPE_CONTAINS === $type) {
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...oiceType::TYPE_CONTAINS has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_CONTAINS instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
54
                $andX->like()->field('a.'.$field)->literal('%'.$value.'%');
55
            } elseif (ChoiceType::TYPE_EQUAL === $type) {
0 ignored issues
show
Deprecated Code introduced by
The constant Sonata\AdminBundle\Form\...\ChoiceType::TYPE_EQUAL has been deprecated with message: since sonata-project/admin-bundle 3.57, to be removed with 4.0: Use ContainsOperatorType::TYPE_EQUAL instead

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
56
                $andX->like()->field('a.'.$field)->literal($value);
57
            }
58
        }
59
60
        // filter is active as we have now modified the query
61
        $this->active = true;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getDefaultOptions()
68
    {
69
        return [];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getRenderSettings()
76
    {
77
        return ['sonata_type_filter_default', [
78
            'operator_type' => 'sonata_type_equal',
79
            'field_type' => $this->getFieldType(),
80
            'field_options' => $this->getFieldOptions(),
81
            'label' => $this->getLabel(),
82
        ]];
83
    }
84
}
85