Completed
Push — 3.x-dev-kit ( e63a8f )
by
unknown
02:51
created

StringFilter::filter()   C

Complexity

Conditions 12
Paths 66

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 19
c 2
b 0
f 0
nc 66
nop 4
dl 0
loc 33
rs 5.1612

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\DoctrineMongoDBAdminBundle\Filter;
13
14
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
15
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
16
17
class StringFilter extends Filter
18
{
19
    /**
20
     * @param ProxyQueryInterface $queryBuilder
21
     * @param string              $alias
0 ignored issues
show
Bug introduced by
There is no parameter named $alias. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
     * @param string              $field
23
     * @param string              $data
24
     *
25
     * @return
26
     */
27
    public function filter(ProxyQueryInterface $queryBuilder, $name, $field, $data)
28
    {
29
        if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
30
            return;
31
        }
32
33
        $data['value'] = trim($data['value']);
34
35
        if (strlen($data['value']) == 0) {
36
            return;
37
        }
38
39
        $data['type'] = isset($data['type']) && !empty($data['type']) ? $data['type'] : ChoiceType::TYPE_CONTAINS;
40
41
        $obj = $queryBuilder;
42
        if ($this->condition == self::CONDITION_OR) {
43
            $obj = $queryBuilder->expr();
44
        }
45
46
        if ($data['type'] == ChoiceType::TYPE_EQUAL) {
47
            $obj->field($field)->equals($data['value']);
48
        } elseif ($data['type'] == ChoiceType::TYPE_CONTAINS) {
49
            $obj->field($field)->equals(new \MongoRegex(sprintf('/%s/i', $data['value'])));
50
        } elseif ($data['type'] == ChoiceType::TYPE_NOT_CONTAINS) {
51
            $obj->field($field)->not(new \MongoRegex(sprintf('/%s/i', $data['value'])));
52
        }
53
54
        if ($this->condition == self::CONDITION_OR) {
55
            $queryBuilder->addOr($obj);
56
        }
57
58
        $this->active = true;
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getDefaultOptions()
65
    {
66
        return array();
67
    }
68
69
    public function getRenderSettings()
70
    {
71
        return array('sonata_type_filter_choice', array(
72
                'field_type' => $this->getFieldType(),
73
                'field_options' => $this->getFieldOptions(),
74
                'label' => $this->getLabel(),
75
        ));
76
    }
77
}
78