Completed
Push — move_function ( 8e1d00...e3c266 )
by Maximilian
04:27 queued 02:18
created

StringFilter::filter()   C

Complexity

Conditions 13
Paths 31

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 30
nc 31
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
16
17
class StringFilter extends Filter
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
23
    {
24
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
25
            return;
26
        }
27
28
        $value = trim($data['value']);
29
        $data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
30
31
        if (strlen($value) == 0) {
32
            return;
33
        }
34
35
        $where = $this->getWhere($proxyQuery);
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...
36
        $isComparisonLowerCase = $this->getOption('compare_case_insensitive');
37
        $value = $isComparisonLowerCase ? strtolower($value) : $value;
38
        switch ($data['type']) {
39
            case ChoiceType::TYPE_EQUAL:
40
                if ($isComparisonLowerCase) {
41
                    $where->eq()->lowerCase()->field('a.'.$field)->end()->literal($value);
42
                } else {
43
                    $where->eq()->field('a.'.$field)->literal($value);
44
                }
45
46
                break;
47
            case ChoiceType::TYPE_NOT_CONTAINS:
48
                $where->fullTextSearch('a.'.$field, '* -'.$value);
49
                break;
50
            case ChoiceType::TYPE_CONTAINS:
51
                if ($isComparisonLowerCase) {
52
                    $where->like()->lowerCase()->field('a.'.$field)->end()->literal('%'.$value.'%');
53
                } else {
54
                    $where->like()->field('a.'.$field)->literal('%'.$value.'%');
55
                }
56
57
                break;
58
            case ChoiceType::TYPE_CONTAINS_WORDS:
59
            default:
60
                $where->fullTextSearch('a.'.$field, $value);
61
        }
62
63
        // filter is active as we have now modified the query
64
        $this->active = true;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getDefaultOptions()
71
    {
72
        return array(
73
            'format' => '%%%s%%',
74
            'compare_lower_case' => false,
75
        );
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getRenderSettings()
82
    {
83
        return array('Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType', array(
84
            'field_type' => $this->getFieldType(),
85
            'field_options' => $this->getFieldOptions(),
86
            'label' => $this->getLabel(),
87
        ));
88
    }
89
}
90