Completed
Push — master ( bae860...fc1986 )
by Grégoire
11s
created

src/Filter/StringFilter.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineMongoDBAdminBundle\Filter;
15
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
18
19
class StringFilter extends Filter
20
{
21
    /**
22
     * @param ProxyQueryInterface $queryBuilder
23
     * @param string              $alias
0 ignored issues
show
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...
24
     * @param string              $field
25
     * @param string              $data
26
     */
27
    public function filter(ProxyQueryInterface $queryBuilder, $name, $field, $data): void
28
    {
29
        if (!$data || !\is_array($data) || !array_key_exists('value', $data) || null === $data['value']) {
30
            return;
31
        }
32
33
        $data['value'] = trim($data['value']);
34
35
        if (0 === \strlen($data['value'])) {
36
            return;
37
        }
38
39
        $data['type'] = isset($data['type']) && !empty($data['type']) ? $data['type'] : ChoiceType::TYPE_CONTAINS;
40
41
        $obj = $queryBuilder;
42
        if (self::CONDITION_OR === $this->condition) {
43
            $obj = $queryBuilder->expr();
44
        }
45
46
        if (ChoiceType::TYPE_EQUAL === $data['type']) {
47
            $obj->field($field)->equals($data['value']);
48
        } elseif (ChoiceType::TYPE_CONTAINS === $data['type']) {
49
            $obj->field($field)->equals(new \MongoRegex(sprintf('/%s/i', $data['value'])));
50
        } elseif (ChoiceType::TYPE_NOT_CONTAINS === $data['type']) {
51
            $obj->field($field)->not(new \MongoRegex(sprintf('/%s/i', $data['value'])));
52
        }
53
54
        if (self::CONDITION_OR === $this->condition) {
55
            $queryBuilder->addOr($obj);
56
        }
57
58
        $this->active = true;
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getDefaultOptions()
65
    {
66
        return [];
67
    }
68
69
    public function getRenderSettings()
70
    {
71
        return [ChoiceType::class, [
72
            'field_type' => $this->getFieldType(),
73
            'field_options' => $this->getFieldOptions(),
74
            'label' => $this->getLabel(),
75
        ]];
76
    }
77
}
78