Completed
Push — master ( eb83f4...bb238e )
by Maximilian
14s
created

ChoiceFilter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
C filter() 0 39 13
A getDefaultOptions() 0 4 1
A getRenderSettings() 0 9 1
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\AdminBundle\Form\Type\Filter\ChoiceType;
16
17
class ChoiceFilter 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('type', $data) || !array_key_exists('value', $data)) {
25
            return;
26
        }
27
28
        $values = (array) $data['value'];
29
        $type = $data['type'];
30
31
        // clean values
32
        foreach ($values as $key => $value) {
33
            $value = trim($value);
34
            if (!$value) {
35
                unset($values[$key]);
36
            } else {
37
                $values[$key] = $value;
38
            }
39
        }
40
41
        // if values not set or "all" specified, do not do this filter
42
        if (!$values || in_array('all', $values, true)) {
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...
43
            return;
44
        }
45
46
        $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...
47
48
        foreach ($values as $value) {
49
            if ($type == ChoiceType::TYPE_NOT_CONTAINS) {
50
                $andX->not()->like()->field('a.'.$field)->literal('%'.$value.'%');
51
            } elseif ($type == ChoiceType::TYPE_CONTAINS) {
52
                $andX->like()->field('a.'.$field)->literal('%'.$value.'%');
53
            } elseif ($type == ChoiceType::TYPE_EQUAL) {
54
                $andX->like()->field('a.'.$field)->literal($value);
55
            }
56
        }
57
58
        // filter is active as we have now modified the query
59
        $this->active = true;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getDefaultOptions()
66
    {
67
        return array();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getRenderSettings()
74
    {
75
        return array('sonata_type_filter_default', array(
76
            'operator_type' => 'sonata_type_equal',
77
            'field_type' => $this->getFieldType(),
78
            'field_options' => $this->getFieldOptions(),
79
            'label' => $this->getLabel(),
80
        ));
81
    }
82
}
83