Completed
Push — 1.2 ( d3ea0d...d8c512 )
by David
16:21
created

NodeNameFilter::filter()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 27
rs 5.3846
cc 8
eloc 16
nc 9
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
15
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
16
17
class NodeNameFilter 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
        $data['value'] = trim($data['value']);
29
        $data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
30
31
        if (strlen($data['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
37
        switch ($data['type']) {
38
            case ChoiceType::TYPE_EQUAL:
39
                $where->eq()->localName($alias)->literal($data['value']);
40
                break;
41
            case ChoiceType::TYPE_CONTAINS:
42
            default:
43
                $where->like()->localName($alias)->literal('%'.$data['value'].'%');
44
        }
45
46
        // filter is active as we have now modified the query
47
        $this->active = true;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function getDefaultOptions()
54
    {
55
        return array(
56
            'format'   => '%%%s%%'
57
        );
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getRenderSettings()
64
    {
65
        return array('doctrine_phpcr_type_filter_choice', array(
66
            'field_type'    => $this->getFieldType(),
67
            'field_options' => $this->getFieldOptions(),
68
            'label'         => $this->getLabel()
69
        ));
70
    }
71
}
72