ModelAutocompleteFilter   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 3
dl 0
loc 101
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 16 6
A getDefaultOptions() 0 10 1
A getRenderSettings() 0 10 1
A handleMultiple() 0 16 4
A handleModel() 0 16 4
A association() 0 8 1
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\DoctrineORMAdminBundle\Filter;
15
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\ORM\QueryBuilder;
18
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
19
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
20
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
21
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;
22
23
class ModelAutocompleteFilter extends Filter
24
{
25
    public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void
26
    {
27
        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...
28
            return;
29
        }
30
31
        if ($data['value'] instanceof Collection) {
32
            $data['value'] = $data['value']->toArray();
33
        }
34
35
        if (\is_array($data['value'])) {
36
            $this->handleMultiple($queryBuilder, $alias, $data);
37
        } else {
38
            $this->handleModel($queryBuilder, $alias, $data);
39
        }
40
    }
41
42
    public function getDefaultOptions()
43
    {
44
        return [
45
            'field_name' => false,
46
            'field_type' => ModelAutocompleteType::class,
47
            'field_options' => [],
48
            'operator_type' => EqualOperatorType::class,
49
            'operator_options' => [],
50
        ];
51
    }
52
53
    public function getRenderSettings()
54
    {
55
        return [DefaultType::class, [
56
            'field_type' => $this->getFieldType(),
57
            'field_options' => $this->getFieldOptions(),
58
            'operator_type' => $this->getOption('operator_type'),
59
            'operator_options' => $this->getOption('operator_options'),
60
            'label' => $this->getLabel(),
61
        ]];
62
    }
63
64
    /**
65
     * For the record, the $alias value is provided by the association method (and the entity join method)
66
     *  so the field value is not used here.
67
     *
68
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
69
     * @param string                           $alias
70
     * @param mixed                            $data
71
     *
72
     * @return mixed
73
     */
74
    protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $data)
75
    {
76
        if (0 === \count($data['value'])) {
77
            return;
78
        }
79
80
        $parameterName = $this->getNewParameterName($queryBuilder);
81
82
        if (isset($data['type']) && EqualOperatorType::TYPE_NOT_EQUAL === $data['type']) {
83
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->notIn($alias, ':'.$parameterName));
84
        } else {
85
            $this->applyWhere($queryBuilder, $queryBuilder->expr()->in($alias, ':'.$parameterName));
86
        }
87
88
        $queryBuilder->setParameter($parameterName, $data['value']);
89
    }
90
91
    /**
92
     * @param ProxyQueryInterface|QueryBuilder $queryBuilder
93
     * @param string                           $alias
94
     * @param mixed                            $data
95
     *
96
     * @return mixed
97
     */
98
    protected function handleModel(ProxyQueryInterface $queryBuilder, $alias, $data)
99
    {
100
        if (empty($data['value'])) {
101
            return;
102
        }
103
104
        $parameterName = $this->getNewParameterName($queryBuilder);
105
106
        if (isset($data['type']) && EqualOperatorType::TYPE_NOT_EQUAL === $data['type']) {
107
            $this->applyWhere($queryBuilder, sprintf('%s != :%s', $alias, $parameterName));
108
        } else {
109
            $this->applyWhere($queryBuilder, sprintf('%s = :%s', $alias, $parameterName));
110
        }
111
112
        $queryBuilder->setParameter($parameterName, $data['value']);
113
    }
114
115
    protected function association(ProxyQueryInterface $queryBuilder, $data)
116
    {
117
        $associationMappings = $this->getParentAssociationMappings();
118
        $associationMappings[] = $this->getAssociationMapping();
119
        $alias = $queryBuilder->entityJoin($associationMappings);
120
121
        return [$alias, false];
0 ignored issues
show
Best Practice introduced by
The expression return array($alias, false); seems to be an array, but some of its elements' types (false) are incompatible with the return type of the parent method Sonata\DoctrineORMAdminB...ter\Filter::association of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
122
    }
123
}
124