Completed
Pull Request — 2.x (#521)
by Maximilian
02:11
created

NodeNameFilter::filter()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.4444
c 0
b 0
f 0
cc 8
nc 9
nop 4
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 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)) {
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...
25
            return;
26
        }
27
28
        $data['value'] = trim($data['value']);
29
        $data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
30
31
        if (0 == strlen($data['value'])) {
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
41
                break;
42
            case ChoiceType::TYPE_CONTAINS:
43
            default:
44
                $where->like()->localName($alias)->literal('%'.$data['value'].'%');
45
        }
46
47
        // filter is active as we have now modified the query
48
        $this->active = true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getDefaultOptions()
55
    {
56
        return [
57
            'format' => '%%%s%%',
58
        ];
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getRenderSettings()
65
    {
66
        return ['Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType', [
67
            'field_type' => $this->getFieldType(),
68
            'field_options' => $this->getFieldOptions(),
69
            'label' => $this->getLabel(),
70
        ]];
71
    }
72
}
73