FilterQueryBuilder::addInExpression()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 28.01.19
6
 * Time: 19:30
7
 */
8
9
namespace Sf4\Api\Dto\Filter;
10
11
use Doctrine\ORM\QueryBuilder;
12
13
class FilterQueryBuilder
14
{
15
    /**
16
     * @param QueryBuilder $queryBuilder
17
     * @param FilterItemInterface $filterItem
18
     * @param string $fieldName
19
     */
20
    public static function buildQuery(
21
        QueryBuilder $queryBuilder,
22
        ?FilterItemInterface $filterItem,
23
        string $fieldName
24
    ): void {
25
        if (!$filterItem || empty($filterItem->getValue())) {
26
            return;
27
        }
28
        switch ($filterItem->getType()) {
29
            case FilterItemInterface::TYPE_LIKE:
30
                $expression = static::addLikeExpression($queryBuilder, $filterItem, $fieldName);
31
                break;
32
            case FilterItemInterface::TYPE_EQUAL:
33
                $expression = static::addEqualExpression($queryBuilder, $filterItem, $fieldName);
34
                break;
35
            case FilterItemInterface::TYPE_NOT_EQUAL:
36
                $expression = static::addNotEqualExpression($queryBuilder, $filterItem, $fieldName);
37
                break;
38
            case FilterItemInterface::TYPE_IN:
39
                $expression = static::addInExpression($queryBuilder, $filterItem, $fieldName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $expression is correct as static::addInExpression(...filterItem, $fieldName) targeting Sf4\Api\Dto\Filter\Filte...lder::addInExpression() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
                break;
41
            case FilterItemInterface::TYPE_NOT_IN:
42
                $expression = static::addNotInExpression($queryBuilder, $filterItem, $fieldName);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $expression is correct as static::addNotInExpressi...filterItem, $fieldName) targeting Sf4\Api\Dto\Filter\Filte...r::addNotInExpression() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
                break;
44
            case FilterItemInterface::TYPE_IS_NULL:
45
                $expression = static::addIsNullExpression($queryBuilder, $fieldName);
46
                break;
47
            case FilterItemInterface::TYPE_NOT_NULL:
48
                $expression = static::addIsNotNullExpression($queryBuilder, $fieldName);
49
                break;
50
            default:
51
                $expression = null;
52
        }
53
        if ($expression) {
54
            $queryBuilder->andWhere(
55
                $expression
56
            );
57
        }
58
    }
59
60
    /**
61
     * @param QueryBuilder $queryBuilder
62
     * @param FilterItemInterface $filterItem
63
     * @param string $fieldName
64
     * @return \Doctrine\ORM\Query\Expr\Comparison
65
     */
66
    protected static function addLikeExpression(
67
        QueryBuilder $queryBuilder,
68
        FilterItemInterface $filterItem,
69
        string $fieldName
70
    ): \Doctrine\ORM\Query\Expr\Comparison {
71
        $paramKey = uniqid(':like_', true);
72
        $queryBuilder->setParameter($paramKey, $filterItem->getValue());
73
        return $queryBuilder->expr()->like($fieldName, $paramKey);
74
    }
75
76
    /**
77
     * @param QueryBuilder $queryBuilder
78
     * @param FilterItemInterface $filterItem
79
     * @param string $fieldName
80
     * @return \Doctrine\ORM\Query\Expr\Comparison
81
     */
82
    protected static function addEqualExpression(
83
        QueryBuilder $queryBuilder,
84
        FilterItemInterface $filterItem,
85
        string $fieldName
86
    ): \Doctrine\ORM\Query\Expr\Comparison {
87
        $paramKey = uniqid(':eq_', true);
88
        $queryBuilder->setParameter($paramKey, $filterItem->getValue());
89
        return $queryBuilder->expr()->eq($fieldName, $paramKey);
90
    }
91
92
    /**
93
     * @param QueryBuilder $queryBuilder
94
     * @param FilterItemInterface $filterItem
95
     * @param string $fieldName
96
     * @return \Doctrine\ORM\Query\Expr\Comparison
97
     */
98
    protected static function addNotEqualExpression(
99
        QueryBuilder $queryBuilder,
100
        FilterItemInterface $filterItem,
101
        string $fieldName
102
    ): \Doctrine\ORM\Query\Expr\Comparison {
103
        $paramKey = uniqid(':neq_', true);
104
        $queryBuilder->setParameter($paramKey, $filterItem->getValue());
105
        return $queryBuilder->expr()->neq($fieldName, $paramKey);
106
    }
107
108
    /**
109
     * @param QueryBuilder $queryBuilder
110
     * @param FilterItemInterface $filterItem
111
     * @param string $fieldName
112
     * @return \Doctrine\ORM\Query\Expr\Func|null
113
     */
114
    protected static function addInExpression(
115
        QueryBuilder $queryBuilder,
116
        FilterItemInterface $filterItem,
117
        string $fieldName
118
    ): ?\Doctrine\ORM\Query\Expr\Func {
119
        $values = static::convertStringToArray($filterItem->getValue());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $values is correct as static::convertStringToA...filterItem->getValue()) targeting Sf4\Api\Dto\Filter\Filte...:convertStringToArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120
121
        if (true === is_array($values)) {
0 ignored issues
show
introduced by
The condition true === is_array($values) is always false.
Loading history...
122
            return $queryBuilder->expr()->in($fieldName, $values);
123
        }
124
        return null;
125
    }
126
127
    /**
128
     * @param $string
129
     * @return array|null
130
     */
131
    protected static function convertStringToArray($string): ?array
132
    {
133
        if (false === is_array($string) && true === is_scalar($string)) {
0 ignored issues
show
introduced by
The condition true === is_scalar($string) is always false.
Loading history...
134
            if (false !== strpos($string, ',')) {
135
                return explode(',', $string);
136
            }
137
            return [
138
                $string
139
            ];
140
        }
141
142
        return null;
143
    }
144
145
    /**
146
     * @param QueryBuilder $queryBuilder
147
     * @param FilterItemInterface $filterItem
148
     * @param string $fieldName
149
     * @return \Doctrine\ORM\Query\Expr\Func|null
150
     */
151
    protected static function addNotInExpression(
152
        QueryBuilder $queryBuilder,
153
        FilterItemInterface $filterItem,
154
        string $fieldName
155
    ): ?\Doctrine\ORM\Query\Expr\Func {
156
        $values = static::convertStringToArray($filterItem->getValue());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $values is correct as static::convertStringToA...filterItem->getValue()) targeting Sf4\Api\Dto\Filter\Filte...:convertStringToArray() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
158
        if (true === is_array($values)) {
0 ignored issues
show
introduced by
The condition true === is_array($values) is always false.
Loading history...
159
            return $queryBuilder->expr()->notIn($fieldName, $values);
160
        }
161
        return null;
162
    }
163
164
    /**
165
     * @param QueryBuilder $queryBuilder
166
     * @param string $fieldName
167
     * @return string
168
     */
169
    protected static function addIsNullExpression(QueryBuilder $queryBuilder, string $fieldName): string
170
    {
171
        return $queryBuilder->expr()->isNull($fieldName);
172
    }
173
174
    /**
175
     * @param QueryBuilder $queryBuilder
176
     * @param string $fieldName
177
     * @return string
178
     */
179
    protected static function addIsNotNullExpression(QueryBuilder $queryBuilder, string $fieldName): string
180
    {
181
        return $queryBuilder->expr()->isNotNull($fieldName);
182
    }
183
}
184